mirror of
https://github.com/ZDoom/Raze.git
synced 2024-11-15 17:01:28 +00:00
8b43ed5777
git-svn-id: https://svn.eduke32.com/eduke32@8169 1a8010ca-5511-0410-912e-c29ae57300e0 # Conflicts: # source/build/src/timer.cpp # source/build/src/winlayer.cpp
65 lines
1.9 KiB
C++
65 lines
1.9 KiB
C++
// Build engine timer stuff
|
|
|
|
#include "timer.h"
|
|
|
|
#include "build.h"
|
|
#include "compat.h"
|
|
|
|
#include <chrono>
|
|
|
|
using namespace std;
|
|
using namespace chrono;
|
|
|
|
EDUKE32_STATIC_ASSERT((steady_clock::period::den/steady_clock::period::num) >= 1000000000);
|
|
|
|
static time_point<steady_clock> timerlastsample;
|
|
static int timerticspersec;
|
|
static void(*usertimercallback)(void) = NULL;
|
|
|
|
int timerGetClockRate(void) { return timerticspersec; }
|
|
uint32_t timerGetTicks(void) { return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); }
|
|
uint64_t timerGetTicksU64(void) { return steady_clock::now().time_since_epoch().count() * steady_clock::period::num; }
|
|
uint64_t timerGetFreqU64(void) { return steady_clock::period::den; }
|
|
|
|
// Returns the time since an unspecified starting time in milliseconds.
|
|
// (May be not monotonic for certain configurations.)
|
|
double timerGetHiTicks(void) { return duration<double, nano>(steady_clock::now().time_since_epoch()).count() / 1000000.0; }
|
|
|
|
int timerInit(int const tickspersecond)
|
|
{
|
|
timerticspersec = tickspersecond;
|
|
timerlastsample = steady_clock::now();
|
|
|
|
usertimercallback = NULL;
|
|
|
|
return 0;
|
|
}
|
|
|
|
ATTRIBUTE((flatten)) void timerUpdateClock(void)
|
|
{
|
|
auto time = steady_clock::now();
|
|
auto elapsedTime = time - timerlastsample;
|
|
|
|
uint64_t numerator = (elapsedTime.count() * (uint64_t) timerticspersec * steady_clock::period::num);
|
|
uint64_t freq = timerGetFreqU64();
|
|
int n = tabledivide64(numerator, freq);
|
|
totalclock.setFraction(tabledivide64((numerator - n*timerGetFreqU64()) * 65536, freq));
|
|
|
|
if (n <= 0) return;
|
|
|
|
totalclock += n;
|
|
timerlastsample += n*nanoseconds(1000000000/timerticspersec);
|
|
|
|
if (usertimercallback)
|
|
for (; n > 0; n--) usertimercallback();
|
|
}
|
|
|
|
void(*timerSetCallback(void(*callback)(void)))(void)
|
|
{
|
|
void(*oldtimercallback)(void);
|
|
|
|
oldtimercallback = usertimercallback;
|
|
usertimercallback = callback;
|
|
|
|
return oldtimercallback;
|
|
}
|