module; #include #include module Core.Clock; import ErrorSimulator; using Core::Clock; Clock::Clock() noexcept : index(0), last(0), sum(0), time({}) { } i64 Clock::update() noexcept { i64 current = getNanos(); if(current < 0) { return current; } if(last == 0) { last = current; return 0; } index = (index + 1) & (time.getLength() - 1); sum -= time[index]; time[index] = current - last; sum += time[index]; last = current; return time[index]; } float Core::Clock::getUpdatesPerSecond() const noexcept { return (time.getLength() * 1000000000.0f) / static_cast(sum); } bool Clock::sleepNanos(i64 nanos) noexcept { try { debugThrow(); std::this_thread::sleep_for(std::chrono::nanoseconds(nanos)); } catch(...) { return true; } return false; } bool Clock::sleepMillis(i64 millis) noexcept { return sleepNanos(millis * 1'000'000l); } i64 Clock::getNanos(void) noexcept { try { debugThrow(); using namespace std::chrono; return duration_cast( high_resolution_clock::now().time_since_epoch()) .count(); } catch(...) { return -1; } }