-
Notifications
You must be signed in to change notification settings - Fork 14
/
candle.c
585 lines (491 loc) · 11.6 KB
/
candle.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include "candle.h"
#include "utils/str.h"
#include <string.h>
#include <stdlib.h>
#include "systems/window.h"
#include "systems/render_device.h"
#include "systems/sauces.h"
#include "systems/nodegraph.h"
#include "components/name.h"
#include "components/node.h"
#include "utils/file.h"
#define GLFW_INCLUDE_NONE
#ifdef _WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#define _GLFW_WIN32
#else
#define _GLFW_X11
#endif
#include "third_party/glfw/include/GLFW/glfw3.h"
#ifdef _WIN32
#include "third_party/glfw/include/GLFW/glfw3native.h"
#endif
#ifndef _WIN32
#include <unistd.h>
#define candle_getcwd getcwd
#else
#include <direct.h>
#define candle_getcwd _getcwd
#endif
#ifdef THREADED
#include "third_party/tinycthread/source/tinycthread.h"
#endif
candle_t *g_candle;
entity_t candle_run_command_args(entity_t root, int argc, char **argv,
bool_t *handled)
{
entity_t instance = root;
uint32_t hash = ref(argv[0]);
khiter_t k = kh_get(cmd, g_candle->cmds, hash);
if(k != kh_end(g_candle->cmds))
{
cmd_t *cmd = &kh_value(g_candle->cmds, k);
instance = cmd->cb(instance, argc, argv);
if (handled) *handled = true;
}
return instance;
}
bool_t candle_utility(int argc, char **argv)
{
bool_t handled = false;
argc--;
argv++;
if (argc > 0)
{
candle_run_command_args(entity_null, argc, argv, &handled);
}
return handled;
}
void candle_reset_dir()
{
#ifdef WIN32
_chdir(g_candle->firstDir);
#else
if(chdir(g_candle->firstDir)) printf("Chdir failed.\n");
#endif
}
static void candle_handle_events(void)
{
entity_signal(entity_null, ref("events_begin"), NULL, NULL);
glfwPollEvents();
entity_signal(entity_null, ref("events_end"), NULL, NULL);
}
static int skip = 0;
void candle_skip_frame(int frames)
{
skip += frames;
}
bool_t is_render_thread()
{
#if defined(THREADED)
return thrd_equal(thrd_current(), *(thrd_t*)g_candle->render_thr);
#else
return true;
#endif
}
static void cursor_position_callback(GLFWwindow *window, double xpos, double ypos)
{
candle_event_t event;
event.type = CANDLE_MOUSEMOTION;
event.mouse.x = (int)xpos;
event.mouse.y = (int)ypos;
entity_signal(entity_null, ref("event_handle"), &event, NULL);
}
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
candle_event_t event;
if (action == GLFW_PRESS)
{
event.type = CANDLE_KEYDOWN;
}
else if (action == GLFW_RELEASE)
{
event.type = CANDLE_KEYUP;
}
else
{
return;
}
if (mods & GLFW_MOD_SUPER)
{
return;
}
if (!(mods & GLFW_MOD_SHIFT) && key >= 'A' && key <= 'Z')
{
key += 'a' - 'A';
}
event.key = key;
entity_signal(entity_null, ref("event_handle"), &event, NULL);
}
void window_resize_callback(GLFWwindow *window, int width, int height)
{
candle_event_t event;
event.type = CANDLE_WINDOW_RESIZE;
event.window.width = width;
event.window.height = height;
entity_signal(entity_null, ref("event_handle"), &event, NULL);
}
void joystick_callback(int jid, int event)
{
if (event == GLFW_CONNECTED)
{
printf("joystick connected\n");
}
else if (event == GLFW_DISCONNECTED)
{
printf("joystick disconnected\n");
}
}
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
{
candle_event_t event;
if (action == GLFW_PRESS)
{
event.type = CANDLE_MOUSEBUTTONDOWN;
}
else if (action == GLFW_RELEASE)
{
event.type = CANDLE_MOUSEBUTTONUP;
}
if (button == GLFW_MOUSE_BUTTON_LEFT)
{
event.key = CANDLE_MOUSE_BUTTON_LEFT;
}
else if (button == GLFW_MOUSE_BUTTON_RIGHT)
{
event.key = CANDLE_MOUSE_BUTTON_RIGHT;
}
else if (button == GLFW_MOUSE_BUTTON_MIDDLE)
{
event.key = CANDLE_MOUSE_BUTTON_MIDDLE;
}
entity_signal(entity_null, ref("event_handle"), &event, NULL);
}
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
candle_event_t event;
event.type = CANDLE_MOUSEWHEEL;
event.key = CANDLE_MOUSE_BUTTON_MIDDLE;
event.wheel.x = xoffset;
event.wheel.y = yoffset;
entity_signal(entity_null, ref("event_handle"), &event, NULL);
}
static void render_loop_init(void)
{
c_window_t *window;
g_candle->last_fps_tick = glfwGetTime();
g_candle->fps_count = 0;
entity_add_component(SYS, c_window_new(0, 0));
entity_add_component(SYS, c_render_device_new());
window = c_window(&SYS);
glfwSetMouseButtonCallback(window->window, mouse_button_callback);
glfwSetScrollCallback(window->window, scroll_callback);
glfwSetCursorPosCallback(window->window, cursor_position_callback);
glfwSetKeyCallback(window->window, key_callback);
glfwSetWindowSizeCallback(window->window, window_resize_callback);
glfwSetJoystickCallback(joystick_callback);
}
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#else
/* FAKE EMSCRIPTEN */
/* #define __EMSCRIPTEN__ */
/* void emscripten_set_main_loop(void(*cb)(void), int fps, bool_t somfin) */
/* { */
/* while(!g_candle->exit) */
/* { */
/* cb(); */
/* } */
/* } */
#endif
#ifndef __EMSCRIPTEN__
bool_t first_frame = true;
#endif
static void render_loop_tick(void)
{
double current;
#ifndef __EMSCRIPTEN__
if (first_frame)
{
c_window_lock_fps(c_window(&SYS), 1);
first_frame = false;
}
#endif
#ifdef __EMSCRIPTEN__ /* HACK: emscripten does not update events on glfwPollEvents*/
entity_signal(entity_null, ref("events_end"), NULL, NULL);
#else
candle_handle_events();
#endif
loader_update(g_candle->loader);
{
entity_signal(entity_null, ref("world_draw"), NULL, NULL);
entity_signal(entity_null, ref("world_draw_end"), NULL, NULL);
g_candle->fps_count++;
}
current = glfwGetTime();
if(current - g_candle->last_fps_tick > 1.0)
{
g_candle->fps = g_candle->fps_count;
g_candle->fps_count = 0;
/* printf("%d\n", g_candle->fps); */
g_candle->last_fps_tick = current;
}
#ifdef __EMSCRIPTEN__ /* HACK: emscripten does not update events on glfwPollEvents*/
entity_signal(entity_null, ref("events_begin"), NULL, NULL);
#endif
if (glfwWindowShouldClose(c_window(&SYS)->window))
{
g_candle->exit = true;
}
}
#if defined(THREADED) && !defined(__EMSCRIPTEN__)
static int render_loop(void *data)
{
render_loop_init();
mtx_unlock(g_candle->mtx);
while(!g_candle->exit)
{
render_loop_tick();
ecm_clean(0);
}
printf("Exiting render loop\n");
loader_update(g_candle->loader);
return 0;
}
#endif
static void ticker_loop_tick(bool_t delay)
{
double current;
float dt = 16.0f / 1000.0f;
/* float dt = (current - g_candle->last_update) / 1000.0; */
entity_signal(entity_null, ref("world_update"), &dt, NULL);
current = glfwGetTime();
if (delay && current - g_candle->last_update < 0.016)
{
#ifdef THREADED
struct timespec remaining = { 0, 0 };
remaining.tv_nsec = (0.016 - (current - g_candle->last_update)) * 1000000000;
thrd_sleep(&remaining, NULL);
g_candle->last_update += 0.016;
#endif
}
else
{
g_candle->last_update = current;
}
}
#ifdef THREADED
static int ticker_loop(void *data)
{
do
{
ticker_loop_tick(true);
ecm_clean(0);
}
while(!g_candle->exit);
printf("Exiting ticker loop\n");
return 0;
}
#endif
#ifdef __EMSCRIPTEN__
static void em_ticker_loop(void)
{
#ifndef THREADED
ticker_loop_tick(false);
ticker_loop_tick(false);
#endif
render_loop_tick();
ecm_clean(0);
}
#endif
void candle_wait(void)
{
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(em_ticker_loop, 0, true);
#elif !defined(THREADED)
do
{
ticker_loop_tick(false);
render_loop_tick();
ecm_clean(0);
}
while(!g_candle->exit);
#endif
#ifdef THREADED
#ifndef __EMSCRIPTEN__
thrd_join(*(thrd_t*)g_candle->render_thr, NULL);
#endif
thrd_join(*(thrd_t*)g_candle->ticker_thr, NULL);
#endif
ecm_clean(1);
}
void candle_reg_cmd(const char *key, cmd_cb cb)
{
int ret;
uint32_t hash = ref(key);
khiter_t k = kh_put(cmd, g_candle->cmds, hash, &ret);
cmd_t *cmd = &kh_value(g_candle->cmds, k);
cmd->cb = cb;
strncpy(cmd->key, key, sizeof(cmd->key) - 1);
}
entity_t candle_run_command(entity_t root, const char *command)
{
entity_t instance;
char *copy;
char separators[] = " ";
char *p;
char *argv[64];
int argc;
if(command[0] == '\0') return root;
copy = malloc(strlen(command) + 1);
strcpy(copy, command);
instance = root;
p = strtok(copy, separators);
argc = 0;
argv[argc++] = p;
while((p = strtok(NULL, separators)) != NULL)
{
argv[argc++] = p;
}
instance = candle_run_command_args(root, argc, argv, NULL);
free(copy);
if(!entity_exists(instance)) instance = root;
return instance;
}
int candle_run_from_memory(entity_t root, const char *bytes, size_t bytes_num)
{
char *line;
sfile_t *file = sopen(bytes, bytes_num);
if (file == NULL) return 0;
while ((line = sgets(file)))
{
entity_t entity;
entity = candle_run_command(root, line);
if(entity_exists(root) && c_node(&root) && entity != root)
{
c_node_add(c_node(&root), 1, entity);
}
}
sclose(file);
return 1;
}
int candle_run(entity_t root, const char *map_name)
{
char *line;
FILE *file = fopen(map_name, "r");
if (file == NULL) return 0;
line = NULL;
while (true)
{
entity_t entity;
line = str_readline(file);
if (str_len(line) == 0)
{
str_free(line);
break;
}
entity = candle_run_command(root, line);
if(entity_exists(root) && c_node(&root) && entity != root)
{
c_node_add(c_node(&root), 1, entity);
}
str_free(line);
}
fclose(file);
return 1;
}
static
entity_t _c_new(entity_t root, int argc, char **argv)
{
printf("CREATING NEW COMPONENT %s\n", argv[1]);
return entity_null;
}
#if defined(_WIN32)
static bool_t get_archive(unsigned char **bytes, size_t *bytes_num)
{
HRSRC hRes = FindResource(0, MAKEINTRESOURCE(1), RT_RCDATA);
HGLOBAL hMem = LoadResource(0, hRes);
DWORD size = SizeofResource(0, hRes);
*bytes = (unsigned char*)LockResource(hMem);
*bytes_num = size;
return true;
}
#elif defined(__EMSCRIPTEN__)
#else
extern uint8_t _binary_candle_build_data_zip_start[];
extern uint8_t _binary_candle_build_data_zip_end[];
static bool_t get_archive(unsigned char **bytes, size_t *bytes_num)
{
*bytes = (unsigned char*)_binary_candle_build_data_zip_start;
*bytes_num = (size_t)( _binary_candle_build_data_zip_end
- _binary_candle_build_data_zip_start);
return true;
}
#endif
void candle_init(const char *path)
{
size_t sauces_size;
unsigned char *sauces_bytes;
g_candle = calloc(1, sizeof *g_candle);
g_candle->loader = loader_new();
loader_start(g_candle->loader);
{
char *slash;
strcpy(g_candle->firstDir, path);
slash = strrchr(g_candle->firstDir, '/');
if (slash)
{
slash[0] = '\0';
candle_reset_dir();
}
else
{
g_candle->firstDir[0] = '\0';
}
}
materials_init_vil();
ecm_init();
entity_new({});
g_candle->cmds = kh_init(cmd);
candle_reg_cmd("c_new", (cmd_cb)_c_new);
shaders_reg();
draw_groups_init();
if(c_name(&SYS)) return;
entity_add_component(SYS, c_name_new("Candle"));
entity_add_component(SYS, c_nodegraph_new());
entity_add_component(SYS, c_mouse_new());
entity_add_component(SYS, c_keyboard_new());
#ifndef __EMSCRIPTEN__
entity_add_component(SYS, c_controllers_new());
#endif
entity_add_component(SYS, c_sauces_new());
entity_add_component(SYS, c_node_new());
textures_reg();
meshes_reg();
materials_reg();
#ifndef __EMSCRIPTEN__
if (get_archive(&sauces_bytes, &sauces_size))
{
c_sauces_me_a_zip(c_sauces(&SYS), sauces_bytes, sauces_size);
}
#else
c_sauces_index_dir(c_sauces(&SYS), "resauces");
#endif
#ifdef THREADED
g_candle->mtx = malloc(sizeof(mtx_t));
g_candle->render_thr = malloc(sizeof(thrd_t));
g_candle->ticker_thr = malloc(sizeof(thrd_t));
thrd_create(g_candle->ticker_thr, ticker_loop, NULL);
#ifndef __EMSCRIPTEN__
mtx_init(g_candle->mtx, mtx_plain);
mtx_lock(g_candle->mtx);
thrd_create(g_candle->render_thr, render_loop, NULL);
mtx_lock(g_candle->mtx);
#else
*(thrd_t*)g_candle->render_thr = thrd_current();
render_loop_init();
#endif
#else
render_loop_init();
#endif
}