63b6584706
* initial additions * Move current_blind to game vars * tmp * tmp * tmp * tmp * tmp * tmp * tmp * Compile but is broken * temp fix for token location * tmp * Working, lets clean up * remove todo * Remove odd reroll blind variable * fix skipping blind screen * fix reroll * remove magic for blind token locations * clang format * Cleanup button highlight in blind menu * some cleanup * Add layout file for global UI rects * remove dead code * Clang format * clang format it up * Move reset top left funct to gfx utils * make note of soon-to-be removed vars * Cleanup gamevars declaration * Cleanup layout file * Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Add include for stdbool.h in common_ui.h * Cleanup rect declaration * Start moving state machine stuff to new file * quick tmp * oops, add new files * working call list * fixed pointer stuff * working as per norm * working replacement * move defines to static const * fix compile issue * Add some missing "void"s * Working new substates * add statemachine to blind select * Fix game restart statemachine leak * clang-format * Clang format * Rework shop exit logic * Use new state machine in shop * Remove substates entirely * Remove debug stuff * Clang format * document state machine better * update list tests * clang-format * Update for PR * Claaaaang format * another clang-format * Remove extra state in round_end * cleanup docs for removals in lists * clang-format... again * update doxygen format * Clang format * tmp * Update for Pr * clang-format * Cleanup for only supporting losing * bug fix, thanks logs! * Fix dangling pointer bug The control of removing statemachines may call issues if starting the function is done from another state machine call. To remedy this, add a flag to determine if a state machine is registered and return early instead of blindly removing it to re-add. * Fix bug with audio iterations possibly going in reverse * Remove extra includes * Clang-format * Add sound effects for game win/lose * adjust audio steps * tmp * Nice sounding and cleaned up * Fix and cleanup * clang-format * clang-format * clang format again --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
72 lines
1.8 KiB
C
72 lines
1.8 KiB
C
#include "state_machine.h"
|
|
|
|
#include "game.h"
|
|
#include "list.h"
|
|
#include "util.h"
|
|
|
|
static List update_cbs = LIST_DEFAULT;
|
|
|
|
// Used as a No Operation for game states that have no init and/or exit function.
|
|
// ricfehr3 did the work of determining whether a noop or a NULL check was more
|
|
// efficient. Well, this is the answer.
|
|
// Thanks!
|
|
// https://github.com/cellos51/balatro-gba/issues/137#issuecomment-3322485129
|
|
void noop(void) {};
|
|
|
|
void state_machine_register(StateMachine* state_machine)
|
|
{
|
|
if (state_machine->registered)
|
|
return;
|
|
|
|
state_machine->registered = true;
|
|
|
|
state_machine->active_update = noop;
|
|
state_machine->state = UNDEFINED;
|
|
|
|
list_push_back(&update_cbs, &state_machine->active_update);
|
|
}
|
|
|
|
void state_machine_remove(StateMachine* state_machine)
|
|
{
|
|
if (!state_machine->registered)
|
|
return;
|
|
|
|
state_machine->registered = false;
|
|
list_remove_data(&update_cbs, &state_machine->active_update);
|
|
}
|
|
|
|
void state_machine_update(void)
|
|
{
|
|
ListItr itr = list_itr_create(&update_cbs);
|
|
StateCallback* cb;
|
|
while ((cb = list_itr_next(&itr)))
|
|
{
|
|
(*cb)();
|
|
}
|
|
}
|
|
|
|
void state_machine_change_state(StateMachine* state_machine, int new_state)
|
|
{
|
|
if (!state_machine->registered)
|
|
return;
|
|
|
|
if (state_machine->state >= 0 && state_machine->state < state_machine->num_infos)
|
|
{
|
|
state_machine->state_infos[state_machine->state].on_exit();
|
|
}
|
|
|
|
if (new_state >= 0 && new_state < state_machine->num_infos)
|
|
{
|
|
state_machine->state_infos[new_state].on_init();
|
|
|
|
state_machine->active_update = state_machine->state_infos[new_state].on_update;
|
|
|
|
state_machine->state = new_state;
|
|
}
|
|
else
|
|
{
|
|
state_machine->active_update = noop;
|
|
state_machine->state = UNDEFINED;
|
|
}
|
|
}
|