-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsm.c
79 lines (67 loc) · 1.66 KB
/
fsm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "fsm.h"
void (*fsm_state_callbacks[][5])() = {
{
mainmenu_state_init,
mainmenu_state_handleEvent,
0,
mainmenu_state_render,
mainmenu_state_clean
},
{
play_state_init,
play_state_handleEvent,
play_state_update,
play_state_render,
play_state_clean
},
{
high_scores_state_init,
high_scores_state_handleEvent,
0,
high_scores_state_render,
high_scores_state_clean
},
{
multiplayer_setup_state_init,
multiplayer_setup_state_handleEvent,
multiplayer_setup_state_update,
multiplayer_setup_state_render,
multiplayer_setup_state_clean
},
// end state
{0, 0, 0, 0, 0}
};
s_StateMachine g_stateMachine;
void _executeCallback(s_Game *game, enum fsm_state_action action);
void _executeCallback(s_Game *game, enum fsm_state_action action) {
if (fsm_state_callbacks[g_stateMachine.currentState][action] != 0) {
fsm_state_callbacks[g_stateMachine.currentState][action](game);
}
}
void fsm_init(s_Game *game) {
g_stateMachine.currentState = FSM_ENTRY_STATE;
fsm_initState(game);
}
void fsm_setState(s_Game *game, enum fsm_state_code state) {
fsm_clean(game);
g_stateMachine.currentState = state;
fsm_initState(game);
}
void fsm_initState(s_Game *game) {
_executeCallback(game, fsm_state_init);
}
void fsm_handleEvent(s_Game *game, int key) {
fsm_state_callbacks[g_stateMachine.currentState][fsm_state_handleEvent](game, key);
}
void fsm_update(s_Game *game) {
_executeCallback(game, fsm_state_update);
}
void fsm_render(s_Game *game) {
_executeCallback(game, fsm_state_render);
}
void fsm_clean(s_Game *game) {
_executeCallback(game, fsm_state_clean);
}
char fsm_isRunning() {
return g_stateMachine.currentState != FSM_EXIT_STATE;
}