From 1847b06213e0c66f32a195f766f5544aa4800d00 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Thu, 22 May 2025 05:25:27 +0200 Subject: [PATCH] units: Draw calls are not done outside of the state machine This allows drawing to continue even if the unit state is awaiting an idle state. It also allows states to be shared between units more easily. --- src/defs.s | 10 +++++++ src/macros.inc | 9 +++++- src/unit.s | 76 ++++++++++++++++++++++++++++++-------------------- 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/src/defs.s b/src/defs.s index 4e97540..f829003 100644 --- a/src/defs.s +++ b/src/defs.s @@ -117,6 +117,16 @@ .de act_st_interact, 2 .de act_st_active, 2 .de act_st_idle, 2 + + ; ptr to function for drawing the actor + ; called after state update + ; replace in state code if drawing needs to be + ; changed at runtime + ; inputs: + ; de: actor + ; returns: + ; bc: null +.de act_draw, 2 .de act_size, 0 ; max bge queue size diff --git a/src/macros.inc b/src/macros.inc index 632dfee..c51fcb0 100644 --- a/src/macros.inc +++ b/src/macros.inc @@ -133,7 +133,14 @@ dw $2 dw $3 #endmacro - + + ; defines draw call for actor + ; inputs: + ; $1: draw call +#macro act_def_draw + dw $1 +#endmacro + ; define an actor without state ; to define an actor call ; st_def and act_def diff --git a/src/unit.s b/src/unit.s index c856bf2..2c9219a 100644 --- a/src/unit.s +++ b/src/unit.s @@ -28,6 +28,10 @@ units_update: call st_update pop hl + + push hl + call unit_update_draw + pop hl @skip: ; next actor ld de, act_size @@ -40,6 +44,25 @@ units_update: ret + ; calls unit's draw function + ; inputs: + ; hl: actor ptr +unit_update_draw: + push hl + ld de, act_draw + add hl, de + ; hl = draw ptr + ld a, [hl+] + ld e, a + ld a, [hl] + ld d, a + ; de = draw function + push de + pop hl ; hl = draw function + pop de ; de = actor + call_hl + ret + unit_demo_1_init: ldnull bc ret @@ -58,17 +81,8 @@ unit_demo_1_update: call unit_scroll_center pop de - push bc - call unit_demo_1_draw - pop bc - ret - ; inputs - ; de: actor -unit_demo_1_idle: - call unit_demo_1_draw - ret ; inputs ; de: actor @@ -81,11 +95,6 @@ unit_demo_1_draw: ldnull bc ret - ; inputs: - ; de: actor -unit_demo_1_delay_to_active: - jp unit_delay_to_active - ; draws any unit ; inputs: ; de: actor @@ -142,7 +151,7 @@ unit_handle_inputs: jr z, @notup REL call unit_try_move_up - ld bc, st_unit_demo_1_delay_to_active + ld bc, st_unit_delay_to_active ret @notup: @@ -150,7 +159,7 @@ unit_handle_inputs: jr z, @notdown REL call unit_try_move_down - ld bc, st_unit_demo_1_delay_to_active + ld bc, st_unit_delay_to_active ret @notdown: @@ -158,7 +167,7 @@ unit_handle_inputs: jr z, @notleft REL call unit_try_move_left - ld bc, st_unit_demo_1_delay_to_active + ld bc, st_unit_delay_to_active ret @notleft: @@ -166,7 +175,7 @@ unit_handle_inputs: jr z, @notright REL call unit_try_move_right - ld bc, st_unit_demo_1_delay_to_active + ld bc, st_unit_delay_to_active ret @notright: @@ -265,8 +274,6 @@ unit_use_move: dec a ld [hl], a - call unit_pause_objs - ; we need the z flag to not be set ehre ; so just do something that will always unset it or a, 1 @@ -571,6 +578,12 @@ unit_delay_to_active: ldnull bc ret + ; inputs + ; de: actor +unit_idle: + ldnull bc + ret + ; pauses object redraws ; until unpause is called unit_pause_objs: @@ -587,29 +600,32 @@ unit_resume_objs: ret unit_demo_1: - st_def 0x00, unit_demo_1_init, st_unit_demo_1_idle + st_def 0x00, unit_demo_1_init, st_unit_idle act_def ACT_T_DEMO_1, 0, 1, 2, 3, 4, 6, 2, 2, 0 - act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_demo_1_idle + act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_idle + act_def_draw unit_demo_1_draw unit_demo_2: - st_def 0x00, unit_demo_1_init, st_unit_demo_1_idle + st_def 0x00, unit_demo_1_init, st_unit_idle act_def ACT_T_DEMO_1, 0, 1, 2, 3, 1, 5, 3, 3, 0 - act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_demo_1_idle + act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_idle + act_def_draw unit_demo_1_draw unit_demo_3: - st_def 0x00, unit_demo_1_init, st_unit_demo_1_idle + st_def 0x00, unit_demo_1_init, st_unit_idle act_def ACT_T_DEMO_1, 0, 1, 2, 3, 0, 5, 4, 4, 0 - act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_demo_1_idle + act_st_def NULL, NULL, st_unit_demo_1_update, st_unit_idle + act_def_draw unit_demo_1_draw st_unit_demo_1_update: st_def 0x00, unit_demo_1_update, st_unit_demo_1_update -st_unit_demo_1_idle: - st_def 0x00, unit_demo_1_idle, st_unit_demo_1_idle +st_unit_idle: + st_def 0x00, unit_idle, st_unit_idle -st_unit_demo_1_delay_to_active: - st_def CURSOR_MOVE_TIMER, unit_demo_1_delay_to_active, st_unit_switch_to_active +st_unit_delay_to_active: + st_def CURSOR_MOVE_TIMER, unit_delay_to_active, st_unit_switch_to_active st_unit_switch_to_active: st_def 0, unit_switch_to_active, st_unit_switch_to_active -- 2.30.2