33 lines
681 B
C++
33 lines
681 B
C++
#include <deerith/core/tick.h>
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
namespace deerith
|
|
{
|
|
|
|
}
|
|
|
|
void deerith::wait_for_tick()
|
|
{
|
|
using clock = std::chrono::steady_clock;
|
|
|
|
static auto last_tick = clock::now();
|
|
|
|
constexpr auto tick_duration = std::chrono::milliseconds(20);
|
|
constexpr auto sleep_interval = std::chrono::milliseconds(1);
|
|
|
|
const auto target = last_tick + tick_duration;
|
|
|
|
while (clock::now() < target)
|
|
{
|
|
const auto remaining = target - clock::now();
|
|
|
|
if (remaining > sleep_interval)
|
|
std::this_thread::sleep_for(sleep_interval);
|
|
else
|
|
std::this_thread::yield();
|
|
}
|
|
|
|
last_tick = clock::now();
|
|
} |