From: Lukas Krickl Date: Mon, 14 Apr 2025 13:41:55 +0000 (+0200) Subject: bg: Added bg update queue X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=27737a2471b382fe02565ead2a5d076c68d9e4cb;p=gbrg%2F.git bg: Added bg update queue This queue can store up to 32 update requests for tiles. --- diff --git a/src/buildings.s b/src/buildings.s index 3ad7d43..92fca86 100644 --- a/src/buildings.s +++ b/src/buildings.s @@ -14,6 +14,11 @@ build_warehouse: build_road: call cursor_get_cell + + call cursor_get_tile + ld a, UI_TILE_ROAD + call bg_update_queue_push + ldnull bc ret diff --git a/src/defs.s b/src/defs.s index a8e3efc..531c488 100644 --- a/src/defs.s +++ b/src/defs.s @@ -11,6 +11,7 @@ #define TILE_SIZE 8 #define MAP_W 32 +.def int MAP_W_DEF = MAP_W #define MAP_H 24 #define MAP_SIZE (MAP_W * MAP_H) @@ -111,3 +112,11 @@ .de BT_WAREHOUSE, 1 .de BT_FARM, 1 .de BT_LUMBER, 1 + +#define BGE_MAX 32 + + ; bg update entry +.se 0 +.de bge_tile_ptr, 2 +.de bge_new_tile, 1 +.de bge_size, 0 diff --git a/src/macros.inc b/src/macros.inc index 4f82d8a..b4b6ee1 100644 --- a/src/macros.inc +++ b/src/macros.inc @@ -156,3 +156,39 @@ .db 0 #endmacro + ; generic version of cursor get + ; math + ; inputs: + ; $1: stating address + ; $1: row size in bytes + ; $2: col size in bytes +#macro cursor_get_at_generic + ; find the cell the cursor has selected + ld hl, $1 + + ; move y rows down + ld de, $2 + ld a, [cursor_y] + div8 a + cp a, 0 + jr z, @not_y_loop REL +@y_loop: + add hl, de + dec a + cp a, 0 + jr nz, @y_loop REL +@not_y_loop: + + ; move x cells + ld de, $3 + ld a, [cursor_x] + div8 a + cp a, 0 + jr z, @not_x_loop REL +@x_loop: + add hl, de + dec a + cp a, 0 + jr nz, @x_loop REL +@not_x_loop: +#endmacro diff --git a/src/player.s b/src/player.s index adafaff..d28623f 100644 --- a/src/player.s +++ b/src/player.s @@ -219,33 +219,16 @@ try_abort_move_at: ; returns: ; hl: cell ptr cursor_get_cell: - ; find the cell the cursor has selected - ld hl, state_cells + cursor_get_at_generic state_cells, MAP_W_DEF * c_size, c_size - ; move y rows down - ld de, MAP_W * c_size - ld a, [cursor_y] - div8 a - cp a, 0 - jr z, @not_y_loop REL -@y_loop: - add hl, de - dec a - cp a, 0 - jr nz, @y_loop REL -@not_y_loop: - - ; move x cells - ld de, c_size - ld a, [cursor_x] - div8 a - cp a, 0 - jr z, @not_x_loop REL -@x_loop: - add hl, de - dec a - cp a, 0 - jr nz, @x_loop REL -@not_x_loop: + ret + + ; gets the current tile in SCRN0 + ; based on cursor's location + ; cursor_y/x + ; retunrs: + ; hl: tile position +cursor_get_tile: + cursor_get_at_generic SCRN0, MAP_W_DEF, 1 ret diff --git a/src/state.s b/src/state.s index 5ead5ca..914e081 100644 --- a/src/state.s +++ b/src/state.s @@ -107,7 +107,7 @@ st_update_game_over: st_build_warehouse: st_def 0x00, build_warehouse, st_null st_build_road: - st_def 0x00, build_road, st_null + st_def 0x00, build_road, st_build_road st_build_farm: st_def 0x00, build_farm, st_null st_build_lumber: diff --git a/src/update.s b/src/update.s index e9be5fc..294245a 100644 --- a/src/update.s +++ b/src/update.s @@ -10,7 +10,7 @@ update_game: call st_update ; call player_update - call sim_update + ; call sim_update ldnull bc ret @@ -44,4 +44,28 @@ bg_update_queue_process: ; hl: ptr to tile ; a: tile data bg_update_queue_push: + push hl + pop bc ; move hl to bc + push af + ld hl, bg_update_queue + ld a, [bg_update_index] + ld d, 0 + ld e, a + add hl, de ; hl = update queue + current offset + + inc a ; offset += bgu_size + inc a + inc a + ld [bg_update_index], a + + ; store ptr + ld a, c + ld [hl+], a + ld a, b + ld [hl+], a + + ; store new tile + pop af + ld [hl], a + ret diff --git a/src/video.s b/src/video.s index 92610c1..1a20089 100644 --- a/src/video.s +++ b/src/video.s @@ -11,6 +11,7 @@ vblank: call poll_inputs call scroll_write + call bg_update_queue_process ; cycle bg tiles for animations ld a, [frame_count] diff --git a/src/wram.s b/src/wram.s index b587f8e..60df776 100644 --- a/src/wram.s +++ b/src/wram.s @@ -28,6 +28,9 @@ building_state: .adv st_size ; they get updated once a frame actor_player: .adv act_size +bg_update_index: .adv 2 +bg_update_queue: .adv bge_size * BGE_MAX + ; game state ; this region holds the entire game state ; everything thats needed for a savme game should be