GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
state_machine.h
Go to the documentation of this file.
1
27#ifndef STATE_MACHINE_H
28#define STATE_MACHINE_H
29
33typedef void (*StateCallback)(void);
34
38typedef struct
39{
40 StateCallback on_init;
41 StateCallback on_update;
42 StateCallback on_exit;
43} StateInfo;
44
48typedef struct
49{
54
59
63 unsigned int num_infos;
64
68 int state;
69
75
81void state_machine_register(StateMachine* state_machine);
82
88void state_machine_remove(StateMachine* state_machine);
89
93void state_machine_update(void);
94
101void state_machine_change_state(StateMachine* state_machine, int new_state);
102
106void noop(void);
107
108// clang-format off
109#define STATE_INFO_UPDATE_FN_ONLY(fn) {.on_init = noop, .on_update = fn, .on_exit = noop}
110#define STATE_INFO_INIT_UPDATE_FN(init_fn, update_fn) {.on_init = init_fn, .on_update = update_fn, .on_exit = noop}
111#define STATE_MACHINE_DEFINE(infos, num) \
112{ \
113 .state_infos = &infos[0], \
114 .num_infos = num, \
115 .registered = false, \
116};
117// clang-format on
118
119#endif // STATE_MACHINE_H
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.
State machine callbacks.
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