poll workers for non-threaded support, emscripten main loop, remove twn_model.c

This commit is contained in:
veclavtalica
2025-02-21 21:16:15 +03:00
parent dc6b298532
commit d76ea06470
12 changed files with 105 additions and 289 deletions

View File

@ -16,7 +16,9 @@
#include <stdbool.h>
#include <limits.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define TICKS_PER_SECOND_DEFAULT 60
#define PACKAGE_EXTENSION "btw"
@ -24,7 +26,7 @@
static void pack_contents_modified(char const *path, enum FilewatchAction action);
#ifndef EMSCRIPTEN
#ifndef __EMSCRIPTEN__
static SDL_sem *opengl_load_semaphore;
#endif
@ -105,14 +107,20 @@ static void update_viewport(void) {
}
static void garbage_collect(void) {
file_read_garbage_collect();
}
static void main_loop(void) {
/*
if (!ctx.is_running) {
end(ctx);
clean_up(ctx);
#ifdef __EMSCRIPTEN__
if (!ctx.is_running)
emscripten_cancel_main_loop();
}
*/
#endif
/* dispatch all filewatch driven events, such as game object and asset pack reload */
filewatch_poll();
/* frame timer */
int64_t current_frame_time = SDL_GetPerformanceCounter();
@ -228,6 +236,8 @@ static void main_loop(void) {
ctx.game.initialization_needed = false;
}
workers_poll();
/* TODO: in some cases machine might want to assume frames will be fed as much as possible */
/* which for now is broken as glBufferData with NULL is used all over right after render */
if (frames != 0)
@ -236,6 +246,8 @@ static void main_loop(void) {
/* don't waste clock cycles on useless work */
/* TODO: make it adjustable from config */
SDL_Delay((uint32_t)(ctx.desired_frametime - ctx.frame_accumulator) / 1250000);
garbage_collect();
}
@ -425,7 +437,7 @@ static bool initialize(void) {
toml_datum_t datum_debug = toml_bool_in(game, "debug");
ctx.game.debug = datum_debug.ok ? datum_debug.u.b : true;
#ifndef EMSCRIPTEN
#ifndef __EMSCRIPTEN__
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
@ -445,6 +457,8 @@ static bool initialize(void) {
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
#endif
toml_datum_t datum_title = toml_string_in(about, "title");
@ -630,7 +644,7 @@ static bool initialize(void) {
profile_end("game object load");
/* delayed as further as possible so that more work is done before we have to wait */
#ifndef EMSCRIPTEN
#ifndef __EMSCRIPTEN__
SDL_SemWait(opengl_load_semaphore);
SDL_DestroySemaphore(opengl_load_semaphore);
profile_end("opengl loading");
@ -756,11 +770,6 @@ static bool try_mounting_root_pack(char *path) {
}
static void garbage_collect(void) {
file_read_garbage_collect();
}
int enter_loop(int argc, char **argv) {
profile_start("startup");
@ -771,7 +780,7 @@ int enter_loop(int argc, char **argv) {
}
profile_end("SDL initialization");
#ifndef EMSCRIPTEN
#ifndef __EMSCRIPTEN__
profile_start("opengl loading");
opengl_load_semaphore = SDL_CreateSemaphore(0);
SDL_Thread *opengl_load_thread = SDL_CreateThread(opengl_load_thread_fn, "opengl loader", opengl_load_semaphore);
@ -871,12 +880,12 @@ int enter_loop(int argc, char **argv) {
profile_end("startup");
while (ctx.is_running) {
/* dispatch all filewatch driven events, such as game object and asset pack reload */
filewatch_poll();
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(main_loop, ctx.desired_frametime, true);
#else
while (ctx.is_running)
main_loop();
garbage_collect();
}
#endif
if (ctx.game.debug)
profile_list_stats();