From e65c736d3e337f70d26baab6e12e9780c2464034 Mon Sep 17 00:00:00 2001 From: tovjemam Date: Sat, 20 Jun 2026 21:57:58 +0200 Subject: [PATCH] Add periodic update to emscripten version --- src/client/main.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/client/main.cpp b/src/client/main.cpp index d369ae8..b361582 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -377,9 +377,13 @@ static void WSClose() #endif /* EMSCRIPTEN */ +static bool can_update = false; +static Uint32 last_update = 0; + static void Frame() { Uint32 current_time = SDL_GetTicks(); + last_update = current_time; s_app->SetTime(current_time / 1000.0f); // Set time in seconds PollEvents(); @@ -409,6 +413,24 @@ static void Frame() SDL_GL_SwapWindow(s_window); } +#ifdef EMSCRIPTEN + +static void Update(void* args) +{ + + // std::cout << "update called" << std::endl; + if (can_update && (SDL_GetTicks() - last_update >= 100)) + { + // std::cout << "calling Frame()" << std::endl; + Frame(); + } + + emscripten_async_call(Update, nullptr, 100); + // std::cout << "update finished" << std::endl; +} + +#endif + static void Main() { if (s_url.empty()) s_url = WS_URL; @@ -430,13 +452,15 @@ static void Main() { SDL_SetRelativeMouseMode(SDL_TRUE); - s_app = std::make_unique(); s_app->SetUserName(s_username); s_app->AddChatMessagePrefix("WebSocket", "připojování na " + s_url); + can_update = true; + #ifdef EMSCRIPTEN - emscripten_set_main_loop(Frame, 0, true); + emscripten_set_main_loop(Frame, 0, false); + Update(nullptr); #else #ifdef _WIN32 @@ -512,4 +536,5 @@ int main(int argc, char *argv[]) } + #endif