[nostlagia/core/userland] Add running of event handler to main loop

This commit is contained in:
Gary Talent 2021-03-18 01:52:36 -05:00
parent 4956534af4
commit d34db292dc

View File

@ -17,6 +17,7 @@
namespace nostalgia::core { namespace nostalgia::core {
static event_handler g_eventHandler = nullptr; static event_handler g_eventHandler = nullptr;
static uint64_t g_wakeupTime;
void draw(Context *ctx); void draw(Context *ctx);
@ -27,6 +28,7 @@ ox::Error init(Context *ctx) {
ox::Error run(Context *ctx) { ox::Error run(Context *ctx) {
auto id = ctx->windowerData<SdlImplData>(); auto id = ctx->windowerData<SdlImplData>();
SDL_GL_SetSwapInterval(1);
for (auto running = true; running;) { for (auto running = true; running;) {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
@ -42,9 +44,17 @@ ox::Error run(Context *ctx) {
} }
} }
} }
const auto ticks = ticksMs();
if (g_wakeupTime <= ticks && g_eventHandler) {
auto sleepTime = g_eventHandler(ctx);
if (sleepTime >= 0) {
g_wakeupTime = ticks + static_cast<unsigned>(sleepTime);
} else {
g_wakeupTime = ~uint64_t(0);
}
}
draw(ctx); draw(ctx);
SDL_GL_SwapWindow(id->window); SDL_GL_SwapWindow(id->window);
SDL_Delay(1);
} }
return OxError(0); return OxError(0);
} }