;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;; RENDER ENGINE ;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;; INCLUDES ;;;;;;;;;;;                                  
.include "cpctelera.h.s" 
.include "render.h.s"                                                                  
.include "../cpct_globals.h.s"    
.include "../man/man_entity.h.s"   
.include "../man/man_game.h.s"
.include "../assets/assets.h.s"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;::::
;;  VIDEO MODES :   0 -> 160x200 px -> 80x200 Bytes -> 16 colors  ;;
;;                  1 -> 320x200 px -> 80x200 Bytes -> 4  colors  ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;------------------------------------------------
;;      DEFINICION DE VARIABLES Y CONSTANTES
;;------------------------------------------------
str_title:      .asciz "Press any key to Start..."                      ;; STRING TITLE
str_endgame:    .asciz "You Passed!! Press any key to continue"           ;; STRING WHEN FINISHED LEVEL
str_finished:   .asciz "CONGRATS!! Press any key to restart"            ;; STRING WHEN FINISHED LEVEL
str_deadp:      .asciz "Press any key to restart this level"                      ;; STRING WHEN PLAYER DEAD
str_punt_name:  .asciz "Points: "

v_mode_game         = 0     ;; MODO 0
v_mode_title        = 1     ;; MODO 1
pal_size_game       = 16    ;; 16 COLORES
pal_size_title      = 4     ;; 4 COLORES

screen_end          = 0xFFFF;; POS DONDE TERMINA LA MEMORIA DE VIDEO
render_cmps         = e_cmps_render

;;-----------------------------------------
;;  RENDERIZA CUALQUIER ENTIDAD
;;-----------------------------------------
;;  INPUT: << IX A LA ENTIDAD A RENDERIZAR
;;-----------------------------------------
;;  MODIFIES : AF, BC, DE , HL
;;-----------------------------------------
render_entity::

    ;; BORRADO DE LA ULTIMA POSICION EN DONDE ESTABA LA ENTIDAD

    ;;ld d, e_videoPnter_h(ix)
    ;;ld e, e_videoPnter_l(ix)
;;
    ;;xor a                       ;; SE PONE EL COLOR DE FONDO
    ;;
    ;;ld c, e_width(ix)
    ;;ld b, e_height(ix)
;;
    ;;call cpct_drawSolidBox_asm

    ;;========================================================

    ld de, #CPCT_VMEM_START_ASM
    ld b, e_posy(ix)                ;; y en Bytes (0-199)
    ld c, e_posx(ix)                ;; x en Bytes (0-79)

    call cpct_getScreenPtr_asm      ;;  INPUTS:     "DE" (START OF SCREEN)
                                    ;;              "BC" (x,y)
                                    ;;  RETURNS: pointer the byte xy en "HL"
                  
    ex  de,  hl                     ;;  ahora el puntero es "DE"

    ld  b,   e_height(ix)
    ld  c,   e_width(ix)
    ld  l,   e_sprite_ptr_l(ix)
    ld  h,   e_sprite_ptr_h(ix)
    call cpct_drawSprite_asm        ;; INPUTS:  HL => Source Sprite Ptr
                                    ;;          DE => Destination vmem Ptr  
                                    ;;          C  => Width (Bytes)
                                    ;;          B  => Height (Bytes)
ret

;;[HL] << PUNTERO AL STRING A RENDERIZAR
render_points::

    push ix
    push iy

    ;; MARCADOR DE PUNTOS EN LA PARTE SUPERIOR

    ld l, #3      ;;  Color elementos primer plano
    ld h, #11        ;;  Color elementos background

    call cpct_setDrawCharM0_asm     ;; inputs E,D

    x = 32
    y = 0

    call man_game_getPlayerPuntuation_render
    ld__iy_hl           ;; pointer to the string
    cpctm_screenPtr_asm hl, CPCT_VMEM_START_ASM, x, y

    call cpct_drawStringM0_asm      ;;  inputs IY, HL

    pop iy
    pop ix
ret

;;---------------------------------
;;RENDERIZA LA TITLE SCREEN
;;---------------------------------
render_title::

    ;;  INPUTS PANTALLA TITULO
    ;;--------------------------------------------------------------------------
    ;;  IMG?: "1" if has compressed image, "0" if it doesn't
    ;;  INPUTS:<<      IMG,   _decomp_img_end,    _string,   x,  y,  c_fg, c_bg 
    ;;--------------------------------------------------------------------------
    SetScreenImageMode1   1, _my_title_screen_end, str_title, 17, 185,  1,  0

ret

;;--------------------------------------
;;RENDERIZA LA PANTALLA DE VICTORIA
;;--------------------------------------
render_endgame::

    SetScreenImageMode1   0, 0xFFFF , str_endgame,  3, 40, 3, 0

ret

;;--------------------------------------
;;RENDERIZA LA PANTALLA DE MUERTE
;;--------------------------------------
render_deadPlayer::

    SetScreenImageMode1   1, _my_dead_screen_end , str_deadp,    5, 175, 3, 0

ret

;;--------------------------------------
;;RENDERIZA LA PANTALLA DE GAME FINISHED
;;--------------------------------------
render_finished::

    SetScreenImageMode1   1, _my_winner_screen_end , str_finished, 5, 175, 3, 0

ret

;; ===========================
;; RENDERIZA EL TILEMAP
;; [A] << NIVEL A RENDERIZAR
;; ===========================
render_tilemap:

    ;; CARGAMOS EL TILESET

        ld bc, #0x1914
        ld de, #0x0014

        ld hl, #_tiles_00

        call cpct_etm_setDrawTilemap4x8_ag_asm
    ;; ========================================

    ;; CARGAMOS EL NIVEL A RENDERIZAR

        call man_game_getActualLevel

        ld c, #1
        cp c
        
    jr z, render_lvl1

        ld c, #2
        cp c

    jr z, render_lvl2

            ld c, #3
        cp c
        
    jr z, render_lvl3

        ld c, #4
        cp c

    jr z, render_lvl4

        ld c, #5
        cp c
        
    jr z, render_lvl5

        ld c, #6
        cp c
        
    jr z, render_lvl6

        ld c, #7
        cp c
        
    jr z, render_lvl7

        ld c, #8
        cp c
        
    jr z, render_lvl8

        ld c, #9
        cp c

    jr z, render_lvl9




    render_lvl1:
        ;; RENDER LVL 1

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_01

        call cpct_etm_drawTilemap4x8_ag_asm

    ret

    render_lvl2:
        ;; RENDER LVL 2

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_02

        call cpct_etm_drawTilemap4x8_ag_asm
    ret

    ;; RENDER LVL 3

    render_lvl3:

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_03

        call cpct_etm_drawTilemap4x8_ag_asm
    ret 

    ;; RENDER LVL 4

    render_lvl4:

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_04
        call cpct_etm_drawTilemap4x8_ag_asm

    ret

    ;; RENDER LVL 5
    render_lvl5:

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_05

        call cpct_etm_drawTilemap4x8_ag_asm

    ret

        ;; RENDER LVL 5
    render_lvl6:

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_06

        call cpct_etm_drawTilemap4x8_ag_asm

    ret

        ;; RENDER LVL 5
    render_lvl7:

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_07

        call cpct_etm_drawTilemap4x8_ag_asm

    ret

        ;; RENDER LVL 5
    render_lvl8:

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_08

        call cpct_etm_drawTilemap4x8_ag_asm

    ret


        ;; RENDER LVL 5
    render_lvl9:

        ld hl, #CPCT_VMEM_START_ASM
        ld de, #_tilemap_09

        call cpct_etm_drawTilemap4x8_ag_asm

    ret

;; ====================================
;; RENDER HUD
;; ====================================
render_hud:

    ;; MARCADOR DE PUNTOS EN LA PARTE SUPERIOR

        ;; Points:

        ld l, #3      ;;  Color elementos primer plano
        ld h, #11        ;;  Color elementos background

        call cpct_setDrawCharM0_asm     ;; inputs E,D
        x = 4
        y = 0
        ld iy, #str_punt_name            ;; pointer to the string
        cpctm_screenPtr_asm hl, CPCT_VMEM_START_ASM, x, y
        call cpct_drawStringM0_asm      ;;  inputs IY, HL

            ld l, #3       ;;  Color elementos primer plano
            ld h, #11        ;;  Color elementos background

            call cpct_setDrawCharM0_asm     ;; inputs E,D
            x = 32
            y = 0

            call man_game_getPlayerPuntuation_render
            ld__iy_hl            ;; pointer to the string
            cpctm_screenPtr_asm hl, CPCT_VMEM_START_ASM, x, y

        call cpct_drawStringM0_asm      ;;  inputs IY, HL


    ;; MARCADOR DE NIVEL EN LA PARTE SUPERIOR

        ld l, #3       ;;  Color elementos primer plano
        ld h, #11        ;;  Color elementos background

        call cpct_setDrawCharM0_asm     ;; inputs E,D
        x = 56
        y = 0

        call man_game_getActualLevel_render
        ld__iy_hl            ;; pointer to the string
        cpctm_screenPtr_asm hl, CPCT_VMEM_START_ASM, x, y

        call cpct_drawStringM0_asm      ;;  inputs IY, HL

ret

;;---------------------------------
;;RENDERIZA LA INGAME SCREEN
;;---------------------------------
render_game::

    ;;push af

    ;;LIMPIA LA PANTALLA ANTERIOR DE TITULO
    cpctm_clearScreen_asm 0
    
    ld c, #v_mode_game
    call cpct_setVideoMode_asm

    ld hl, #_g_palette
    ld de, #pal_size_game
    call cpct_setPalette_asm

    cpctm_setBorder_asm HW_BLUE

    ;; RENDERIZA EL TILEMAP

    ;;pop af
    call render_tilemap

    ;; RENDERIZA EL HUD

    call render_hud

ret


render_cleanEntity::

    ;; BORRADO DE LA ULTIMA POSICION EN DONDE ESTABA LA ENTIDAD AL DESTRUIRSE

    ld de, #CPCT_VMEM_START_ASM
    ld b, e_posy(ix)                
    ld c, e_posx(ix)                

    call cpct_getScreenPtr_asm      
                                    
    ex  de,  hl                     

    xor a                       ;; SE PONE EL COLOR DE FONDO
    
    ld c, e_width(ix)
    ld b, e_height(ix)

    call cpct_drawSolidBox_asm


ret

;;----------------------------------
;;ACTUALIZA LOS ELEMENTOS DEL JUEGO
;;----------------------------------
render_update::
    
    ld de, #e_cmps_destructible
    ld hl, #render_cleanEntity
    call man_entity_for_all_matching

    ld de, #render_cmps
    ld hl, #render_entity
    call man_entity_for_all_matching


    call render_points
ret
