tiles: tile updates are now pre-computed
authorLukas Krickl <lukas@krickl.dev>
Thu, 13 Nov 2025 04:52:19 +0000 (05:52 +0100)
committerLukas Krickl <lukas@krickl.dev>
Thu, 13 Nov 2025 04:52:19 +0000 (05:52 +0100)
This saves a lot of time during vblank

src/map.s
src/tiles.s
src/wram.s

index 7ec22cb22c592d4c9d2cfc1b707890f23026a818..0b8f5539440c60fa5e321d7b27a17abf33db346c 100644 (file)
--- a/src/map.s
+++ b/src/map.s
@@ -137,57 +137,37 @@ map_tile_banks_load:
        ; draws a tile 
        ; to the screen
        ; inputs:
-       ;   b/c: y/x position
+       ;               update_tile_to_draw/vram: filled with valid ptr and tile data
 map_draw_tile:
-       ; find tile to draw
-       push bc
-       call map_get_tile
-       ld bc, t_tile
-       add hl, bc ; hl = tile gfx
-       pop bc
-       
-       ; load tile into a
-       ld a, [hl] ; a = tile
-       push af ; save tile gfx
+       ; load vram destination
+       ld a, [update_tile_vram]
+       ld h, a
+       ld a, [update_tile_vram+1]
+       ld l, a
 
-       ld hl, SCRN0
-       ld de, MAP_W * 4  ; * 4 because tiles are 8x8 
+       ld de, update_tile_to_draw
        
-       ; skip y loop if b is 0
-       ld a, b
-       cp a, 0
-       jr z, @skip_y REL
-
-@y_loop:
-               add hl, de 
-               dec b
-               jr nz, @y_loop REL
-@skip_y:
-
-       ld d, 0
-       ld a, c
-       add a, a ; * 2 because tiles are 8x8
-       ld e, a
-       add hl, de ; hl = SCRN location
-               
-       pop af 
-
-       ; draw 2x2 tile
+       ; draw 4 tiles
+       ld a, [de]
+       inc de
        ld [hl+], a
-       inc a
+       
+       ld a, [de]
+       inc de
        ld [hl], a
 
+       ; next row
        ; next row 
-       ld de, (MAP_W * 2) - 1
-       add hl, de
-       
-       ; move down one tile row as well
-       add a, 15
+       ld bc, (MAP_W * 2) - 1
+       add hl, bc
 
+       ld a, [de]
+       inc de
        ld [hl+], a
-       inc a
-       ld [hl], a
 
+       ld a, [de]
+       inc de
+       ld [hl], a
        ret
        
        ; gets a tile based on a position
@@ -228,13 +208,67 @@ map_get_tile:
        ; to tile_to_draw[0] to [3]
        ; inputs:
        ;               b/c: y/x position
+       ;       returns:
+       ;               update_tile_to_draw: 4 bytes of new tile data
+       ;               upte_tile_vram: ptr to vram
 map_draw_tile_prep:
+       ; find tile to draw
+       push bc
        call map_get_tile
-       ; hl = tile
+       ld bc, t_tile
+       add hl, bc ; hl = tile gfx
+       pop bc
        
-       ld de, update_tile_to_draw
+       ; load tile into a
+       ld a, [hl] ; a = tile
+       push af ; save tile gfx
+
+       ld hl, SCRN0
+       ld de, MAP_W * 4  ; * 4 because tiles are 8x8 
+       
+       ; skip y loop if b is 0
+       ld a, b
+       cp a, 0
+       jr z, @skip_y REL
+
+@y_loop:
+               add hl, de 
+               dec b
+               jr nz, @y_loop REL
+@skip_y:
+
+       ld d, 0
+       ld a, c
+       add a, a ; * 2 because tiles are 8x8
+       ld e, a
+       add hl, de ; hl = SCRN location
+
+       ; save hl to vram ptr 
+
+       ld a, h
+       ld [update_tile_vram], a
+       ld a, l
+       ld [update_tile_vram+1], a
+       
+       ; hl = tile data buffer
+       ld hl, update_tile_to_draw
+
+       ; a = start tile
+       pop af 
+
+       ; draw 2x2 tile
+       ld [hl+], a
+       inc a
+       ld [hl+], a
+
+       
+       ; move down one tile row as well
+       add a, 15
+
+       ld [hl+], a
+       inc a
+       ld [hl], a
 
-       ; TODO:
        ret
 
 #macro map_draw_row_inc_c
index 996c325d291539a9b7df985fd45108a8cc94790c..af25ea852fcfdfd80397200d9277841a7786479a 100644 (file)
@@ -76,12 +76,8 @@ tile_update_selected:
        ret
        
        ; draws the recently updated tile
-       ; bsed on update_tile y and x
+       ; based on update_tile prep set in previous frame
 tile_update_draw:
-       ld a, [update_tile_y]
-       ld b, a
-       ld a, [update_tile_x]
-       ld c, a
        jp map_draw_tile
 
 
index ec8ce4ad979839a847c598f10a783948cb9d3bb8..f5a454b6b30f127410aa70a779b3544274148806 100644 (file)
@@ -106,3 +106,5 @@ update_tile: .adv 2
        ; that represent the current tile
        ; this gets drawn during the next blank
 update_tile_to_draw: .adv 4
+       ; ptr to vram for the next update tile
+update_tile_vram: .adv 2