;; This file is part of Moonsters
;;  Copyright (C) 2021 Dam Studios
;;
;;     Moonsters is free software: you can redistribute it and/or modify
;;     it under the terms of the GNU General Public License as published by
;;     the Free Software Foundation, either version 3 of the License, or
;;     (at your option) any later version.
;; 
;;     SHOOT TO MOVE 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 General Public License for more details.
;; 
;;     You should have received a copy of the GNU General Public License
;;     along with Moonsters  If not, see <https://www.gnu.org/licenses/>.

.include "man/entity_manager.h.s"
.include "sys/render.h.s"
.include "assets/assets.h.s"


;;
;; RENDER SYSTEM
;;

MAXSCROLL = 0x40
SCR_TILE_WIDTH = 40
MAP_WIDTH = 105
MAP_HEIGHT = 50
VIDEO_POINTER = 0x8000


;;return scroll in a
sys_render_getScroll::

	ld a, (scroll)

	ret

sys_render_init::
	ld l, #0x20
	call cpct_setVideoMemoryPage_asm

	ld de, #VIDEO_POINTER;(2B DE) array  Pointer to the first byte of the array to be filled up (starting point in memory)
   	ld a, #0x00;(1B A ) value  8-bit value to be set
   	ld bc, #0x4000;(2B BC) size   Number of bytes to be set (>= 2)
   	call cpct_memset_asm

	;ld c, #0
	;call cpct_setVideoMode_asm ;;pone el modo de video a cero

	
	ld a, #00
	ld (scroll), a
	ld l, a
   call cpct_setVideoMemoryOffset_asm


	call sys_render_tilemap_init



	ret
.globl	_g_palette
.globl	_g_tileset


.globl cpct_etm_setTileset2x4_asm
.globl cpct_memset_asm
.globl cpct_etm_drawTileBox2x4_asm
.globl cpct_zx7b_decrunch_s_asm


tilemap_comprimido::
	.dw 0x0000


sys_render_tilemap_init:

		;;DESCOMPRIMIMOS MAPA INICIAL

		ld de, #tilemap_end;(2B DE) dest_end	Ending (latest) byte of the destination (decompressed) array
		ld hl, (tilemap_comprimido);(2B HL) source_end	Ending (latest) byte of the source (compressed) array
		call cpct_zx7b_decrunch_s_asm
		

 		ld hl, #_g_tileset;(2B HL) ptileset Pointer to the start of the tileset (array of pointers to tile definitions)
   	call cpct_etm_setTileset2x4_asm

   	
   ;; Set Parameters on the stack
   	ld   hl, #tilemap_actual   ;; HL = pointer to the tilemap
    	push hl              ;; Push ptilemap to the stack
    	ld   hl, #VIDEO_POINTER  ;; HL = Pointer to video memory location where tilemap is drawn
    	push hl              ;; Push pvideomem to the stack
    	;; Set Paramters on registers
    	ld    a, #MAP_WIDTH  ;; A = map_width
    	ld    b, #0          ;; B = x tile-coordinate
    	ld    c, #0          ;; C = y tile-coordinate
    	ld    d, #MAP_HEIGHT ;; H = height in tiles of the tile-box
    	ld    e, #SCR_TILE_WIDTH   ;; L =  width in tiles of the tile-box
    	call  cpct_etm_drawTileBox2x4_asm ;; Call the function

	ret

scroll:
	.db 0x00


pVideo:
   .dw VIDEO_POINTER

pTilemap:
  .dw tilemap_actual + 39

sys_render_tilemap_update:

	;;COMPROBAMOS MAXSCROLL

	ld a, (scroll)
	cp #MAXSCROLL
	jr z, noMoverScroll

	ld b, a
	add a, b
	ld b, a
	;;CALCULAMOS SCROLL
	call man_entity_getArray
	ld a, e_x(ix)
	sub a, b
	ld b, #40
	sub a, b ;;si x menor que cuarenta
	jr c, noMoverScroll

	ld a, (scroll)
	inc a
	ld (scroll), a



   ld a, (scroll)
   ld l, a
   call cpct_setVideoMemoryOffset_asm

   ld de, #VIDEO_POINTER
   ld a, (scroll)
   ld b, a
   add a, b
   dec a
   dec a
   ld b, #0x50
   add a, b
   ld c, a ;;x
   ld b, #0 ;;y
   call cpct_getScreenPtr_asm

   ld (pVideo), hl

    ;; Set Parameters on the stack
    ;;sumamos a _g_tilemap + 39 el scroll
    ld   hl, (pTilemap)   ;; HL = pointer to the tilemap
    ld a, (scroll)
    ld c, a
    ld b, #00
    add hl, bc
    push hl              ;; Push ptilemap to the stack
    ld   hl, (pVideo)  ;; HL = Pointer to video memory location where tilemap is drawn
    push hl              ;; Push pvideomem to the stack
    ;; Set Paramters on registers
    ld    a, #MAP_WIDTH  ;; A = map_width
    ld    b, #0          ;; B = x tile-coordinate
    ld    c, #0          ;; C = y tile-coordinate
    ld    d, #MAP_HEIGHT ;; H = height in tiles of the tile-box
    ld    e, #1   ;; L =  width in tiles of the tile-box
    call  cpct_etm_drawTileBox2x4_asm ;; Call the function

noMoverScroll:

	ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;UPDATE
;;Llama a sys_render_one_entity para cada entidad
;;Registros destruidos:
;; AF, BC, DE, HL, IX
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_render_update::

	
	call sys_render_tilemap_update

	
	ld hl, #sys_render_one_entity
	call man_entity_forall
	ret


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Render One entity
;;Borra una entidad y la dibuja (si esta marcada para destruccion solo la borra) 
;;Input:
;; IX-> puntero a la entidad
;;Registros destruidos:
;; AF, BC, DE, HL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_render_one_entity::


	ld a, e_render(ix)
	cp #e_render_notVisible ;;falta comprobar COMENTAR ESTO PARA DEBUGEAR PLATAFORMAS
	jr z, not_render

	ld d, e_oldVideo_l(ix)
	ld e, e_oldVideo_h(ix) ;;cargamos la posicion antigua
	ld a, #0x00
	ld b, e_h(ix)
	ld c, e_w(ix)
	call cpct_drawSolidBox_asm ;;Borramos lo antiguo

	ld a, e_type(ix)
	cp #e_type_destroy
	jr z, not_render ;;si esta marcado para destruir borramos pero no dibujamos


	ld a, e_render(ix)
	cp #e_render_notVisibleFirst ;;falta comprobar COMENTAR ESTO PARA DEBUGEAR PLATAFORMAS
	jr z, not_render_First


	;;CALCULAMOS POSICION DE MEMORIA
	ld de, #VIDEO_POINTER ;;OJO SI SE USA DOBLE BUFFER HAY QUE MODIFICAR
	ld c, e_x(ix)
	ld b, e_y(ix)
	call cpct_getScreenPtr_asm

	ex de, hl

	;;Nos guardamos la posicion para borrar la proxima vez
	ld e_oldVideo_l(ix), d
	ld e_oldVideo_h(ix), e

	;;Ahora dibujamos
	;ld a, #0xFF
	ld c, e_w(ix)
	ld b, e_h(ix)

	;call cpct_drawSolidBox_asm
	ld a, e_type(ix)
	cp #e_type_static
	jr z, render_box


	ld h, e_pspr_h(ix)
	ld l, e_pspr_l(ix)
	call cpct_drawSprite_asm
	jr not_render
render_box:


	ld a, #0xFF
	call cpct_drawSolidBox_asm

not_render:

	ret

not_render_First:
	ld a, #e_render_notVisible
	ld e_render(ix), a
	ret


tilemap_actual:
	.ds 5250
tilemap_end = .-1