bg: Added bg update queue
authorLukas Krickl <lukas@krickl.dev>
Mon, 14 Apr 2025 13:41:55 +0000 (15:41 +0200)
committerLukas Krickl <lukas@krickl.dev>
Mon, 14 Apr 2025 13:41:55 +0000 (15:41 +0200)
This queue can store up to 32 update requests for tiles.

src/buildings.s
src/defs.s
src/macros.inc
src/player.s
src/state.s
src/update.s
src/video.s
src/wram.s

index 3ad7d435d92577ab1ab261c4bfc26a793b5f5771..92fca865d7871a25ab7ec2541f3db90a8cfab893 100644 (file)
@@ -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
 
index a8e3efc035576e0a2ceab442a42bd1842055cc7c..531c4883b77c198fbf6e00e1f817512b1be24ce5 100644 (file)
@@ -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)
 
 .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
index 4f82d8a2c1ea07cd40ea90455311a5ae364d516e..b4b6ee1858558551522836b6e272c75d4ca317aa 100644 (file)
   .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
index adafaffdb1ae2eadb9fcdd06d50838bff63d174d..d28623fd9c84bc2571a61e46da799f9498c655d0 100644 (file)
@@ -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
index 5ead5ca8fdde8206fb6951ec82d6969ea3a53e92..914e0814235ab7e326ef8b89587b3675aed1dea4 100644 (file)
@@ -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:
index e9be5fc2e0f0c91a26ea31bcef5898b04496e334..294245a5278a04e43a52db9d343b552b669af0f0 100644 (file)
@@ -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
index 92610c11a6940a150be0029290f00a3892b71d16..1a20089a2da01592da5d89506fbebc81c668370b 100644 (file)
@@ -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]
index b587f8e900ecfa9002138815822a7ef422b80313..60df77661bdeb6ba09d42b759bf23c4660f2320e 100644 (file)
@@ -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