;;-----------------------------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 "0000"
string2: .asciz "Space to Start"
string3: .asciz "GAME OVER"
string4: .asciz "Space to Restart"

man_screen_init::

    ld hl, #_inicio_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm


    ;;;; Set up draw char colours before calling draw string
    ;;ld    h, #0         ;; D = Background PEN (0)
    ;;ld    l, #5      ;; 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, #48                  ;; B = y coordinate (24 = 0x18)
    ;;ld    c, #12                  ;; C = x coordinate (16 = 0x10)
;;
    ;;call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL
;;
    ;;;; 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
;;
    ;;;; Calculate a video-memory location for printing a string
    ;;ld   de, #CPCT_VMEM_START_ASM ;; DE = Pointer to start of the screen
    ;;ld    b, #82                  ;; B = y coordinate (24 = 0x18)
    ;;ld    c, #12                  ;; C = x coordinate (16 = 0x10)
;;
    ;;call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL
;;
    ;;;; 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
;;
    screen_loop:

    ld hl, #Key_Space
    call cpct_isKeyPressed_asm
    jr z, screen_loop

    ret

;;man_screen_controls::
;;
;;    ld hl, #_controles_end
;;    ld de, #0xFFFF
;;    call cpct_zx7b_decrunch_s_asm
;;
;;    controls_loop:
;;    call cpct_scanKeyboard_asm
;;    ld hl, #Key_Space
;;    call cpct_isKeyPressed_asm
;;    jr z, controls_loop
;;    
;;    ret

man_screen_gameOver::

    ld hl, #_gameover_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    ;;;; Set up draw char colours before calling draw string
    ld    h, #0         ;; D = Background PEN (0)
    ld    l, #5      ;; 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, #130                  ;; B = y coordinate (24 = 0x18)
    ;ld    c, #30                  ;; C = x coordinate (16 = 0x10)
;;;
    ;call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL
    cpctm_screenPtr_asm hl, #0xC000, #34, #130
;;
    ;;;; 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    

    call man_screen_final_score

    screen_loop_over:

        ld hl, #Key_Space
        call cpct_isKeyPressed_asm
        jr z, screen_loop_over
        call reset

    ret

reset:
    call man_game_reset
    ret 

;;man_screen_credits::
;;    ld hl, #_credits_end
;;    ld de, #0xFFFF
;;    call cpct_zx7b_decrunch_s_asm
;;
;;    ;;;; Set up draw char colours before calling draw string
;;    ld    h, #0         ;; D = Background PEN (0)
;;    ld    l, #5      ;; 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, #130                  ;; B = y coordinate (24 = 0x18)
;;    ;ld    c, #30                  ;; C = x coordinate (16 = 0x10)
;;;;;
;;    ;call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL
;;    ;cpctm_screenPtr_asm hl, #0xC000, #30, #130
;;    ld hl, #0xD51E
;;;;
;;    ;;;; 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 
;;
;;    credits_loop:
;;    call cpct_scanKeyboard_asm
;;    ld hl, #Key_Space
;;    call cpct_isKeyPressed_asm
;;    jr z, credits_loop
;;    call man_game_reset
;;    
;;ret

man_screen_final_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, #0        ;; D = Background PEN (0)
    ld    l, #5

    call cpct_setDrawCharM0_asm   ;; Set draw char colours

    ;;
    ;;Dibujamos Unidades
    ;;

    cpctm_screenPtr_asm, hl, #0xC000, #44, #130
    ;;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, #40, #130

    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, #36, #130

    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, #32, #130

    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 