.include "cpctelera_functions.h.s"
.include "man/entity_manager.h.s"
.include "cpctelera.h.s"
.include "assets/assets.h.s"
.include "tilemaps/map1.h.s"
.include "man/game_manager.h.s"

.globl _player_lives
.globl score

m_front_buffer: .db 0xC0
m_back_buffer: .db 0x80

string:     .asciz "PRESS START";
end:        .asciz "GAME OVER"
restart:    .asciz "PRESS SPACE KEY TO START AGAIN"
lives:      .asciz "x3"
score_str:  .asciz "0000 points"

waitKeyPressed:
      push  hl
      call  cpct_scanKeyboard_f_asm
      pop   hl
      push  hl
      call  cpct_isKeyPressed_asm
      pop   hl
      jr    nz, waitKeyPressed

loop2:
      push  hl
      call  cpct_scanKeyboard_f_asm
      pop   hl
      push  hl
      call  cpct_isKeyPressed_asm
      pop   hl
      jr    z, loop2
      ret


render_system_eraseAll::
    ld b, a
    inc b
    
    _loop_update:
        ; Comprueba num entidades
        dec b
        ret z


        ld  hl, #0x0000
        ;;call    entity_manager_getEntityArray
        ld  e_last_sprite_pointer_position_l(ix), l
        ld  e_last_sprite_pointer_position_h(ix), h
        

        _next_e:
            ld      de, #sizeof_e
            add     ix, de
            jr      _loop_update

ret

render_system_end::
    cpctm_clearScreen_asm 0
    ld    b, #0        ;; D = Background PEN (0)
    ld    c, #14       ;; E = Foreground PEN (3)
    call cpct_drawCharM0_asm   ;; Set draw char colours
    ;; Calculate a video-memory location for printing a string
    ld   de, #0xC000 ;; DE = Pointer to start of the screen
    ld    b, #0x64                  ;; B = y coordinate (24 = 0x18)
    ld    c, #21                    ;; 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, #end   ;; IY = Pointer to the string 
    call cpct_drawStringM0_asm  ;; Draw the string

    ; Imprimir puntuacion
    ld   de, #0xC000 ;; DE = Pointer to start of the screen
    ld    b, #0x75                  ;; B = y coordinate (24 = 0x18)
    ld    c, #21                    ;; C = x coordinate (16 = 0x10)
    call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL
    push hl
    ; Cargar puntuacion en la string (convertir de numero a ascii)
    ld hl,(score)
    ld   iy, #score_str   ;; IY = Pointer to the string
    ; 1r digito
    ld a,h
    rra
    rra
    rra
    rra
    and #0x0F
    add #48
    ld (iy),a
    ; 2o digito
    ld a,h
    and #0x0F
    add #48
    ld 1(iy),a
    ; 3r digito
    ld a,l
    rra
    rra
    rra
    rra
    and #0x0F
    add #48
    ld 2(iy),a
    ; 4o digito
    ld a,l
    and #0x0F
    add #48
    ld 3(iy),a

    ; Print score
    pop hl
    call cpct_drawStringM0_asm  ;; Draw the string
    
    ; Esperar pulsación usuario
    ld    hl, #Key_Space        
    call waitKeyPressed
    cpctm_clearScreen_asm 0

    ;; Limpiamos la memoria de video
    ld      hl, #0xC000
    ld      de, #0xC001
    ld      bc, #0x4000-1
    ld      (hl), #0
    ldir

    ;call    render_system_tilemap

    
    ;; Borramos todas las entidades que quedasen en pantalla
    call    entity_manager_getEntityArray
    call    render_system_eraseAll

    call    entity_manager_getEntityArray
    call    game_manager_level2

    call    entity_manager_getEntityArray
    jp      game_manager_setPlayerPosition
     
    ;ld a,#3
    ;ld (_player_lives),a
    ;jp    render_system_numlives
    

render_system_menu::
    
    ret

;; Poner modo de video, seleccionar la paleta, etc
render_system_init::

    ;; Ponemos el modo de video a cero
    ld      c, #0
    call    cpct_setVideoMode_asm
    ;; Establecemos la paleta
    ld      hl, #_pal_main
    ld      de, #16
    call    cpct_setPalette_asm
    ;; Ponemos el color del borde 
    cpctm_setBorder_asm HW_WHITE

    ld    b, #0         ;; D = Background PEN (0)
    ld    c, #3     ;; E = Foreground PEN (3)
    call cpct_drawCharM0_asm   ;; Set draw char colours
    ;; Calculate a video-memory location for printing a string
    ld   de, #0xC000 ;; DE = Pointer to start of the screen
    ld    b, #0xA4                  ;; B = y coordinate (24 = 0x18)
    ld    c, #16                  ;; 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
    
    ld    hl, #Key_Space
    call waitKeyPressed
    cpctm_clearScreen_asm 0

    ; Pintamos numero de vidas
    jp render_system_numlives

    ;; Ponemos el background
    ;jp   render_system_tilemap

render_system_tilemap:
    ;; (1B C) width	Width in tiles of the view window to be drawn
    ;; (1B B) height	Height in tiles of the view window to be drawn
    ;; (2B DE) tilemapWidth	Width in tiles of the complete tilemap
    ;; (2B HL) tileset	Pointer to the start of the tileset definition (list of 32-byte tiles).
    ld      c, #_tilemap_01_W
    ld      b, #_tilemap_01_H
    ld      de, #_tilemap_01_W
    ld      hl, #_tiles_00
    call    cpct_etm_setDrawTilemap4x8_ag_asm
    
    ;; (2B HL) memory	Video memory location where to draw the tilemap (character & 4-byte aligned)
    ;; (2B DE) tilemap	Pointer to the upper-left tile of the view to be drawn of the tilemap
    
    ld      hl, #0xC000
    
    ;;ld      a, (m_back_buffer) ;; DE = Back m_back_buffer
    ;;ld      h, a
    ;;ld      l, #0
    
    ld      de, #_tilemap_01
    jp    cpct_etm_drawTilemap4x8_ag_asm	

render_system_newScene::
    call render_system_switchBuffers
    jp render_system_clearBackBuffer

render_system_clearBackBuffer::
    ld      a, (m_back_buffer)
    ld      h, a
    ld      l, #0
    ld      d, a
    ld      e, #1
    ld      bc, #0x4000 - 1
    ldir
    
    ret

render_system_switchBuffers::
    ld      a, (m_back_buffer)
    ld      b, a
    ld      a, (m_front_buffer)
    ld      (m_back_buffer), a
    ld      a, b
    ld      (m_front_buffer), a

    srl     b
    srl     b
    ld      l, b
    jp    cpct_setVideoMemoryPage_asm
    
    ;; CUANDO TIENES UNA FUNCION CON CALL Y LA SIGUIENTE
    ;; INSTRUCCION RET PUEDES QUITAR EL RET Y SUSTITUIR EL
    ;; CALL POR JP

render_system_update::

        ;;Pintamos el fondo
        ld      e, e_last_sprite_pointer_position_l(ix)
        ld      d, e_last_sprite_pointer_position_h(ix)
        ;;xor     a
        ;;ld      h, e_sprite_pointer_h(ix)
        ;;ld      l, e_sprite_pointer_l(ix) 
        ld      a, #0
        ld      b, e_h(ix)
        ld      c, e_w(ix)
        push    bc
        call    cpct_drawSolidBox_asm

        ;; Obtenemos un puntero a la posición de memoria de video donde
        ;; vamos a dibujar la entidad
        ld      de, #0xC000
        ld      c, e_x(ix)
        ld      b, e_y(ix)
        call    cpct_getScreenPtr_asm

        ld      e_last_sprite_pointer_position_l(ix), l
        ld      e_last_sprite_pointer_position_h(ix), h

        
        ;; Guardamos en HL el puntero a la dirección de memoria donde empieza el sprite
        ex      de, hl
        ld      l, e_sprite_pointer_l(ix) 
        ld      h, e_sprite_pointer_h(ix)
        pop     bc
        jp    cpct_drawSprite_asm


;;================================================
;; Imprime el numero de vidas restantes
;; Input:
;;      
;; Destroy:
;;      BC, AF, DE, HL, IX, IY
;; Return:
;;      
;;================================================
render_system_numlives::

    ;; Imprimir numero de vidas
    ld   de, #0xC000 ;; DE = Pointer to start of the screen
    ld    b, #17                 ;; B = y coordinate (24 = 0x18)
    ld    c, #60                  ;; C = x coordinate (16 = 0x10)
    call cpct_getScreenPtr_asm    ;; Calculate video memory location and return it in HL
    ld   iy, #lives    ;; IY = Pointer to the string 
    ld   a, (_player_lives)
    add #48
    ld   1(iy), a ; ponemos el numero de vidas
    jp cpct_drawStringM0_asm  ;; Draw the string


;;change_screen::
;;f_change_screen = . + 1
  ;;  jp   change_screen_to_C400

;;change_screen_to_C400:
  ;;  ld   de, #0x0C32
  ;;  call set_crtc

  ;;  ld   a, #0xC0
  ;;  ld   (_back_buffer), a

  ;;  ld   hl, #change_screen_to_C000
  ;;  ld   (f_change_screen), hl
    
  ;;  ret


;;change_screen_to_C000:
  ;;  ld   de, #0x0C30
  ;;  call set_crtc

  ;;  ld   a, #0xC4
  ;;  ld   (_back_buffer), a

  ;;  ld   hl, #change_screen_to_C400
  ;;  ld   (f_change_screen), hl
    
  ;;  ret
