GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
state_machine.h File Reference

State Machine. More...

Go to the source code of this file.

Data Structures

struct  StateInfo
 State machine callbacks. More...
 
struct  StateMachine
 State machine instance. More...
 

Macros

#define STATE_INFO_UPDATE_FN_ONLY(fn)   {.on_init = noop, .on_update = fn, .on_exit = noop}
 
#define STATE_INFO_INIT_UPDATE_FN(init_fn, update_fn)   {.on_init = init_fn, .on_update = update_fn, .on_exit = noop}
 
#define STATE_MACHINE_DEFINE(infos, num)
 

Typedefs

typedef void(* StateCallback) (void)
 State machine callback function pointer type.
 

Functions

void state_machine_register (StateMachine *state_machine)
 Register a statemachine to run it's update function once per frame.
 
void state_machine_remove (StateMachine *state_machine)
 Remove a statemachine's update function.
 
void state_machine_update (void)
 Update registered state machines' update functions.
 
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 noop (void)
 no operation
 

Detailed Description

State Machine.

This file is the interface into a generic state machine system.

State machines are defined as an array of function callbacks where each state is an index in the array with three functions per state: on_init(), on_update(), and on_exit().

**on_init()** Ran once when transitioning into the new state **on_update()** Ran once every frame **on_exit()** Ran once when exiting a state, for cleanup.

The state machine on_update() function is "registered" to a linked-list of other state machine update functions. This list will call the state machines active on_update() function. This allows much of the complexity of state transitions to remain central to state_machine.c. Also, multiple state machines can be registered to this list. This allows using substates within states, or have one off state machines like animation controllers.

When a state machine is finished, it can "remove" itself from the main update callback list. This can be done within the update method of its own state machine. This can be used to start a self destructing state machine.

Definition in file state_machine.h.

Macro Definition Documentation

◆ STATE_INFO_INIT_UPDATE_FN

#define STATE_INFO_INIT_UPDATE_FN (   init_fn,
  update_fn 
)    {.on_init = init_fn, .on_update = update_fn, .on_exit = noop}

Definition at line 110 of file state_machine.h.

◆ STATE_INFO_UPDATE_FN_ONLY

#define STATE_INFO_UPDATE_FN_ONLY (   fn)    {.on_init = noop, .on_update = fn, .on_exit = noop}

Definition at line 109 of file state_machine.h.

◆ STATE_MACHINE_DEFINE

#define STATE_MACHINE_DEFINE (   infos,
  num 
)
Value:
{ \
.state_infos = &infos[0], \
.num_infos = num, \
.registered = false, \
};

Definition at line 111 of file state_machine.h.

Typedef Documentation

◆ StateCallback

typedef void(* StateCallback) (void)

State machine callback function pointer type.

Definition at line 33 of file state_machine.h.

Function Documentation

◆ noop()

void noop ( void  )

no operation

Definition at line 14 of file state_machine.c.

◆ state_machine_change_state()

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.

Parameters
state_machinepointer to StateMachine, cannot be NULL
new_stateoffset into state_infos array to transition to

Definition at line 48 of file state_machine.c.

◆ state_machine_register()

void state_machine_register ( StateMachine state_machine)

Register a statemachine to run it's update function once per frame.

Parameters
state_machinepointer to StateMachine to register, cannot be NULL

Definition at line 16 of file state_machine.c.

◆ state_machine_remove()

void state_machine_remove ( StateMachine state_machine)

Remove a statemachine's update function.

Parameters
state_machinepointer to StateMachine to remove, cannot be NULL

Definition at line 29 of file state_machine.c.

◆ state_machine_update()

void state_machine_update ( void  )

Update registered state machines' update functions.

Definition at line 38 of file state_machine.c.