-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpoggen.h
More file actions
271 lines (209 loc) · 8.53 KB
/
Copy pathpoggen.h
File metadata and controls
271 lines (209 loc) · 8.53 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#pragma once
#include <poglib/application.h>
#include "./poggen/scene.h"
#include "poglib/basic/arena.h"
#include "poglib/basic/concurrency.h"
#include "poglib/basic/ds/hashtable.h"
#include "poglib/ecs.h"
#include "poglib/input/commandqueue.h"
#include "poglib/physics/jolt-wrapper.h"
#include "poglib/pipeline/render/common.h"
#include "poglib/pipeline/render/render_queue.h"
#include "poglib/util/assetmanager.h"
/*=============================================================================
- GAME ENGINE -
==============================================================================*/
typedef struct {
bool enable_physics;
bool enable_ecs;
} poggen_config_t;
typedef struct {
bool phy_simulation_started;
physics_sys_jolt_t *instance;
} poggen__internal_physics_t;
typedef struct poggen_t {
poggen_config_t config;
hashtable_t scenes;
scene_t *current_scene;
bgtask_manager_t bg_task_manager;
arena_t arena;
struct {
application_t *app;
} handle;
struct {
poggen__internal_physics_t physics;
ecs_t ecs;
assetmanager_t assets;
renderqueue_t renderqueue;
} systems;
} poggen_t ;
global poggen_t *global_poggen = NULL;
#include "poglib/util/workbench.h"
poggen_t * poggen_init(application_t * const app, const poggen_config_t config);
#define poggen_add_scene(PGEN, SCENE_NAME) poggen__internal_add_scene((PGEN), scene__internal_init(SCENE_NAME))
void poggen_remove_scene(poggen_t *self, str_t label);
void poggen_change_scene(poggen_t *self, str_t scene_label);
void poggen_register_physics_rules(poggen_t * const self, const physics_sys_jolt_rules_config_t config);
commandqueue_t * poggen_get_scene_commandqueue(const poggen_t *const self);
window_t * poggen_get_window(const poggen_t *self);
physics_sys_jolt_event_queue_t * poggen_get_physics_collision_events(const poggen_t * const self);
ecs_t * poggen_get_ecs_handle(poggen_t * const self);
void poggen_tick(poggen_t *const self);
void poggen_update(poggen_t *self, const f32 dt);
void poggen_render(poggen_t *self, const f32 dt);
void poggen_destroy(poggen_t *self);
/*----------------------------------------------------------------------------*/
#ifndef IGNORE_POGGEN_IMPLEMENTATION
#define MAX_SCENES_ALLOWED 10
window_t * poggen_get_window(const poggen_t *self)
{
return application_get_window(self->handle.app);
}
poggen_t * poggen_init(application_t * const app, const poggen_config_t config)
{
if (!global_window) eprint("A window is required to run poggen\n");
if (global_poggen) eprint("Trying to initialize a second `poggen` in the same instance");
poggen_t *output = (poggen_t *)calloc(1, sizeof(poggen_t ));
ASSERT(output);
arena_t arena = arena_init(&app->handle.arena, 2 * MB);
*output = (poggen_t ){
.config = config,
.scenes = hashtable_init(MAX_SCENES_ALLOWED, HT_KEY_TYPE_STR, (ht_value_type){ .size = sizeof(scene_t), .type = HT_STORAGE_BY_REFERENCE }, &app->handle.arena),
.current_scene = NULL,
.arena = arena,
.bg_task_manager = bgtask_manager_init(),
.handle = {
.app = app
},
.systems = {
.assets = assetmanager_init(&output->bg_task_manager),
.renderqueue = renderqueue_init(),
.ecs = config.enable_ecs ? ecs_init() : (ecs_t){0},
.physics = config.enable_physics
? (poggen__internal_physics_t){
.phy_simulation_started = false,
.instance = physics_sys_jolt_init(&arena)
} : (poggen__internal_physics_t){0},
},
};
global_poggen = output;
//FIXME: this is odd, but dont know how to fix this otherwise
workbench_init(arena_store(&arena, &arena, sizeof(arena_t)));
return global_poggen;
}
void poggen__internal_add_scene(poggen_t *self, const scene_t scene_config)
{
assert(self);
scene_t * const scene = mem_init((scene_t *)&scene_config, sizeof(scene_t));
hashtable_insert(
&self->scenes,
(hashtable_key_t){scene_config.label},
scene
);
if (!self->current_scene)
self->current_scene = scene;
if (self->config.enable_physics && !self->systems.physics.phy_simulation_started) {
physics_sys_jolt_start_simulation(self->systems.physics.instance);
self->systems.physics.phy_simulation_started = true;
}
scene->__init(scene);
}
void poggen_remove_scene(poggen_t *self, const str_t label)
{
assert(self);
assert(label.len);
hashtable_delete(&self->scenes, (hashtable_key_t){label});
}
void poggen_change_scene(poggen_t *self, const str_t scene_label)
{
assert(self);
assert(scene_label.len);
const hashtable_t *table = &self->scenes;
scene_t *scene = (scene_t *)hashtable_get_value(table, (hashtable_key_t){scene_label});
assert(scene);
printf("SCENE UPDATED FROM (%s) TO (%s)\n", self->current_scene->label, scene->label);
self->current_scene = scene;
}
void poggen_render(poggen_t *self, const f32 dt)
{
ASSERT(self);
workbench_render();
self->current_scene->__render(self->current_scene, dt);
renderqueue_dispatch(&self->systems.renderqueue);
}
void poggen_tick(poggen_t *const self)
{
ASSERT(self);
ASSERT(self->current_scene);
workbench_tick();
scene__internal_tick(self->current_scene);
}
void poggen_update(poggen_t *self, const f32 dt)
{
assert(self);
renderqueue_flush(&self->systems.renderqueue);
if (self->config.enable_physics && self->systems.physics.instance && !self->systems.physics.phy_simulation_started)
eprint("Missed to register physics interaction rules, else DISABLE physics in config passed to engine");
assetmanager_update(&self->systems.assets);
bgtask_manager_run_all_tasks(&self->bg_task_manager);
scene_t *current_scene = self->current_scene;
if (current_scene == NULL) eprint("Current scene is null");
current_scene->__input(current_scene, dt);
if (self->systems.physics.phy_simulation_started)
physics_sys_jolt_update(self->systems.physics.instance, dt);
if (self->config.enable_ecs) {
ecs_update(&self->systems.ecs);
}
workbench_update(dt);
scene__internal_update(current_scene, dt);
}
void poggen_destroy(poggen_t *self)
{
assert(self);
assetmanager_destroy(&self->systems.assets);
hashtable_iterator(&self->scenes, tableentry) {
hashtable_entry_t *entry = tableentry;
__scene_destroy(entry->value);
mem_free(entry->value, sizeof(scene_t));
}
hashtable_destroy(&self->scenes);
bgtask_manager_destroy(&self->bg_task_manager);
if (self->config.enable_physics)
physics_sys_jolt_destroy(self->systems.physics.instance);
if (self->config.enable_ecs)
ecs_destroy(&self->systems.ecs);
workbench_destroy();
renderqueue_destroy(&self->systems.renderqueue);
arena_destroy(&self->arena);
self->current_scene = NULL;
free(self);
self = NULL;
global_poggen = NULL;
}
void poggen_register_physics_rules(poggen_t *const self, const physics_sys_jolt_rules_config_t config)
{
ASSERT(self->config.enable_physics);
ASSERT(self->systems.physics.instance);
physics_sys_jolt_set_interaction_rules(self->systems.physics.instance, config);
physics_sys_jolt_start_simulation(self->systems.physics.instance);
}
physics_sys_jolt_event_queue_t * poggen_get_physics_collision_events(const poggen_t * const self)
{
if (!self->config.enable_physics) {
eprint("Enable physics first, to use this");
}
ASSERT(self->systems.physics.instance);
return physics_sys_get_collision_event_queue(self->systems.physics.instance);
}
ecs_t * poggen_get_ecs_handle(poggen_t * const self)
{
ASSERT(self->config.enable_ecs);
return &self->systems.ecs;
}
commandqueue_t * poggen_get_scene_commandqueue(const poggen_t *const self)
{
ASSERT(self);
ASSERT(self->current_scene);
return &self->current_scene->commandqueue;
}
#endif