;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of CPCtelera: An Amstrad CPC Game Engine 
;;  Copyright (C) 2021 Daniel José García Evans, Iván Alejandro Ontano Herrera,
;;  Sergio Izquierdo Pintado (PyroClastic Software)
;;
;;  Special Thanks to Ennio Fernando Velásquez Berlingeri (PyroBombastic)
;;
;;  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/>.

;; 
;; Screen Manager
;; 

.include "../globals.h.s"


string: .asciz "SCORE";
string2: .asciz "0000"
string3: .asciz "1111"

man_ui_init::
    ld a, (lives)   ;;cargo las vidas
    and a
    jr z, draw_score_text   ;;si no hay mas vidas no sigue pintando

    push af
    ;;------------------------------
    ;;Primer Corazon ^^
    ;;------------------------------

    ;;ld de, #0xC000
    ;;ld c, #2 ;;pixel x
    ;;ld b, #1 ;;pixel y
    ;;call cpct_getScreenPtr_asm

    ;;cpctm_screenPtr_asm, hl, #0xC000, #2, #1 ;; MACRO DEL SCREEN PTR
    ld hl, #0xC802                             ;; RESULTADO DIRECTO

    ex de, hl
    ld hl, #_char_heart_0
    ld c, #4   ;;ancho = 8 pixeles (4 bytes)
    ld b, #12   ;;alto  = 16 pixeles = 16 bytes
    call cpct_drawSprite_asm

    pop af
    dec a
    jr z, draw_score_text   ;;si no hay mas vidas no sigue pintando

    push af
    ;;----------------------------
    ;;Segundo Corazon ^^
    ;;----------------------------

    ;;ld de, #0xC000
    ;;ld c, #7 ;;pixel x
    ;;ld b, #1 ;;pixel y
    ;;call cpct_getScreenPtr_asm

    ;;cpctm_screenPtr_asm, hl, #0xC000, #7, #1
    ld hl, #0xC807

    ex de, hl
    ld hl, #_char_heart_1
    ld c, #4   ;;ancho = 8 pixeles (4 bytes)
    ld b, #12   ;;alto  = 16 pixeles = 16 bytes
    call cpct_drawSprite_asm

    pop af
    dec a
    jr z, draw_score_text   ;;si no hay mas vidas no sigue pintando

    push af
    ;;--------------------------
    ;;Tercer Corazon ^^
    ;;--------------------------

    ;;ld de, #0xC000
    ;;ld c, #12 ;;pixel x
    ;;ld b, #1 ;;pixel y
    ;;call cpct_getScreenPtr_asm

    ;;cpctm_screenPtr_asm, hl, #0xC000, #12, #1
    ld hl, #0xC80C

    ex de, hl
    ld hl, #_char_heart_2
    ld c, #4   ;;ancho = 8 pixeles (4 bytes)
    ld b, #12   ;;alto  = 16 pixeles = 16 bytes
    call cpct_drawSprite_asm

    pop af
    dec a
    jr z, draw_score_text   ;;si no hay mas vidas no sigue pintando

    push af
    ;;---------------------------
    ;;Cuarto Corazon ^^
    ;;---------------------------

    ;;ld de, #0xC000
    ;;ld c, #17 ;;pixel x
    ;;ld b, #1 ;;pixel y
    ;;call cpct_getScreenPtr_asm

    ;;cpctm_screenPtr_asm, hl, #0xC000, #17, #1
    ld hl, #0xC811

    ex de, hl
    ld hl, #_char_heart_3
    ld c, #4   ;;ancho = 8 pixeles (4 bytes)
    ld b, #12   ;;alto  = 16 pixeles = 16 bytes
    call cpct_drawSprite_asm

    pop af
    dec a
    jr z, draw_score_text   ;;si no hay mas vidas no sigue pintando

    ;;push af no hay que dejar un push inutil
    ;;------------------------
    ;;Quinto Corazon ^^
    ;;------------------------

    ;;ld de, #0xC000
    ;;ld c, #22 ;;pixel x
    ;;ld b, #1 ;;pixel y
    ;;call cpct_getScreenPtr_asm

    ;;cpctm_screenPtr_asm, hl, #0xC000, #22, #1
    ld hl, #0xC816

    ex de, hl
    ld hl, #_char_heart_4
    ld c, #4   ;;ancho = 8 pixeles (4 bytes)
    ld b, #12   ;;alto  = 16 pixeles = 16 bytes
    call cpct_drawSprite_asm

    ;;---------------------------------;;
    ;;  SCORE TEXT                     ;;
    ;;---------------------------------;;
    draw_score_text:

    ld    h, #15         ;; D = Background PEN (0)
    ld    l, #0         ;; E = Foreground PEN (3)

    call cpct_setDrawCharM0_asm   ;; Set draw char colours

    ;; Calculate a video-memory location for printing a string
    ;;ld   de, #CPCT_VMEM_START_ASM ;; DE = Pointer to start of the screen
    ;;ld    b, #4                  ;; B = y coordinate (24 = 0x18)
    ;;ld    c, #40                  ;; C = x coordinate (16 = 0x10)
    ;;call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL

    ;;cpctm_screenPtr_asm, hl, #0xC000, #40, #4
    ld hl, #0xE028

    ;; Print the string in video memory
    ;; HL already points to video memory, as it is the return
    ;; value from cpct_getScreenPtr_asm
    ld   iy, #string    ;; IY = Pointer to the string 

    call cpct_drawStringM0_asm  ;; Draw the string

    ;;---------------------------
    ;;  NUMERO DE PUNTOS
    ;;---------------------------
    ld    h, #15         ;; D = Background PEN (0)
    ld    l, #0

    call cpct_setDrawCharM0_asm   ;; Set draw char colours

    ;; Calculate a video-memory location for printing a string
    ;;ld   de, #CPCT_VMEM_START_ASM ;; DE = Pointer to start of the screen
    ;;ld    b, #4                  ;; B = y coordinate (24 = 0x18)
    ;;ld    c, #40                  ;; C = x coordinate (16 = 0x10)
    ;;call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL

    cpctm_screenPtr_asm, hl, #0xC000, #62, #4

    ;; Print the string in video memory
    ;; HL already points to video memory, as it is the return
    ;; value from cpct_getScreenPtr_asm
    ;;ld   iy, #string2    ;; IY = Pointer to the string 
;;
    ;;call cpct_drawStringM0_asm  ;; Draw the string

    ret

;;Recibe por HL la puntuacion
man_ui_update_score::
    ld hl, (score)

    ld b, h ;; B = 10
    ld c, l ;; C = 25

    push bc ;; Nos guardamos la puntuacion porque sera destruida

    ld    h, #15         ;; D = Background PEN (0)
    ld    l, #0

    call cpct_setDrawCharM0_asm   ;; Set draw char colours

    ;;
    ;;Dibujamos Unidades
    ;;

    cpctm_screenPtr_asm, hl, #0xC000, #74, #4
    ;;Se carga AF, BC, DE y HL

    pop bc

    ld a, c ;; A = 25     0001 0000
    and #0x0F
    add #48
    ld e, a ;; E = 5

    push bc

    call cpct_drawCharM0_asm

    ;;
    ;;Dibujamos Decenas
    ;;

    cpctm_screenPtr_asm, hl, #0xC000, #70, #4

    pop bc
    
    ld a, c ;; A = 25     0001 0000
    rra                 ;;0000 1000
    rra                 ;;0000 0100
    rra                 ;;0000 0010
    rra                 ;;0000 0001
    and #0x0F
    add #48
    ld e, a ;; E = 2

    push bc

    call cpct_drawCharM0_asm

    ;;
    ;;Dibujamos Centenas
    ;;

    cpctm_screenPtr_asm, hl, #0xC000, #66, #4

    pop bc

    ld a, b ;; A = 10     0001 0000
    and #0x0F
    add #48
    ld e, a ;; E = 0

    push bc

    call cpct_drawCharM0_asm

    ;;
    ;;Dibujamos Millares
    ;;

    cpctm_screenPtr_asm, hl, #0xC000, #62, #4

    pop bc
    
    ld a, b ;; A = 10     0001 0000
    rra                 ;;0000 1000
    rra                 ;;0000 0100
    rra                 ;;0000 0010
    rra                 ;;0000 0001
    and #0x0F
    add #48
    ld e, a ;; E = 1

    call cpct_drawCharM0_asm

ret 

;;recibe en A el numero de vidas actuales
man_ui_delete_heart::
    ld b, a
    cp #4
    jr z, delete_fifth_heart    
        cp #3
        jr z, delete_fourth_heart 
            cp #2
            jr z, delete_third_heart
                cp #1
                jr z, delete_second_heart
                    ;;delete_first_heart
                    ;;Primer Corazon 
                    ;;ld de, #0xC000
                    ;;ld c, #2 ;;pixel x
                    ;;ld b, #1 ;;pixel y
                    ;;call cpct_getScreenPtr_asm

                    ;;cpctm_screenPtr_asm, hl, #0xC000, #2, #1
                    ld hl, #0xC802  

                    ex de, hl
                    ld a, #0xFF
                    ld c, #4   ;;ancho = 8 pixeles (4 bytes)
                    ld b, #12   ;;alto  = 16 pixeles = 16 bytes
                    call cpct_drawSolidBox_asm

                    ret
                delete_second_heart:
                ;;Segundo Corazon
                ;;ld de, #0xC000
                ;;ld c, #7 ;;pixel x
                ;;ld b, #1 ;;pixel y
                ;;call cpct_getScreenPtr_asm

                ;;cpctm_screenPtr_asm, hl, #0xC000, #7, #1
                ld hl, #0xC807  

                ex de, hl
                ld a, #0xFF
                ld c, #4   ;;ancho = 8 pixeles (4 bytes)
                ld b, #12   ;;alto  = 16 pixeles = 16 bytes
                call cpct_drawSolidBox_asm

                ret
            delete_third_heart:
            ;;Tercer Corazon
            ;;ld de, #0xC000
            ;;ld c, #12 ;;pixel x
            ;;ld b, #1 ;;pixel y
            ;;call cpct_getScreenPtr_asm

            ;;cpctm_screenPtr_asm, hl, #0xC000, #12, #1
            ld hl, #0xC80C  

            ex de, hl
            ld a, #0xFF
            ld c, #4   ;;ancho = 8 pixeles (4 bytes)
            ld b, #12   ;;alto  = 16 pixeles = 16 bytes
            call cpct_drawSolidBox_asm
            ret    
        delete_fourth_heart:
        ;;Cuarto Corazon
        ;;ld de, #0xC000
        ;;ld c, #17 ;;pixel x
        ;;ld b, #1 ;;pixel y
        ;;call cpct_getScreenPtr_asm

        ;;cpctm_screenPtr_asm, hl, #0xC000, #17, #1
        ld hl, #0xC811  

        ex de, hl
        ld a, #0xFF
        ld c, #4   ;;ancho = 8 pixeles (4 bytes)
        ld b, #12   ;;alto  = 16 pixeles = 16 bytes
        call cpct_drawSolidBox_asm
        ret 
    delete_fifth_heart:
    ;;Quinto Corazon
    ;;ld de, #0xC000
    ;;ld c, #22 ;;pixel x
    ;;ld b, #1 ;;pixel y
    ;;call cpct_getScreenPtr_asm

    ;;cpctm_screenPtr_asm, hl, #0xC000, #22, #1
    ld hl, #0xC816  

    ex de, hl
    ld a, #0xFF
    ld c, #4   ;;ancho = 8 pixeles (4 bytes)
    ld b, #12   ;;alto  = 16 pixeles = 16 bytes
    call cpct_drawSolidBox_asm    
    ret