Refactored state machine.

Added a state info map which keeps track of a particular state's on_init, on_update and on_exit functions.
A few of the on_update functions have sub-states, which create extremely long switch statements.
Not sure if perhaps the best route would be to keep the switch statements but break the cases into individual
functions or if we should keep track of substates. The latter sounds like it could get complicated and really
messy quick. Problem for another day.
This commit is contained in:
Drew
2025-10-14 10:20:40 -05:00
parent 6dce57e197
commit 121bbc01d5
6 changed files with 149 additions and 118 deletions
+31 -9
View File
@@ -28,14 +28,15 @@
enum GameState
{
GAME_SPLASH_SCREEN,
GAME_MAIN_MENU,
GAME_PLAYING,
GAME_ROUND_END,
GAME_SHOP,
GAME_BLIND_SELECT,
GAME_LOSE,
GAME_WIN
GAME_STATE_SPLASH_SCREEN,
GAME_STATE_MAIN_MENU,
GAME_STATE_PLAYING,
GAME_STATE_ROUND_END,
GAME_STATE_SHOP,
GAME_STATE_BLIND_SELECT,
GAME_STATE_LOSE,
GAME_STATE_WIN,
GAME_STATE_MAX
};
enum HandState
@@ -75,10 +76,30 @@ enum HandType
FLUSH_FIVE
};
typedef struct
{
int round;
int ante;
int money;
int score;
int temp_score;
int chips;
int mult;
int current_blind;
} RunState;
typedef struct
{
enum GameState state;
void (*on_init)();
void (*on_update)();
void (*on_exit)();
} StateInfo;
// Game functions
void game_init();
void game_update();
void game_set_state(enum GameState new_game_state);
void game_change_state(enum GameState new_game_state);
// Forward declaration
struct List;
@@ -88,6 +109,7 @@ typedef struct List List;
typedef struct CardObject CardObject; // forward declaration, actually declared in card.h
typedef struct Card Card;
typedef struct JokerObject JokerObject;
CardObject** get_hand_array(void);
int get_hand_top(void);
int hand_get_size(void);
+9
View File
@@ -0,0 +1,9 @@
// (stateEnum, on_init, on_update, on_exit)
DEF_STATE_INFO(GAME_STATE_SPLASH_SCREEN, splash_screen_init, splash_screen_update, _noop)
DEF_STATE_INFO(GAME_STATE_MAIN_MENU, game_main_menu_init, game_main_menu_update, _noop)
DEF_STATE_INFO(GAME_STATE_PLAYING, game_round_init, game_playing_update, _noop)
DEF_STATE_INFO(GAME_STATE_ROUND_END, _noop, game_round_end_update, game_round_end_exit)
DEF_STATE_INFO(GAME_STATE_SHOP, _noop, game_shop_update, game_state_shop_exit)
DEF_STATE_INFO(GAME_STATE_BLIND_SELECT, _noop, game_blind_select_update, game_blind_select_exit)
DEF_STATE_INFO(GAME_STATE_LOSE, game_lose_init, game_lose_update, _noop)
DEF_STATE_INFO(GAME_STATE_WIN, game_win_init, game_win_update, _noop)
+15
View File
@@ -0,0 +1,15 @@
DEF_JOKER_GFX(0)
DEF_JOKER_GFX(1)
DEF_JOKER_GFX(2)
DEF_JOKER_GFX(3)
DEF_JOKER_GFX(4)
DEF_JOKER_GFX(5)
DEF_JOKER_GFX(6)
DEF_JOKER_GFX(7)
DEF_JOKER_GFX(8)
DEF_JOKER_GFX(9)
DEF_JOKER_GFX(10)
DEF_JOKER_GFX(11)
DEF_JOKER_GFX(12)
DEF_JOKER_GFX(13)
DEF_JOKER_GFX(14)
+2 -2
View File
@@ -8,6 +8,6 @@
#define SPLASH_DURATION_FRAMES (SPLASH_FPS * SPLASH_DURATION_SECONDS)
void splash_screen_init();
void splash_screen_update(uint timer);
void splash_screen_update();
#endif // SPLASH_SCREEN_H
#endif // SPLASH_SCREEN_H