Skip to content

Commit

Permalink
Merge pull request #862 from 32blit/patch-sdl-present
Browse files Browse the repository at this point in the history
sdl: copy/present only after rendering
  • Loading branch information
Daft-Freak authored Oct 16, 2024
2 parents 51674f2 + b1af1eb commit add1351
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
43 changes: 26 additions & 17 deletions 32blit-sdl/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,28 @@ int System::timer_thread() {
}

int System::update_thread() {
// Run the blit user code once every time we are signalled.
SDL_Event event = {};
event.type = loop_event;

::init(); // Run init here because the user can make it hang.

while (true) {
SDL_SemWait(s_loop_update);
if(!running) break;
loop();
if(!running) break;
SDL_PushEvent(&event);
SDL_SemWait(s_loop_redraw);
}
SDL_SemPost(s_loop_ended);
return 0;
// Run the blit user code once every time we are signalled.
SDL_Event event = {};
event.type = loop_event;

::init(); // Run init here because the user can make it hang.

while (true) {
SDL_SemWait(s_loop_update);
if(!running) break;
bool rendered = loop();
if(!running) break;
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];
Expand All @@ -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();
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion 32blit-sdl/System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class System {
int update_thread();
int timer_thread();

void loop();
bool loop();

Uint32 mode();
Uint32 format();
Expand Down

0 comments on commit add1351

Please sign in to comment.