Skip to content

Commit a26c452

Browse files
committed
switched to std::chrono for better frame throttling management
1 parent 6a95cb3 commit a26c452

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/drivers/dingux-sdl/dingoo.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
#include <windows.h>
5050
#endif
5151

52+
#include <chrono>
53+
#include <thread>
54+
5255
extern double g_fpsScale;
5356

5457
extern bool MaxSpeed;
@@ -330,20 +333,27 @@ static void DoFun(int fskip) {
330333
int32 ssize;
331334
extern uint8 PAL;
332335
int done = 0, timer = 0, ticks = 0, tick = 0, fps = 0;
333-
unsigned int frame_limit = 60, frametime = 16667;
336+
unsigned int frame_limit = 60;
337+
338+
namespace sc = std::chrono;
339+
using time_stamp = sc::time_point<sc::steady_clock, sc::microseconds>;
334340

335-
fpsthrottle = false;
341+
static constexpr auto frametime = sc::microseconds((uint64_t)(1000000 / 60.0f));
336342

337343
while (GameInfo) {
338-
int ticks = SDL_GetTicks();
344+
time_stamp s = sc::time_point_cast<sc::microseconds>(sc::steady_clock::now());
339345

340346
FCEUI_Emulate(&gfx, &sound, &ssize, 0);
341347
FCEUD_Update(gfx, sound, ssize);
342348

343-
int msec = SDL_GetTicks() - ticks;
349+
time_stamp e = sc::time_point_cast<sc::microseconds>(sc::steady_clock::now());
350+
351+
auto delta = e - s;
352+
353+
//printf("%d %d %d\n", delta.count(), (frametime - delta).count(), frametime.count());
344354

345-
if (msec < 16)
346-
SDL_Delay(16 - msec);
355+
if (delta < frametime)
356+
std::this_thread::sleep_for(frametime - delta);
347357
}
348358
}
349359

0 commit comments

Comments
 (0)