;;------------------------------------------------LICENSE NOTICE------------------------------------------------------------------------------
;;  This file is part of Amstrad CPC videogame "Redsea"
;;  Copyright (C) 2021 Crab Party / Elena López (AneleSplats in itch.io) / Iván Tortosa (Pluvs in itch.io, @IvanTortosaBer1 on Twitter) / Antonio Gosálvez (BalgroHub in itch.io)
;;  Copyright (C) 2015 ronaldo / Fremos / Cheesetea / ByteRealms (@FranGallegoBR)
;;
;;  This program is free software: you can redistribute it and/or modify
;;  it under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation, either version 3 of the License, or
;;  (at your option) any later version.
;;
;;  This program is distributed in the hope that it will be useful,
;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;  GNU Lesser General Public License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;------------------------------------------------LICENSE NOTICE-------------------------------------------------------------------------
.include "cpctelera.h.s"

;Variables globales
.globl cpct_etm_setDrawTilemap4x8_ag_asm
.globl cpct_etm_drawTilemap4x8_ag_asm
.globl cpct_setVideoMode_asm
.globl cpct_setPalette_asm
.globl cpct_getScreenPtr_asm
.globl cpct_drawSprite_asm
.globl cpct_drawSolidBox_asm
.globl man_entity_forall
.globl e_type_dead

.globl man_entity_forall_matching
.globl matching_type
.globl _main_palette



;Cambia el modo a 0, el color de fondo a negro y el borde
sys_render_init::
   ld c, #0
   call cpct_setVideoMode_asm

   cpctm_setBorder_asm HW_BLACK
   ;ld hl, #palette
   ld hl, #_main_palette
   ld de, #16
   call cpct_setPalette_asm ;HW_BLACK

ret

sys_render_gameinit::
    
    call sys_render_tilemap

ret

; sys_render_level_start::
;     ;ld de, #0xC000
;     ;ld b, #180
;     ;ld c, #0
;     ;call cpct_getScreenPtr_asm
;     cpctm_screenPtrSym_asm floor_ptr1, 0xC000, 0, 180
;     cpctm_screenPtrSym_asm floor_ptr2, 0xC000, 40, 180

;     ld de, #floor_ptr1
;     ld a, #0xFF
;     ld bc, #0x1428
;     call cpct_drawSolidBox_asm

;     ld de, #floor_ptr2
;     ld a, #0xFF
;     ld bc, #0x1428
;     call cpct_drawSolidBox_asm
; ret



;Dibuja los sprites de cada entidad por pantalla
;Se le pasa la dir por hl
sys_render_one_entity::


    ; Si la entidad esta marcada para morir, se borra visualmente
    ld a, #0x80
    ld b, a
    ld a, (ix)
    and b
    jr z, seguir4

    ;ERASE ENTITY
    ld de, #0xC000
    ld c, 1(ix)
    ld b, 2(ix)
    call cpct_getScreenPtr_asm

    ld d, h
    ld e, l
    xor a
    ld b, 4(ix)
    ld c, 3(ix)
    
    call cpct_drawSolidBox_asm


    ret
   seguir4:

    ;ERASE ENTITY
    ld e, 11(ix)
    ld d, 12(ix)
    xor a
    ld b, 4(ix)
    ld c, 3(ix)
    
    call cpct_drawSolidBox_asm

    ;CALCULATE NEW VIDEO LOCATION
    ld de, #0xC000

    ld c, 1(ix)

    ld b, 2(ix)


    call cpct_getScreenPtr_asm ;pide de, c y b, devuelve hl
    ex de, hl   ;hl = 0xC000 de = pos memoria


    ;SAVE prevPtr for erasing on next frame
    ld 11(ix), e ;prevPtr
    ld 12(ix), d ;prevPtr+1


    ;DRAW ENTITY
    ld c, 3(ix)

    ld b, 4(ix)

    push de
    ld e, 7(ix)
    ld d, 8(ix)
    ex de, hl
    pop de
    call cpct_drawSprite_asm ;pide hl, de, c y b
ret

;Le pasa la dir de la funcion sys_render_one_entity por (hl)
;Se le aplica la funcion a todas las unidades
sys_render_update::
    ld ix, #sys_render_one_entity
    ld a, #1
    ld (#matching_type), a
    call man_entity_forall_matching
ret

.globl _tilemap_01
.globl _tilemap_02
.globl _tiles_00
.globl tilemapActual
sys_render_tilemap:: ;hay que poner los sprites con lo del image_conversion.mk de IMAGE FORMAT zgtiles
    ;push de
    ld bc, #0x1914 ; height in tiles of view windows| width in tiles of view window
    ld de, #0x14 ;width in tiles of the complete tilemap
    ld hl,  #_tiles_00 ;#_tiles_00 poner esto no el 0 ;pointer to the start of the tileset
    call cpct_etm_setDrawTilemap4x8_ag_asm

    ld hl, #0xC000 ;video memory location where to draw the tilemap (character & 4-byte aligned)
    push ix
    ld ix, #tilemapActual
    ld a, (ix) ; Coge el tilemap que esté en la variable tilemapActual de game
    ld e, a
    ld a, 1(ix)
    ld d, a
    call cpct_etm_drawTilemap4x8_ag_asm
    pop ix
    ;jr .
    ret


; Recibe el tilemap por de
sys_render_tilemapActual:: ;hay que poner los sprites con lo del image_conversion.mk de IMAGE FORMAT zgtiles
    ;push de
    push de
    ld bc, #0x1914 ; height in tiles of view windows| width in tiles of view window
    ld de, #0x14 ;width in tiles of the complete tilemap
    ld hl,  #_tiles_00 ;#_tiles_00 poner esto no el 0 ;pointer to the start of the tileset
    call cpct_etm_setDrawTilemap4x8_ag_asm
    
    
    
    ld hl, #0xC000 ;video memory location where to draw the tilemap (character & 4-byte aligned)
    pop de
    call cpct_etm_drawTilemap4x8_ag_asm

ret

