This repository was archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathengine.cpp
More file actions
220 lines (154 loc) · 4.89 KB
/
engine.cpp
File metadata and controls
220 lines (154 loc) · 4.89 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "engine.h"
#include "util/threadstate.h"
platform_api* global_api = null;
DLL_EXPORT engine* start_up(platform_api* api) {
engine* state = new(api->heap_alloc(sizeof(engine))) engine;
state->func_state.this_dll = api->your_dll;
state->platform = api;
global_api = api;
global_log = &state->log;
global_dbg = &state->dbg;
global_func = &state->func_state;
setup_fptrs();
state->basic_a = MAKE_PLATFORM_ALLOCATOR("basic"_);
begin_thread("main"_, &state->basic_a);
state->dbg_a = MAKE_PLATFORM_ALLOCATOR("dbg"_);
state->dbg.init(&state->dbg_a);
state->dbg.profiler.register_thread(180);
BEGIN_FRAME();
{ PROF_SCOPE("Init Log"_);
state->log_a = MAKE_PLATFORM_ALLOCATOR("log"_);
state->log = log_manager::make(&state->log_a);
state->dbg.console.setup_log(&state->log);
platform_file log_all_file;
CHECKED(create_file, &log_all_file, "log_all.html"_, platform_file_open_op::cleared);
state->log.add_file(log_all_file, log_level::alloc, log_out_type::html);
state->log.add_stdout(log_level::debug);
}
LOG_PUSH_CONTEXT("startup"_);
LOG_INFO("Beginning startup..."_);
{ PROF_SCOPE("Start Log"_);
LOG_INFO("Starting logger..."_);
state->log.start();
}
{ PROF_SCOPE("Init Events"_);
LOG_INFO("Setting up events..."_);
state->evt_a = MAKE_PLATFORM_ALLOCATOR("event"_);
state->evt = evt_manager::make(&state->evt_a);
state->evt.start();
}
{ PROF_SCOPE("Init Window"_);
LOG_INFO("Creating window..."_);
_memcpy("Exile", state->window.settings.c_title, 6);
CHECKED(create_window, &state->window);
}
{ PROF_SCOPE("Init OpenGL"_);
LOG_INFO("Setting up OpenGL..."_);
state->ogl_a = MAKE_PLATFORM_ALLOCATOR("ogl"_);
state->ogl = ogl_manager::make(&state->window, &state->ogl_a);
}
{ PROF_SCOPE("Init ImGui"_);
LOG_INFO("Setting up IMGUI..."_);
state->imgui_a = MAKE_PLATFORM_ALLOCATOR("imgui"_);
state->imgui = imgui_manager::make(&state->window, &state->imgui_a);
}
{ PROF_SCOPE("Init Game"_);
LOG_INFO("Setting up game..."_);
state->game_state = start_up_game(state);
}
{ PROF_SCOPE("Debug Settings"_);
state->dbg.store.add_var("window/settings"_, &state->window.settings);
state->dbg.store.add_ele("window/apply"_, FPTR(dbg_reup_window), state);
state->dbg.store.add_val("ogl/info"_, &state->ogl.info);
state->dbg.store.add_var("ogl/settings"_, &state->ogl.settings);
state->dbg.store.add_ele("ogl/apply"_, FPTR(ogl_apply), state);
}
LOG_INFO("Done with startup!"_);
LOG_POP_CONTEXT();
END_FRAME();
state->running = true;
return state;
}
DLL_EXPORT bool main_loop(engine* state) {
BEGIN_FRAME();
{PROF_SCOPE("Main Loop"_);
state->evt.run_events(state);
state->imgui.begin_frame(&state->window);
run_game(state->game_state);
state->dbg.UI(&state->window);
state->imgui.end_frame();
RESET_ARENA(&this_thread_data.scratch_arena);
{PROF_SCOPE("Swap Buffers"_);
CHECKED(swap_buffers, &state->window);
}
#ifndef RELEASE
{PROF_SCOPE("Reload Checks"_);
state->ogl.try_reload_programs();
if(state->apply_window_settings) {
if(global_api->apply_window_settings(&state->window)) {
state->imgui.gl_destroy();
state->ogl.gl_begin_reload();
global_api->recreate_window(&state->window);
state->ogl.gl_end_reload();
state->imgui.gl_load();
gl_reload_game(state->game_state);
}
state->apply_window_settings = false;
}
}
#endif
}
END_FRAME();
return state->running;
}
DLL_EXPORT void shut_down(engine* state) {
BEGIN_FRAME();
LOG_INFO("Beginning shutdown..."_);
LOG_DEBUG("Destroying game..."_);
shut_down_game(state->game_state);
LOG_DEBUG("Destroying IMGUI"_);
state->imgui.destroy();
LOG_DEBUG("Destroying OpenGL"_);
state->ogl.destroy();
LOG_DEBUG("Destroying window"_);
CHECKED(destroy_window, &state->window);
LOG_DEBUG("Destroying events"_);
state->evt.destroy();
LOG_DEBUG("Destroying debug system"_);
state->dbg.console.shutdown_log(&state->log);
state->dbg.destroy();
LOG_DEBUG("Done with shutdown!"_);
END_FRAME();
state->log.stop();
state->log.destroy();
state->dbg.destroy_prof();
cleanup_fptrs();
end_thread();
global_log = null;
global_dbg = null;
global_func = null;
global_api->heap_free(state);
global_api = null;
}
DLL_EXPORT void on_reload(platform_api* api, engine* state) {
global_api = api;
global_log = &state->log;
global_dbg = &state->dbg;
global_func = &state->func_state;
state->func_state.reload_all();
begin_thread("main"_, &state->basic_a);
state->dbg.profiler.register_thread(180);
state->ogl.load_global_funcs();
state->imgui.reload();
state->evt.start(); // NOTE(max): needed to reset platform function pointer pointing into the game DLL
state->log.start();
LOG_INFO("End reloading game code"_);
reload_game(state, state->game_state);
}
DLL_EXPORT void on_unload(engine* state) {
LOG_INFO("Begin reloading game code"_);
unload_game(state, state->game_state);
state->log.stop();
state->dbg.profiler.collate();
end_thread();
}