Skip to content

Commit

Permalink
Better timing for windows
Browse files Browse the repository at this point in the history
Couldn't figure out that stuff for other compilers, OSes and architectures. sorry
  • Loading branch information
Spodi committed Sep 26, 2023
1 parent e5f6e7f commit b585968
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "gfx_screen_config.h"
#ifdef _WIN32
#include <WTypesbase.h>
#include <Windows.h>
#endif

#define GFX_BACKEND_NAME "SDL"
Expand Down Expand Up @@ -540,7 +541,11 @@ static inline void sync_framerate_with_timer(void) {
t = qpc_to_100ns(SDL_GetPerformanceCounter());

const int64_t next = previous_time + 10 * FRAME_INTERVAL_US_NUMERATOR / FRAME_INTERVAL_US_DENOMINATOR;
const int64_t left = next - t;
int64_t left = next - t;
#ifdef _WIN32
// We want to exit a bit early, so we can busy-wait the rest to never miss the deadline
left -= 15000UL;
#endif
if (left > 0) {
#ifndef _WIN32
const timespec spec = { 0, left * 100 };
Expand All @@ -554,6 +559,12 @@ static inline void sync_framerate_with_timer(void) {
#endif
}

#ifdef _WIN32
do {
YieldProcessor(); // TODO: Find a way for other compilers, OSes and architectures
t = qpc_to_100ns(SDL_GetPerformanceCounter());
} while (t < next);
#endif
t = qpc_to_100ns(SDL_GetPerformanceCounter());
if (left > 0 && t - next < 10000) {
// In case it takes some time for the application to wake up after sleep,
Expand Down

0 comments on commit b585968

Please sign in to comment.