Skip to content

Commit 89deb96

Browse files
committed
⚡ Interrupt sleep-until-input when stores are changed
fixes #35
1 parent 31e7257 commit 89deb96

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/windows/WindowsPlatform.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <shellapi.h>
3030
#include <shtypes.h>
3131

32+
#include "APILayerStore.hpp"
3233
#include "CheckForUpdates.hpp"
3334
#include "Config.hpp"
3435
#include "LoaderData.hpp"
@@ -204,6 +205,16 @@ void WindowsPlatform::GUIMain(const std::function<void()> drawFrame) {
204205
void WindowsPlatform::MainLoop(const std::function<void()>& drawFrame) {
205206
constexpr auto Interval
206207
= std::chrono::microseconds(1000000 / Config::MAX_FPS);
208+
209+
const wil::unique_event changeEvent(
210+
CreateEventW(nullptr, FALSE, TRUE, nullptr));
211+
const auto changeSubscriptions
212+
= APILayerStore::Get()
213+
| std::views::transform([e = changeEvent.get()](const auto store) {
214+
return store->OnChange(std::bind_front(&SetEvent, e));
215+
})
216+
| std::ranges::to<std::vector>();
217+
207218
while (true) {
208219
const auto earliestNextFrame = std::chrono::steady_clock::now() + Interval;
209220
MSG msg {};
@@ -216,11 +227,16 @@ void WindowsPlatform::MainLoop(const std::function<void()>& drawFrame) {
216227
return;
217228
}
218229

230+
ResetEvent(changeEvent.get());
231+
219232
this->BeforeFrame();
220233
drawFrame();
221234
this->AfterFrame();
222235

223-
WaitMessage();
236+
{
237+
const auto e = changeEvent.get();
238+
MsgWaitForMultipleObjects(1, &e, FALSE, INFINITE, QS_ALLINPUT);
239+
}
224240
const auto sleepFor = earliestNextFrame - std::chrono::steady_clock::now();
225241
if (sleepFor > std::chrono::steady_clock::duration::zero()) {
226242
std::this_thread::sleep_for(

0 commit comments

Comments
 (0)