From 04fced3a60568ac3a862d90bcd6a3f2c5aa45dfc Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Thu, 13 Nov 2025 05:52:19 +0100 Subject: [PATCH] tiles: tile updates are now pre-computed This saves a lot of time during vblank --- src/map.s | 122 +++++++++++++++++++++++++++++++++------------------- src/tiles.s | 6 +-- src/wram.s | 2 + 3 files changed, 81 insertions(+), 49 deletions(-) diff --git a/src/map.s b/src/map.s index 7ec22cb..0b8f553 100644 --- 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 diff --git a/src/tiles.s b/src/tiles.s index 996c325..af25ea8 100644 --- a/src/tiles.s +++ b/src/tiles.s @@ -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 diff --git a/src/wram.s b/src/wram.s index ec8ce4a..f5a454b 100644 --- a/src/wram.s +++ b/src/wram.s @@ -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 -- 2.30.2