From 1963776c3e6ac80246e149688886e83248dd44a1 Mon Sep 17 00:00:00 2001 From: Charlie Birks Date: Wed, 2 Oct 2024 23:09:13 +0100 Subject: [PATCH] sdl: copy/present only after rendering --- 32blit-sdl/System.cpp | 17 +++++++++++++---- 32blit-sdl/System.hpp | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/32blit-sdl/System.cpp b/32blit-sdl/System.cpp index d005b5a48..827803e3b 100644 --- a/32blit-sdl/System.cpp +++ b/32blit-sdl/System.cpp @@ -295,16 +295,19 @@ int System::update_thread() { while (true) { SDL_SemWait(s_loop_update); if(!running) break; - loop(); + bool rendered = loop(); if(!running) break; - SDL_PushEvent(&event); - SDL_SemWait(s_loop_redraw); + if(rendered) { + // present on main thread if we need to + SDL_PushEvent(&event); + SDL_SemWait(s_loop_redraw); + } } SDL_SemPost(s_loop_ended); return 0; } -void System::loop() { +bool System::loop() { SDL_LockMutex(m_input); blit::buttons = shadow_buttons; blit::tilt.x = shadow_tilt[0]; @@ -314,6 +317,8 @@ void System::loop() { blit::joystick.y = shadow_joystick[1]; SDL_UnlockMutex(m_input); + bool rendered = false; + // only render at 50Hz (main loop runs at 100Hz) // however, the emscripten loop (usually) runs at the display refresh rate auto time_now = ::now(); @@ -328,12 +333,16 @@ void System::loop() { _mode = requested_mode; cur_format = requested_format; } + + rendered = true; } blit::tick(::now()); blit_input->rumble_controllers(blit::vibration); blit_multiplayer->update(); + + return rendered; } Uint32 System::mode() { diff --git a/32blit-sdl/System.hpp b/32blit-sdl/System.hpp index a8149c9c3..3a9c3921a 100644 --- a/32blit-sdl/System.hpp +++ b/32blit-sdl/System.hpp @@ -18,7 +18,7 @@ class System { int update_thread(); int timer_thread(); - void loop(); + bool loop(); Uint32 mode(); Uint32 format();