aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAstatin <[email protected]>2024-09-10 16:10:59 +0900
committerAstatin <astatin@redacted>2024-09-10 16:10:59 +0900
commitc3d302cd8332d55bb477b5e93ea0f6027d26c632 (patch)
tree2f050fc21968051e95b624c1dff5c63533bf395f
parent8a69fd747d22206afe4ea31a743e58070699b3d9 (diff)
Optimize object displays and preload map scrolling to stay in VBlank
-rw-r--r--definitions.gbasm7
-rw-r--r--main.gbasm6
-rw-r--r--map/loading.gbasm104
-rw-r--r--map/objects.gbasm173
-rw-r--r--scripts/generate-tiledata.py2
-rw-r--r--sprites/bg/carrot.pngbin0 -> 164 bytes
-rw-r--r--tiles.gbasm2
-rw-r--r--tileset.gbasm6
8 files changed, 190 insertions, 110 deletions
diff --git a/definitions.gbasm b/definitions.gbasm
index 18b4237..84e7691 100644
--- a/definitions.gbasm
+++ b/definitions.gbasm
@@ -27,6 +27,13 @@
.DEFINE mem_map_loading_flags ($c00c)
; bit 0: if the object should be reloaded (scroll or first load)
; bit 1: if the interactions should be checked (at the end of a movement)
+; bit 2: if the prepared block should be updated at the next frame
+
+.DEFINE mem_prepared_block_tile ($c00d)
+.DEFINE mem_prepared_block_position_1 ($c00e)
+.DEFINE mem_prepared_block_position_2 ($c00f)
+
+.DEFINE mem_object_load_counter ($c010)
.DEFINE next_free_head_higher_bytes $c7
.DEFINE mem_next_free_head_lower_bytes ($c6ff)
diff --git a/main.gbasm b/main.gbasm
index f96b893..ad1fa44 100644
--- a/main.gbasm
+++ b/main.gbasm
@@ -10,6 +10,7 @@ New_Dungeon:
CALL =Initialize_Objects
CALL =Load_Tile
CALL =Load_Map
+ CALL =Load_Objects
; LCDC
LD A, $87
@@ -21,8 +22,8 @@ New_Dungeon:
JP =Wait_for_VRAM.loop
VBLANK_Entrypoint:
- CALL =Display_Scrolling_Map
- CALL =Display_Objects
+ CALL =Display_Prepared_Block
+ CALL =Display_Object
CALL =Display_Entities
CALL =Object_Interactions_Check
@@ -30,6 +31,7 @@ VBLANK_Entrypoint:
CALL =Pad_Button_Check
CALL =Move_Bunny
+ CALL =Prepare_Scrolling_Map
RET
.INCLUDE "tiles.gbasm"
diff --git a/map/loading.gbasm b/map/loading.gbasm
index e42f388..be4c8a4 100644
--- a/map/loading.gbasm
+++ b/map/loading.gbasm
@@ -13,6 +13,7 @@ Load_Map:
Load_Map.For_X:
LD A, C
CALL =Load_Block
+ CALL =Display_Prepared_Block
INC C
LD A, C
@@ -98,7 +99,7 @@ Load_Column: ; (+1/-1 direction in B)
POP AF
RET
-Display_Scrolling_Map:
+Prepare_Scrolling_Map:
LD A, $mem_map_loading_flags
BIT 0, A
JR Z, =Display_Scrolling_Map.End
@@ -211,59 +212,33 @@ Load_Block: ; X in A, Y in B
ADD $20
LD C, A
+ LD A, $mem_map_loading_flags
+ SET 2, A
+ LD $mem_map_loading_flags, A
+
+
POP DE
+
+ LD A, D
+ LD $mem_prepared_block_position_1, A
+ LD A, E
+ LD $mem_prepared_block_position_2, A
+
POP AF
CALL =Is_Solid
-
CP $00
+
JR Z, =Load_Block.Empty
Load_Block.Solid:
LD A, C
- CALL =VBlank_Wait
- LD (DE), A
- LD A, E
- OR $20
- LD E, A
- LD A, C
- OR $02
- CALL =VBlank_Wait
- LD (DE), A
- INC E
- LD A, C
- OR $03
- CALL =VBlank_Wait
- LD (DE), A
- LD A, E
- AND $DF
- LD E, A
- LD A, C
- OR $01
- CALL =VBlank_Wait
- LD (DE), A
+ LD $mem_prepared_block_tile, A
JR =Load_Block.End
Load_Block.Empty:
LD A, $00
- CALL =VBlank_Wait
- LD (DE), A
- LD A, E
- OR $20
- LD E, A
- LD A, $00
- CALL =VBlank_Wait
- LD (DE), A
- INC E
- LD A, $00
- CALL =VBlank_Wait
- LD (DE), A
- LD A, E
- AND $DF
- LD E, A
- LD A, $00
- CALL =VBlank_Wait
- LD (DE), A
+ LD $mem_prepared_block_tile, A
Load_Block.End:
@@ -272,6 +247,53 @@ Load_Block: ; X in A, Y in B
POP BC
RET
+Display_Prepared_Block:
+ PUSH AF
+ PUSH BC
+ PUSH DE
+
+ LD A, $mem_map_loading_flags
+ BIT 2, A
+ JR Z, =Display_Prepared_Block.End
+
+ LD A, $mem_map_loading_flags
+ RES 2, A
+ LD $mem_map_loading_flags, A
+
+ LD A, $mem_prepared_block_position_1
+ LD D, A
+
+ LD A, $mem_prepared_block_position_2
+ LD E, A
+
+ LD A, $mem_prepared_block_tile
+ LD C, A
+
+ LD (DE), A
+ LD A, E
+ OR $20
+ LD E, A
+ LD A, C
+ OR $02
+ LD (DE), A
+ INC E
+ LD A, C
+ OR $03
+ LD (DE), A
+ LD A, E
+ AND $DF
+ LD E, A
+ LD A, C
+ OR $01
+ LD (DE), A
+
+ Display_Prepared_Block.End:
+
+ POP DE
+ POP BC
+ POP AF
+ RET
+
Is_Solid: ; X in A, Y in B, Result A
PUSH BC
diff --git a/map/objects.gbasm b/map/objects.gbasm
index bfaa903..22a65ee 100644
--- a/map/objects.gbasm
+++ b/map/objects.gbasm
@@ -3,6 +3,10 @@ Initialize_Objects:
OR $01
LD $mem_map_loading_flags, A
LD HL, $mem_object_list
+ CALL =Spawn_stairs
+ RET
+
+Spawn_stairs:
LD A, $60
LD (HL+), A
@@ -42,6 +46,9 @@ Initialize_Objects:
LD (HL+), A
LD A, C
LD (HL+), A
+ INC HL
+ INC HL
+ INC HL
RET
Stairs_action:
@@ -57,88 +64,109 @@ Stairs_action:
JP =New_Dungeon
-Display_Objects:
- LD A, $mem_map_loading_flags
- BIT 0, A
- JR Z, =Display_Objects.End
- LD A, $mem_moving_animation_step
- CP $00
- JR NZ, =Display_Objects.End
+Display_Object:
LD HL, $mem_object_list
Display_Objects.loop:
- LD A, (HL+)
- CP $00
- JR Z, =Display_Objects.next
- LD C, A
- LD A, (HL+)
- ADD $80
- LD D, A
- LD A, $mem_viewport_x
- SUB $83
- CP D
- JR NC, =Display_Objects.next
- ADD $10
- CP D
- JR C, =Display_Objects.next
- LD A, D
- SUB $80
- LD D, A
+ LD A, $mem_object_load_counter
+ INC A
+ AND $0f
+ LD $mem_object_load_counter, A
+ SLA A
+ SLA A
+ SLA A
+ AND $f8
+ LD L, A
- LD A, (HL+)
- ADD $80
- LD B, A
- LD A, $mem_viewport_y
- SUB $83
- CP B
- JR NC, =Display_Objects.next
- ADD $10
- CP B
- JR C, =Display_Objects.next
- LD A, B
- SUB $80
- LD B, A
+ LD A, (HL+)
+ LD C, A
+ LD A, (HL+)
+ ADD $80
+ LD D, A
+ LD A, $mem_viewport_x
+ SUB $83
+ CP D
+ JR NC, =Display_Objects.End
+ ADD $10
+ CP D
+ JR C, =Display_Objects.End
+ LD A, D
+ SUB $80
+ LD D, A
- LD A, D
- CALL =Construct_Tile_Address
+ LD A, (HL+)
+ ADD $80
+ LD B, A
+ LD A, $mem_viewport_y
+ SUB $83
+ CP B
+ JR NC, =Display_Objects.End
+ ADD $10
+ CP B
+ JR C, =Display_Objects.End
+ LD A, B
+ SUB $80
+ LD B, A
- CALL =VBlank_Wait
- LD A, C
- LD (DE), A
+ LD A, C
+ CP $00
- INC E
- INC C
+ JR Z, =Display_Objects.Nothing
- CALL =VBlank_Wait
- LD A, C
- LD (DE), A
+ LD A, D
+ CALL =Construct_Tile_Address
- INC C
+ LD A, C
+ LD (DE), A
- LD A, E
- XOR $21
- LD E, A
+ INC E
+ INC C
- CALL =VBlank_Wait
- LD A, C
- LD (DE), A
+ LD A, C
+ LD (DE), A
- INC C
- INC E
+ INC C
- CALL =VBlank_Wait
- LD A, C
- LD (DE), A
+ LD A, E
+ XOR $21
+ LD E, A
+
+ LD A, C
+ LD (DE), A
+
+ INC C
+ INC E
+
+ LD A, C
+ LD (DE), A
- Display_Objects.next:
- LD A, L
- AND $f8
- ADD $08
- LD L, A
- CP $80
- JR NZ, =Display_Objects.loop
Display_Objects.End:
RET
+ Display_Objects.Nothing:
+ LD A, D
+ CALL =Load_Block
+ CALL =Display_Prepared_Block
+ RET
+
+Load_Objects:
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ CALL =Display_Object
+ RET
+
Object_Interactions_Check:
LD A, $mem_map_loading_flags
BIT 1, A
@@ -166,8 +194,21 @@ Object_Interactions_Check:
LD A, (HL+)
LD E, A
+ PUSH BC
+
+ ; So we have HL set to the object pointer
+ LD A, L
+ AND $f8
+ LD L, A
+
+ ; This is confusing but this thing is actually CALL DE
+ LD BC, =Object_Interactions_Check.interaction_end
+ PUSH BC
PUSH DE
RET
+ Object_Interactions_Check.interaction_end:
+
+ POP BC
Object_Interactions_Check.next:
LD A, L
diff --git a/scripts/generate-tiledata.py b/scripts/generate-tiledata.py
index ff62b0e..25c79ce 100644
--- a/scripts/generate-tiledata.py
+++ b/scripts/generate-tiledata.py
@@ -16,6 +16,8 @@ print("\n\t; Trees")
get_sprite_png_parse_output("./sprites/bg/tree-tileset.png")
print("\n\t; Stairs")
get_sprite_png_parse_output("./sprites/bg/stairs.png")
+print("\n\t; Carrot")
+get_sprite_png_parse_output("./sprites/bg/carrot.png")
sprite_idx = 0x02
print("\nOBJ_Tile_Image_Data:")
diff --git a/sprites/bg/carrot.png b/sprites/bg/carrot.png
new file mode 100644
index 0000000..1c42ba9
--- /dev/null
+++ b/sprites/bg/carrot.png
Binary files differ
diff --git a/tiles.gbasm b/tiles.gbasm
index 48cb285..6c902d4 100644
--- a/tiles.gbasm
+++ b/tiles.gbasm
@@ -24,7 +24,7 @@ Load_Tile:
CALL =Load_Number_Font
LD HL, $9200
LD DE, =BG_Tile_Image_Data
- LD BC, $0440
+ LD BC, $0480
CALL =memcpy
RET
diff --git a/tileset.gbasm b/tileset.gbasm
index 39a32db..854e33e 100644
--- a/tileset.gbasm
+++ b/tileset.gbasm
@@ -72,6 +72,12 @@ BG_Tile_Image_Data:
.DB $9f, $91, $ff, $91, $ff, $91, $ff, $91, $ff, $91, $fd, $93, $f1, $9f, $ff, $ff ; 0x62
.DB $ff, $11, $ff, $11, $ff, $11, $f1, $1f, $91, $7f, $11, $ff, $11, $ff, $ff, $ff ; 0x63
+ ; Carrot
+ .DB $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $02, $02, $07, $05, $0f, $08 ; 0x64
+ .DB $00, $00, $30, $30, $58, $68, $b4, $fc, $fe, $aa, $ac, $f4, $58, $b8, $a0, $e0 ; 0x65
+ .DB $1f, $10, $1f, $10, $3e, $21, $3c, $23, $7b, $47, $7c, $4c, $70, $70, $00, $00 ; 0x66
+ .DB $c0, $40, $a0, $60, $20, $e0, $c0, $c0, $00, $00, $00, $00, $00, $00, $00, $00 ; 0x67
+
OBJ_Tile_Image_Data:
; Bunny side