[nostalgia/core/sdl] Fix SDL implementation of Core

This commit is contained in:
Gary Talent 2021-11-04 00:35:40 -05:00
parent f61efbafaf
commit d622ab380d
3 changed files with 44 additions and 23 deletions

View File

@ -1,21 +1,26 @@
add_library(
NostalgiaCore-SDL
core.cpp
gfx.cpp
)
find_package(SDL2 REQUIRED) find_package(SDL2 REQUIRED)
target_link_libraries( if(SDL2_FOUND)
NostalgiaCore-SDL PUBLIC
SDL2::SDL2
NostalgiaCore-Userspace
)
install( add_library(
TARGETS
NostalgiaCore-SDL NostalgiaCore-SDL
DESTINATION core.cpp
LIBRARY DESTINATION lib/nostalgia gfx.cpp
ARCHIVE DESTINATION lib/nostalgia )
)
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()

View File

@ -22,8 +22,13 @@ static uint64_t g_wakeupTime;
void draw(Context *ctx); void draw(Context *ctx);
ox::Error init(Context *ctx) noexcept { ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const char *appName) noexcept {
oxReturnError(initGfx(ctx)); auto ctx = ox::make_unique<Context>();
ctx->rom = std::move(fs);
ctx->appName = appName;
const auto id = new SdlImplData;
ctx->setWindowerData(id);
oxReturnError(initGfx(ctx.get()));
return OxError(0); 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"); oxTrace("nostalgia::core::sdl", "Could not enable adaptive vsync, falling back on vsync");
SDL_GL_SetSwapInterval(1); // fallback on normal vsync SDL_GL_SetSwapInterval(1); // fallback on normal vsync
} }
for (auto running = true; running;) { id->running = true;
while (id->running) {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_q) { if (event.key.keysym.sym == SDLK_q) {
running = false; id->running = false;
} }
break; break;
case SDL_QUIT: { case SDL_QUIT: {
running = false; id->running = false;
break; break;
} }
} }
} }
const auto ticks = ticksMs(); const auto ticks = ticksMs(ctx);
if (g_wakeupTime <= ticks && g_eventHandler) { if (g_wakeupTime <= ticks && g_eventHandler) {
auto sleepTime = g_eventHandler(ctx); auto sleepTime = g_eventHandler(ctx);
if (sleepTime >= 0) { if (sleepTime >= 0) {
@ -61,6 +67,8 @@ ox::Error run(Context *ctx) noexcept {
draw(ctx); draw(ctx);
SDL_GL_SwapWindow(id->window); SDL_GL_SwapWindow(id->window);
} }
ctx->setWindowerData(nullptr);
delete id;
return OxError(0); return OxError(0);
} }
@ -76,4 +84,9 @@ bool buttonDown(Key) noexcept {
return false; return false;
} }
void shutdown(Context *ctx) noexcept {
const auto id = ctx->windowerData<SdlImplData>();
id->running = false;
}
} }

View File

@ -17,6 +17,9 @@ namespace nostalgia::core {
struct SdlImplData { struct SdlImplData {
SDL_Window *window = nullptr; SDL_Window *window = nullptr;
SDL_GLContext renderer = nullptr; SDL_GLContext renderer = nullptr;
int64_t startTime = 0;
uint64_t wakeupTime = 0;
bool running = false;
}; };
} }