forked from ScatteredRay/SurfaceTension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemmanager.cpp
137 lines (121 loc) · 2.72 KB
/
systemmanager.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "systemmanager.h"
#include <assert.h>
struct GameSystem
{
symbol Name;
init_func Init;
destroy_func Destroy;
void* SystemPointer;
GameSystem* NextSystem;
};
struct SystemManager
{
GameSystem* FirstSystem;
GameSystem** LastSystemSlot;
GameSystem* RunSystem;
run_func RunFunc;
symbol_table* symbols;
};
void declare_system(SystemManager* SM, symbol name, init_func init, destroy_func destroy)
{
GameSystem* System = new GameSystem;
System->Name = name;
System->Init = init;
System->Destroy = destroy;
System->SystemPointer = NULL;
System->NextSystem = NULL;
*SM->LastSystemSlot = System;
SM->LastSystemSlot = &System->NextSystem;
}
void set_run_system(SystemManager* SM, symbol name, run_func run)
{
GameSystem* System = SM->FirstSystem;
while(System)
{
if(System->Name == name)
{
SM->RunSystem = System;
SM->RunFunc = run;
return;
}
System = System->NextSystem;
}
}
void system_manager_run(SystemManager* SM)
{
SM->RunFunc(SM->RunSystem->SystemPointer);
}
SystemManager* create_system_manager(symbol_table* tbl)
{
SystemManager* SM = new SystemManager();
SM->FirstSystem = NULL;
SM->LastSystemSlot = &SM->FirstSystem;
SM->RunSystem = NULL;
SM->symbols = tbl;
return SM;
}
// Doesn't destroy the systems.
void destroy_system_manager(SystemManager* SM)
{
GameSystem* System = SM->FirstSystem;
while(System)
{
GameSystem* Tmp = System->NextSystem;
delete System;
System = Tmp;
}
delete SM;
}
void system_manager_init_systems(SystemManager* SM)
{
GameSystem* System = SM->FirstSystem;
while(System)
{
System->SystemPointer = System->Init(SM);
System = System->NextSystem;
}
}
void system_manager_shutdown_systems(SystemManager* SM)
{
GameSystem* System = SM->FirstSystem;
while(System)
{
System->Destroy(System->SystemPointer);
System->SystemPointer = NULL;
System = System->NextSystem;
}
}
GameSystem* system_manager_find(SystemManager* SM, const char* Sys)
{
GameSystem* System = SM->FirstSystem;
symbol system_symbol = symbol_from_string(SM->symbols, Sys);
while(System)
{
if(System->Name == system_symbol)
return System;
System = System->NextSystem;
}
return NULL;
}
// This needs to ensure it's been initialized already
void* system_manager_require(SystemManager* SM, const char* Sys)
{
GameSystem* System = system_manager_find(SM, Sys);
assert(System);
return System->SystemPointer;
}
void* system_manager_request(SystemManager* SM, const char* Sys)
{
GameSystem* System = system_manager_find(SM, Sys);
if(System)
return System->SystemPointer;
return NULL;
}
symbol_table* system_manager_get_symbol_table(SystemManager* SM)
{
return SM->symbols;
}
symbol system_manager_symbol(SystemManager* SM, const char* str)
{
return symbol_from_string(SM->symbols, str);
}