GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
state_machine.c
1#include "state_machine.h"
2
3#include "game.h"
4#include "list.h"
5#include "util.h"
6
7static List s_update_cbs = LIST_DEFAULT;
8
9// Used as a No Operation for game states that have no init and/or exit function.
10// ricfehr3 did the work of determining whether a noop or a NULL check was more
11// efficient. Well, this is the answer.
12// Thanks!
13// https://github.com/cellos51/balatro-gba/issues/137#issuecomment-3322485129
14void noop(void) {};
15
17{
18 if (state_machine->registered)
19 return;
20
21 state_machine->registered = true;
22
23 state_machine->active_update = noop;
24 state_machine->state = UNDEFINED;
25
26 list_push_back(&s_update_cbs, &state_machine->active_update);
27}
28
30{
31 if (!state_machine->registered)
32 return;
33
34 state_machine->registered = false;
35 list_remove_data(&s_update_cbs, &state_machine->active_update);
36}
37
39{
40 ListItr itr = list_itr_create(&s_update_cbs);
41 StateCallback* cb;
42 while ((cb = list_itr_next(&itr)))
43 {
44 (*cb)();
45 }
46}
47
48void state_machine_change_state(StateMachine* state_machine, int new_state)
49{
50 if (!state_machine->registered)
51 return;
52
53 if (state_machine->state >= 0 && state_machine->state < state_machine->num_infos)
54 {
55 state_machine->state_infos[state_machine->state].on_exit();
56 }
57
58 if (new_state >= 0 && new_state < state_machine->num_infos)
59 {
60 state_machine->state_infos[new_state].on_init();
61
62 state_machine->active_update = state_machine->state_infos[new_state].on_update;
63
64 state_machine->state = new_state;
65 }
66 else
67 {
68 state_machine->active_update = noop;
69 state_machine->state = UNDEFINED;
70 }
71}
A doubly-linked list.
void * list_itr_next(ListItr *itr)
Definition list.c:281
bool list_remove_data(List *list, void *data)
Definition list.c:316
#define LIST_DEFAULT
Default list declaration for empty lists.
Definition list.h:30
void list_push_back(List *list, void *data)
Definition list.c:89
ListItr list_itr_create(List *list)
Definition list.c:257
State Machine.
void(* StateCallback)(void)
State machine callback function pointer type.
void state_machine_remove(StateMachine *state_machine)
Remove a statemachine's update function.
void state_machine_register(StateMachine *state_machine)
Register a statemachine to run it's update function once per frame.
void noop(void)
no operation
void state_machine_change_state(StateMachine *state_machine, int new_state)
Calls the current state's on_exit, the new state's on_init, and sets the active update fn.
void state_machine_update(void)
Update registered state machines' update functions.
An iterator into a list.
Definition list.h:91
A doubly-linked list.
Definition list.h:61
State machine instance.
StateInfo * state_infos
Array of StateCallbacks , one entry per state.
bool registered
Flag to determine if statemachine is already running it's active_update function.
int state
The current state of the state machine, the offset into state_infos.
unsigned int num_infos
Number of elements in the state_infos array.
StateCallback active_update
Pointer to the active update function in state_infos
Utilities relating around number string representation and protected arithmatic helper functions.