Skip to content

Commit

Permalink
Adjust SDL loop timing a bit
Browse files Browse the repository at this point in the history
Signal SDL loop every 10 ms but limit render to 20ms. Also call render
before update to match what stm32/pico do.
  • Loading branch information
Daft-Freak committed Feb 11, 2022
1 parent 8eded36 commit 416b0e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
37 changes: 22 additions & 15 deletions 32blit-sdl/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,12 @@ void System::run() {
}

int System::timer_thread() {
// Signal the system loop every 20 msec.
// Signal the system loop every 10 msec.
int dropped = 0;
SDL_Event event = {};
event.type = timer_event;

while (SDL_SemWaitTimeout(s_timer_stop, 20)) {
while (SDL_SemWaitTimeout(s_timer_stop, 10)) {
if (SDL_SemValue(s_loop_update)) {
dropped++;
if(dropped > 100) {
Expand Down Expand Up @@ -295,20 +295,27 @@ int System::update_thread() {
return 0;
}

void System::loop()
{
SDL_LockMutex(m_input);
blit::buttons = shadow_buttons;
blit::tilt.x = shadow_tilt[0];
blit::tilt.y = shadow_tilt[1];
blit::tilt.z = shadow_tilt[2];
blit::joystick.x = shadow_joystick[0];
blit::joystick.y = shadow_joystick[1];
SDL_UnlockMutex(m_input);
blit::tick(::now());
void System::loop() {
SDL_LockMutex(m_input);
blit::buttons = shadow_buttons;
blit::tilt.x = shadow_tilt[0];
blit::tilt.y = shadow_tilt[1];
blit::tilt.z = shadow_tilt[2];
blit::joystick.x = shadow_joystick[0];
blit::joystick.y = shadow_joystick[1];
SDL_UnlockMutex(m_input);

// only render at 50Hz
auto time_now = ::now();
if(time_now - last_render_time >= 20) {
blit::render(time_now);
last_render_time = time_now;
}

blit::tick(::now());
blit_input->rumble_controllers(blit::vibration);
blit::render(::now());
blit_multiplayer->update();

blit_multiplayer->update();
}

Uint32 System::mode() {
Expand Down
2 changes: 2 additions & 0 deletions 32blit-sdl/System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class System {

bool running = false;

Uint32 last_render_time = 0;

// shadow input
Uint32 shadow_buttons = 0;
float shadow_joystick[2] = {0, 0};
Expand Down

0 comments on commit 416b0e5

Please sign in to comment.