Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust SDL loop timing a bit #752

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions 32blit-sdl/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,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 @@ -296,20 +296,31 @@ 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 (main loop runs at 100Hz)
// however, the emscripten loop (usually) runs at the display refresh rate
auto time_now = ::now();
#ifndef __EMSCRIPTEN__
if(time_now - last_render_time >= 20)
#endif
{
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