Mesen-SX/Utilities/Timer.cpp

35 lines
694 B
C++
Raw Normal View History

2019-02-13 14:10:36 -05:00
#include "stdafx.h"
#include "Timer.h"
#include <thread>
#include <chrono>
2019-02-13 14:10:36 -05:00
using namespace std::chrono;
2019-02-13 14:10:36 -05:00
Timer::Timer()
2019-02-13 14:10:36 -05:00
{
Reset();
}
void Timer::Reset()
{
_start = high_resolution_clock::now();
2019-02-13 14:10:36 -05:00
}
double Timer::GetElapsedMS()
{
high_resolution_clock::time_point end = high_resolution_clock::now();
duration<double> span = duration_cast<duration<double>>(end - _start);
return span.count() * 1000.0;
2019-02-13 14:10:36 -05:00
}
void Timer::WaitUntil(double targetMillisecond)
2019-02-13 14:10:36 -05:00
{
if(targetMillisecond > 0) {
2019-02-13 14:10:36 -05:00
double elapsedTime = GetElapsedMS();
if(targetMillisecond - elapsedTime > 1) {
2019-02-13 14:10:36 -05:00
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>((int)(targetMillisecond - elapsedTime)));
}
}
}