From d622ab380dc781f10a8b2e12b56e5e48dfade01d Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 4 Nov 2021 00:35:40 -0500 Subject: [PATCH] [nostalgia/core/sdl] Fix SDL implementation of Core --- src/nostalgia/core/sdl/CMakeLists.txt | 39 +++++++++++++++------------ src/nostalgia/core/sdl/core.cpp | 25 ++++++++++++----- src/nostalgia/core/sdl/core.hpp | 3 +++ 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/nostalgia/core/sdl/CMakeLists.txt b/src/nostalgia/core/sdl/CMakeLists.txt index d09bb976..1c47d389 100644 --- a/src/nostalgia/core/sdl/CMakeLists.txt +++ b/src/nostalgia/core/sdl/CMakeLists.txt @@ -1,21 +1,26 @@ -add_library( - NostalgiaCore-SDL - core.cpp - gfx.cpp -) - find_package(SDL2 REQUIRED) -target_link_libraries( - NostalgiaCore-SDL PUBLIC - SDL2::SDL2 - NostalgiaCore-Userspace -) +if(SDL2_FOUND) -install( - TARGETS + add_library( NostalgiaCore-SDL - DESTINATION - LIBRARY DESTINATION lib/nostalgia - ARCHIVE DESTINATION lib/nostalgia -) + core.cpp + gfx.cpp + ) + + target_link_libraries( + NostalgiaCore-SDL PUBLIC + SDL2::SDL2 + NostalgiaGlUtils + NostalgiaCore-Userspace + ) + + install( + TARGETS + NostalgiaCore-SDL + DESTINATION + LIBRARY DESTINATION lib/nostalgia + ARCHIVE DESTINATION lib/nostalgia + ) + +endif() \ No newline at end of file diff --git a/src/nostalgia/core/sdl/core.cpp b/src/nostalgia/core/sdl/core.cpp index 4626497c..89f4e20b 100644 --- a/src/nostalgia/core/sdl/core.cpp +++ b/src/nostalgia/core/sdl/core.cpp @@ -22,8 +22,13 @@ static uint64_t g_wakeupTime; void draw(Context *ctx); -ox::Error init(Context *ctx) noexcept { - oxReturnError(initGfx(ctx)); +ox::Result> init(ox::UniquePtr fs, const char *appName) noexcept { + auto ctx = ox::make_unique(); + ctx->rom = std::move(fs); + ctx->appName = appName; + const auto id = new SdlImplData; + ctx->setWindowerData(id); + oxReturnError(initGfx(ctx.get())); return OxError(0); } @@ -34,22 +39,23 @@ 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 } - for (auto running = true; running;) { + id->running = true; + while (id->running) { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_q) { - running = false; + id->running = false; } break; case SDL_QUIT: { - running = false; + id->running = false; break; } } } - const auto ticks = ticksMs(); + const auto ticks = ticksMs(ctx); if (g_wakeupTime <= ticks && g_eventHandler) { auto sleepTime = g_eventHandler(ctx); if (sleepTime >= 0) { @@ -61,6 +67,8 @@ ox::Error run(Context *ctx) noexcept { draw(ctx); SDL_GL_SwapWindow(id->window); } + ctx->setWindowerData(nullptr); + delete id; return OxError(0); } @@ -76,4 +84,9 @@ bool buttonDown(Key) noexcept { return false; } +void shutdown(Context *ctx) noexcept { + const auto id = ctx->windowerData(); + id->running = false; +} + } diff --git a/src/nostalgia/core/sdl/core.hpp b/src/nostalgia/core/sdl/core.hpp index 2ac3ab09..f6fa1369 100644 --- a/src/nostalgia/core/sdl/core.hpp +++ b/src/nostalgia/core/sdl/core.hpp @@ -17,6 +17,9 @@ namespace nostalgia::core { struct SdlImplData { SDL_Window *window = nullptr; SDL_GLContext renderer = nullptr; + int64_t startTime = 0; + uint64_t wakeupTime = 0; + bool running = false; }; }