[nostalgia/core/glfw] Move wakeup time to Context
This commit is contained in:
parent
f83e814806
commit
827ba7dcdb
@ -9,7 +9,6 @@
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include <nostalgia/core/config.hpp>
|
||||
#include <nostalgia/core/core.hpp>
|
||||
#include <nostalgia/core/gfx.hpp>
|
||||
#include <nostalgia/core/input.hpp>
|
||||
|
||||
@ -17,23 +16,10 @@
|
||||
|
||||
namespace nostalgia::core {
|
||||
|
||||
static event_handler g_eventHandler = nullptr;
|
||||
static uint64_t g_wakeupTime;
|
||||
|
||||
void draw(Context *ctx);
|
||||
|
||||
ox::Error init(Context *ctx) noexcept {
|
||||
const auto id = new GlfwImplData;
|
||||
ctx->setWindowerData(id);
|
||||
using namespace std::chrono;
|
||||
id->startTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
|
||||
glfwInit();
|
||||
oxReturnError(initGfx(ctx));
|
||||
return OxError(0);
|
||||
}
|
||||
void draw(Context *ctx) noexcept;
|
||||
|
||||
static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) {
|
||||
const auto ctx = ox::bit_cast<Context*>(glfwGetWindowUserPointer(window));
|
||||
const auto ctx = static_cast<Context*>(glfwGetWindowUserPointer(window));
|
||||
const auto id = ctx->windowerData<GlfwImplData>();
|
||||
switch (action) {
|
||||
case GLFW_PRESS:
|
||||
@ -46,6 +32,17 @@ static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int
|
||||
}
|
||||
}
|
||||
|
||||
ox::Error init(Context *ctx) noexcept {
|
||||
const auto id = new GlfwImplData;
|
||||
ctx->setWindowerData(id);
|
||||
using namespace std::chrono;
|
||||
id->startTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
|
||||
glfwInit();
|
||||
oxReturnError(initGfx(ctx));
|
||||
glfwSetKeyCallback(id->window, handleGlfwKeyEvent);
|
||||
return OxError(0);
|
||||
}
|
||||
|
||||
ox::Error run(Context *ctx) noexcept {
|
||||
const auto id = ctx->windowerData<GlfwImplData>();
|
||||
id->running = true;
|
||||
@ -54,16 +51,15 @@ ox::Error run(Context *ctx) noexcept {
|
||||
// oxTrace("nostalgia::core::sdl", "Could not enable adaptive vsync, falling back on vsync");
|
||||
// SDL_GL_SetSwapInterval(1); // fallback on normal vsync
|
||||
//}
|
||||
glfwSetKeyCallback(id->window, handleGlfwKeyEvent);
|
||||
while (id->running && !glfwWindowShouldClose(id->window)) {
|
||||
glfwPollEvents();
|
||||
const auto ticks = ticksMs(ctx);
|
||||
if (g_wakeupTime <= ticks && g_eventHandler) {
|
||||
auto sleepTime = g_eventHandler(ctx);
|
||||
if (id->wakeupTime <= ticks && id->eventHandler) {
|
||||
auto sleepTime = id->eventHandler(ctx);
|
||||
if (sleepTime >= 0) {
|
||||
g_wakeupTime = ticks + static_cast<unsigned>(sleepTime);
|
||||
id->wakeupTime = ticks + static_cast<unsigned>(sleepTime);
|
||||
} else {
|
||||
g_wakeupTime = ~uint64_t(0);
|
||||
id->wakeupTime = ~uint64_t(0);
|
||||
}
|
||||
}
|
||||
draw(ctx);
|
||||
|
@ -17,6 +17,7 @@ struct GlfwImplData {
|
||||
int64_t startTime = 0;
|
||||
bool running = false;
|
||||
event_handler eventHandler = nullptr;
|
||||
uint64_t wakeupTime = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user