summaryrefslogtreecommitdiff
path: root/src/state.c
diff options
context:
space:
mode:
author0scar <qgt268@alumni.ku.dk>2020-09-29 06:51:02 +0000
committer0scar <qgt268@alumni.ku.dk>2023-07-28 09:48:17 +0000
commit6c16f339224a4736f4ed57d15bb3e5f968a635ab (patch)
treeab13afea4b6f9acfb4a139a3125f265c90bc9d80 /src/state.c
Initial independent commit
Diffstat (limited to 'src/state.c')
-rw-r--r--src/state.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/state.c b/src/state.c
new file mode 100644
index 0000000..6b1f9c8
--- /dev/null
+++ b/src/state.c
@@ -0,0 +1,91 @@
+#include <engine/state.h>
+
+#include <include_states.h>
+
+typedef StateType state_update_t (void*);
+
+const char *StateTypeStr[] = {
+ "null",
+#define State(name) #name,
+#include <state_type_list.h>
+#undef State
+ "quit",
+};
+
+void State_init(StateType type, memory *mem) {
+ switch (type) {
+#define State(name) \
+ case (STATE_##name): { \
+ name##_init(memory_allocate(mem, sizeof(name##_state))); \
+ break; \
+ }
+#include <state_type_list.h>
+#undef State
+ case STATE_null:
+ case STATE_quit:
+ _DEBUG("Got %s state.\n", StateTypeStr[type]);
+ break;
+ default:
+ exit(EXIT_FAILURE);
+ }
+}
+
+
+void State_free(StateType type, memory *mem) {
+ switch (type) {
+#define State(name) \
+ case (STATE_##name): { \
+ name##_free(mem->data); \
+ break; \
+ }
+#include <state_type_list.h>
+#undef State
+ case STATE_null:
+ case STATE_quit:
+ _DEBUG("Got %s state.\n", StateTypeStr[type]);
+ break;
+ default:
+ exit(EXIT_FAILURE);
+ }
+ memory_clear(mem);
+}
+
+
+StateType (*State_updateFunc(StateType type))(void*) {
+ switch (type) {
+#define State(name) \
+ case (STATE_##name): { \
+ return (state_update_t*)&name##_update; \
+ break; \
+ }
+#include <state_type_list.h>
+#undef State
+ case STATE_null:
+ case STATE_quit:
+ return NULL; //_DEBUG("Got %s state.\n", StateTypeStr[type]);
+ break;
+ default:
+ exit(EXIT_FAILURE);
+ }
+ return NULL;
+}
+
+StateType State_update(StateType type, memory *mem) {
+ StateType next_state = STATE_null;
+ switch (type) {
+#define State(name) \
+ case (STATE_##name): { \
+ next_state = name##_update(mem->data); \
+ break; \
+ }
+#include <state_type_list.h>
+#undef State
+ case STATE_null:
+ case STATE_quit:
+ _DEBUG("Got %s state.\n", StateTypeStr[type]);
+ break;
+ default:
+ exit(EXIT_FAILURE);
+ }
+ return next_state;
+}