mirror of
https://github.com/TheBozos/BozoEngine.git
synced 2026-12-01 12:53:50 -10:00
FramePacer::frameEnd and the window's own present schedule slept with std::this_thread::sleep_until, which MinGW turns into nanosleep. Under Proton Experimental with fsync that oversleeps by up to 13 ms, so a game with 1 ms of work a frame ran its menus at 55 frames a second while the presents held 60. bozo::app::sleepUntil waits on a high-resolution waitable timer instead (a plain one where that kind is missing, then sleep_until); other systems keep sleep_until. PAL sc3.exe under Proton Experimental with fsync, menus: 58.6 -> 59.95 game frames a second (a standalone pacer loop: 55 -> 60.00). Co-Authored-By: Claude Opus 5.5 <[email protected]>
61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root).
|
|
#include "bozo/app/precise_sleep.h"
|
|
|
|
#include <thread>
|
|
|
|
#if defined(_WIN32)
|
|
#ifndef NOMINMAX
|
|
#define NOMINMAX
|
|
#endif
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#endif
|
|
#include <windows.h>
|
|
#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
|
|
#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
|
|
#endif
|
|
#endif
|
|
|
|
namespace bozo::app
|
|
{
|
|
#if defined(_WIN32)
|
|
namespace
|
|
{
|
|
struct Timer
|
|
{
|
|
HANDLE handle = nullptr;
|
|
Timer()
|
|
{
|
|
handle = CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
|
|
if (!handle)
|
|
handle = CreateWaitableTimerW(nullptr, FALSE, nullptr);
|
|
}
|
|
~Timer()
|
|
{
|
|
if (handle)
|
|
CloseHandle(handle);
|
|
}
|
|
Timer(const Timer &) = delete;
|
|
Timer &operator=(const Timer &) = delete;
|
|
};
|
|
}
|
|
|
|
void sleepUntil(std::chrono::steady_clock::time_point until)
|
|
{
|
|
const auto left = until - std::chrono::steady_clock::now();
|
|
if (left <= std::chrono::steady_clock::duration::zero())
|
|
return;
|
|
thread_local Timer timer;
|
|
LARGE_INTEGER due;
|
|
due.QuadPart = -static_cast<LONGLONG>(std::chrono::duration_cast<std::chrono::nanoseconds>(left).count() / 100);
|
|
if (timer.handle && due.QuadPart < 0 && SetWaitableTimer(timer.handle, &due, 0, nullptr, nullptr, FALSE) &&
|
|
WaitForSingleObject(timer.handle, INFINITE) == WAIT_OBJECT_0)
|
|
return;
|
|
std::this_thread::sleep_until(until);
|
|
}
|
|
#else
|
|
void sleepUntil(std::chrono::steady_clock::time_point until) { std::this_thread::sleep_until(until); }
|
|
#endif
|
|
}
|