[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)
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()

View File

@ -22,8 +22,13 @@ static uint64_t g_wakeupTime;
void draw(Context *ctx);
ox::Error init(Context *ctx) noexcept {
oxReturnError(initGfx(ctx));
ox::Result<ox::UniquePtr<Context>> init(ox::UniquePtr<ox::FileSystem> fs, const char *appName) noexcept {
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);
}
@ -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<SdlImplData>();
id->running = false;
}
}

View File

@ -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;
};
}