;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of The Last Fungus: An Amstrad CPC Game 
;;  Copyright (C) 2021 Miguel Perez Tarruella / Antonio Jose Fernandez Belliure / 
;;                     David Martinez Garcia / Ocacho Games (@OcachoGames)
;;
;;  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/>.
;;------------------------------------------------------------------------------

;;====================================================================
;; GLOBL INCLUDES
;;====================================================================
.include "render.h.s"
.include "cpctelera.h.s"

;;Managers
.include "man/entity.h.s"
.include "man/level_loader.h.s"

;;Globals
.include "global/global.h.s"

;;Math
.include "macros/math.h.s"

;;====================================================================
;; GLOBL FUNCTIONS
;;====================================================================
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -  
;; Objetive: Initialize all the values for the render.
;;
;; Modifies: C, DE, HL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_render_init::
	ld c, #0
    call cpct_setVideoMode_asm
	
    ld hl, #_g_palette
    ld de, #16
    call cpct_setPalette_asm

	cpctm_setBorder_asm HW_BLACK
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -  
;; Objetive: Initialize all the values to show the menu.
;;
;; Modifies: -
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_render_menu_init::
	cpctm_clearScreen_asm 0x00

	;;Here we weill draw the things for the menu

ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -  
;; Objetive: Initialize all the values to start the game.
;;
;; Modifies: A, BC, DE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_render_game_init::
	;;Here we get in HL the memory position of the current level background
	call man_level_get_current_background
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm
ret

;;====================================================================
;; LOCAL FUNCTIONS
;;====================================================================
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: should contain the entity's memory direction we want to update the input 
;; Objetive: Deletes the previous position of the entity.
;;
;; Modifies: A, BC, DE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_render_delete_one_entity:
	;;Here we draw with 00 color a box with the width and height of the entity
	ld e, e_prevptr_b1(ix)
	ld d, e_prevptr_b2(ix)

	ld a, #0x00
	ld c, e_width(ix)
	ld b, e_height(ix)

	call cpct_drawSolidBox_asm
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: should contain the entity's memory direction we want to update the input 
;; Objetive: Draws the entity in the new position.
;;
;; Modifies: A, BC, DE, HL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_render_draw_one_entity:
	;;Here we check if the entity should only be drawn
	ld c, #e_cmp_platform
	IS_ENTITY_MATCHING_SIGNATURE
	jr z, only_draw

	;;Here we check if is first time rendering the entity, if so we don't make the delete, only assign the prevptr
	ld a, e_prevptr_b2(ix)
	IS_EQUALS 0
	jr z, is_first_time_rendering

	call sys_render_delete_one_entity

	;;If the entity is dead we return after have been deleted
	ld a, (ix)
	IS_ENTITY_GIVEN_TYPE_IX e_type_dead
	ret nz
	
	is_first_time_rendering:
	;;Here we load in the registers B and A the velocity in x and the x position
	ld b, e_vel_x(ix)
	ld a, e_pos_x(ix)

	ld de, #0xC000
	ld c, a
	ld b, e_pos_y(ix)

	;;Here we get the new prevptr
	call cpct_getScreenPtr_asm

	;;Now we asign the new prevptr to the entity
	ld e_prevptr_b1(ix), l
	ld e_prevptr_b2(ix), h

	only_draw:
		ld de, #0xC000
		ld c, e_pos_x(ix)
		ld b, e_pos_y(ix)
		;;Here we get in HL the video memory position for the x (in C) and y (in B) for our entity
		call cpct_getScreenPtr_asm


		ld c, e_width(ix)
		ld b, e_height(ix)
		ex de, hl
		ld l, e_sprite_b1(ix)
		ld h, e_sprite_b2(ix)

		call cpct_drawSprite_asm
		ret
ret

;;====================================================================
;; GENERAL UPDATE FUNCTION
;;====================================================================
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Update the render for all the entities matching with the signature
;; Modifies: A, C, HL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
signature_render = (e_cmp_render)
sys_render_update::
	ld hl, #sys_render_draw_one_entity
	
	ld a, #signature_render
	ld c, a
	;;Now we draw all the entities with draw component
	call man_entity_forall_matching
ret