Files
balatro-gba-chinese-jocker-…/include/state_machine.h
T
Geralt 1df8402900 [Feature] Implement seeded runs by adding a Seed selection menu (#482)
* initial commit

* wip

* WIP

* WIP

* clang format

* Choose Seed navigation mostly done

* seed input navigation done

* seed input done

* cleanup and clang format

* Implement base 10 to 36 conversions

* Enable seeded runs

* clang format

* show default deck sprite, name and description

* Rename Deck enum to DeckType

* Implement arbitrary 9-patch expand system

* Make 9-patch example smaller in the docs

* Improve NinePatchRect docs

* fix Deck back side sprite palette

* clang format

* Add last deck description

* clang format

* Fix 'Z' in seed input keyboard

* Fix rebase issue

* fix deck sprite not updating

* Documents static funcs in run_setup.c

* more documentation

* clang format

* Move Play button to the left in its row

* Tests WIP

* removing tests for random.h

* use the new StateMachine struct

* First wave of fixes

* Fix the 9-patch again

* Changed the base 36 logic so it's in big endian

* properly set end of base 36 string to '\0'

* typos

* fix cursor position after rolling a seed + fix random seed generation

* Make keyboard more intuitive to use

* static const var

* Address more comment from @ricfehr3

* clang format

* clang format

* clang format

* clang format

* clang format

* clang format

* clang format

* fix deck palette

* create the `DEF_SEED_KEYBOARD_BUTTON_OBJECT` macro

* Replace `expand_3x3` by a call to `expand_9-patch`

* clang format

* clang format

* clang format

* fix rebase iussue in state_machine.h

---------

Co-authored-by: MathisMartin31 <mathis.martin31@gmail.com>
2026-06-02 00:06:37 -07:00

120 lines
3.5 KiB
C

/**
* @file state_machine.h
*
* @brief 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.
*/
#ifndef STATE_MACHINE_H
#define STATE_MACHINE_H
/**
* @brief State machine callback function pointer type
*/
typedef void (*StateCallback)(void);
/**
* @brief State machine callbacks
*/
typedef struct
{
StateCallback on_init;
StateCallback on_update;
StateCallback on_exit;
} StateInfo;
/**
* @brief State machine instance
*/
typedef struct
{
/**
* @brief Pointer to the active update function in `state_infos`
*/
StateCallback active_update;
/**
* @brief Array of @ref StateCallbacks , one entry per state
*/
StateInfo* state_infos;
/**
* @brief Number of elements in the `state_infos` array
*/
unsigned int num_infos;
/**
* @brief The current state of the state machine, the offset into state_infos
*/
int state;
/**
* @brief Flag to determine if statemachine is already running it's active_update function
*/
bool registered;
} StateMachine;
/**
* @brief Register a statemachine to run it's update function once per frame
*
* @param state_machine pointer to @ref StateMachine to register, cannot be NULL
*/
void state_machine_register(StateMachine* state_machine);
/**
* @brief Remove a statemachine's update function
*
* @param state_machine pointer to @ref StateMachine to remove, cannot be NULL
*/
void state_machine_remove(StateMachine* state_machine);
/**
* @brief Update registered state machines' update functions
*/
void state_machine_update(void);
/**
* @brief Calls the current state's on_exit, the new state's on_init, and sets the active update fn
*
* @param state_machine pointer to @ref StateMachine, cannot be NULL
* @param new_state offset into `state_infos` array to transition to
*/
void state_machine_change_state(StateMachine* state_machine, int new_state);
/**
* @brief no operation
*/
void noop(void);
// clang-format off
#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) \
{ \
.state_infos = &infos[0], \
.num_infos = num, \
.registered = false, \
};
// clang-format on
#endif // STATE_MACHINE_H