jak-project/common/util/FrameLimiter.cpp
water111 73561f10a3
support c++ tools on macos (#2063)
Running reference tests/decompiler should now be possible on macos
(arm). Most of the changes were just cleaning up places where we were
sloppy with ifdefs, but there were two interesting ones:
- `Printer.cpp` was updated to not use a recursive function for printing
lists, to avoid stack overflow
- I replaced xxhash with another version of the same library that
supports arm (the one that comes in zstd). The interface is C instead of
C++ but it's not bad to use. I confirmed that the extractor succeeds on
jak 1 iso so it looks like this gives us the same results as the old
library.
2022-12-22 17:12:05 -05:00

82 lines
1.8 KiB
C++

#include "FrameLimiter.h"
#include <thread>
#include "common/common_types.h"
double FrameLimiter::round_to_nearest_60fps(double current) {
double one_frame = 1.f / 60.f;
int frames_missed = (current / one_frame); // rounds down
if (frames_missed > 4) {
frames_missed = 4;
}
return (frames_missed + 1) * one_frame;
}
#ifdef OS_POSIX
FrameLimiter::FrameLimiter() {}
FrameLimiter::~FrameLimiter() {}
void FrameLimiter::run(double target_fps,
bool experimental_accurate_lag,
bool do_sleeps,
double engine_time) {
double target_seconds;
if (experimental_accurate_lag) {
target_seconds = round_to_nearest_60fps(engine_time);
} else {
target_seconds = 1.f / target_fps;
}
double remaining_time = target_seconds - m_timer.getSeconds();
if (do_sleeps && remaining_time > 0.001) {
std::this_thread::sleep_for(std::chrono::microseconds(int((remaining_time - 0.001) * 1e6)));
}
while (remaining_time > 0) {
remaining_time = target_seconds - m_timer.getSeconds();
}
m_timer.start();
}
#else
#define NOMINMAX
#include <Windows.h>
FrameLimiter::FrameLimiter() {
timeBeginPeriod(1);
}
FrameLimiter::~FrameLimiter() {
timeEndPeriod(0);
}
void FrameLimiter::run(double target_fps,
bool experimental_accurate_lag,
bool do_sleeps,
double engine_time) {
double target_seconds;
if (experimental_accurate_lag) {
target_seconds = round_to_nearest_60fps(engine_time);
} else {
target_seconds = 1.f / target_fps;
}
double remaining_time = target_seconds - m_timer.getSeconds();
if (do_sleeps && remaining_time > 0.001) {
Sleep((remaining_time * 1000) - 1);
}
while (remaining_time > 0) {
remaining_time = target_seconds - m_timer.getSeconds();
}
m_timer.start();
}
#endif