SDL: More refactoring
Jeffrey Pfau jeffrey@endrift.com
Tue, 19 Jan 2016 23:13:51 -0800
2 files changed,
58 insertions(+),
44 deletions(-)
M
src/platform/sdl/gl-sdl.c
→
src/platform/sdl/gl-sdl.c
@@ -111,13 +111,32 @@ void mSDLGLRunloopGB(struct mSDLRenderer* renderer, void* user) {
struct GB* gb = user; SDL_Event event; struct VideoBackend* v = &renderer->gl.d; + int activeKeys = 0; + gb->keySource = &activeKeys; while (true) { int64_t frameCounter = gb->video.frameCounter; while (gb->video.frameCounter == frameCounter) { - LR35902Tick(gb->cpu); + LR35902Tick(gb->cpu); } while (SDL_PollEvent(&event)) { + // TODO: Refactor out + if (event.type == SDL_KEYUP || event.type == SDL_KEYDOWN) { + int key; +#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0) + key = GBAInputMapKey(renderer->player.bindings, SDL_BINDING_KEY, event.key.keysym.scancode); +#else + key = GBAInputMapKey(renderer->player.bindings, SDL_BINDING_KEY, event.key.keysym.sym); +#endif + if (key != GBA_KEY_NONE) { + if (event.type == SDL_KEYDOWN) { + activeKeys |= 1 << key; + } else { + activeKeys &= ~(1 << key); + } + } + } + #if SDL_VERSION_ATLEAST(2, 0, 0) // Event handling can change the size of the screen if (renderer->player.windowUpdated) {
M
src/platform/sdl/main.c
→
src/platform/sdl/main.c
@@ -47,7 +47,7 @@ static void mSDLDeinit(struct mSDLRenderer* renderer);
// TODO: Clean up signatures #ifdef M_CORE_GBA -static int mSDLRunGBA(struct mSDLRenderer* renderer, struct GBAArguments* args, struct GBAOptions* opts, struct GBAConfig* config, struct GBAInputMap* inputMap); +static int mSDLRunGBA(struct mSDLRenderer* renderer, struct GBAArguments* args, struct GBAOptions* opts, struct GBAConfig* config); #endif #ifdef M_CORE_GB static int mSDLRunGB(struct mSDLRenderer* renderer, struct GBAArguments* args);@@ -116,6 +116,14 @@ }
if (!opts.height) { opts.height = VIDEO_VERTICAL_PIXELS; } + GBAVideoSoftwareRendererCreate(&renderer.d); +#ifdef BUILD_GL + mSDLGLCreate(&renderer); +#elif defined(BUILD_GLES2) || defined(USE_EPOXY) + mSDLGLES2Create(&renderer); +#else + mSDLSWCreate(&renderer); +#endif } #endif #ifdef M_CORE_GB@@ -127,6 +135,14 @@ }
if (!opts.height) { opts.height = /*GB_*/VIDEO_VERTICAL_PIXELS; } + GBVideoSoftwareRendererCreate(&renderer.gb); +#ifdef BUILD_GL + mSDLGLCreateGB(&renderer); +#elif defined(BUILD_GLES2) || defined(USE_EPOXY) + mSDLGLES2CreateGB(&renderer); +#else + mSDLSWCreateGB(&renderer); +#endif } #endif else {@@ -157,11 +173,25 @@
renderer.lockAspectRatio = opts.lockAspectRatio; renderer.filter = opts.resampleVideo; + if (!mSDLInit(&renderer)) { + freeArguments(&args); + GBAConfigFreeOpts(&opts); + GBAConfigDeinit(&config); + return 1; + } + + renderer.player.bindings = &inputMap; + GBASDLInitBindings(&inputMap); + GBASDLInitEvents(&renderer.events); + GBASDLEventsLoadConfig(&renderer.events, GBAConfigGetInput(&config)); + GBASDLAttachPlayer(&renderer.events, &renderer.player); + GBASDLPlayerLoadConfig(&renderer.player, GBAConfigGetInput(&config)); + int ret; switch (platform) { case PLATFORM_GBA: - ret = mSDLRunGBA(&renderer, &args, &opts, &config, &inputMap); + ret = mSDLRunGBA(&renderer, &args, &opts, &config); break; case PLATFORM_GB: ret = mSDLRunGB(&renderer, &args);@@ -170,6 +200,10 @@ default:
ret = 1; break; } + GBASDLDetachPlayer(&renderer.events, &renderer.player); + GBAInputMapDeinit(&inputMap); + + mSDLDeinit(&renderer); freeArguments(&args); GBAConfigFreeOpts(&opts);@@ -179,20 +213,7 @@ return ret;
} #ifdef M_CORE_GBA -int mSDLRunGBA(struct mSDLRenderer* renderer, struct GBAArguments* args, struct GBAOptions* opts, struct GBAConfig* config, struct GBAInputMap* inputMap) { - GBAVideoSoftwareRendererCreate(&renderer->d); -#ifdef BUILD_GL - mSDLGLCreate(renderer); -#elif defined(BUILD_GLES2) || defined(USE_EPOXY) - mSDLGLES2Create(renderer); -#else - mSDLSWCreate(renderer); -#endif - - if (!mSDLInit(renderer)) { - return 1; - } - +int mSDLRunGBA(struct mSDLRenderer* renderer, struct GBAArguments* args, struct GBAOptions* opts, struct GBAConfig* config) { struct GBAThread context = { .renderer = &renderer->d.d, .userData = renderer@@ -202,6 +223,7 @@ context.debugger = createDebugger(args, &context);
GBAMapOptionsToContext(opts, &context); GBAMapArgumentsToContext(args, &context); + context.overrides = GBAConfigGetOverrides(config); bool didFail = false;@@ -214,14 +236,6 @@ if (!GBASDLInitAudio(&renderer->audio, &context)) {
didFail = true; } - renderer->player.bindings = inputMap; - GBASDLInitBindings(inputMap); - GBASDLInitEvents(&renderer->events); - GBASDLEventsLoadConfig(&renderer->events, GBAConfigGetInput(config)); - GBASDLAttachPlayer(&renderer->events, &renderer->player); - GBASDLPlayerLoadConfig(&renderer->player, GBAConfigGetInput(config)); - context.overrides = GBAConfigGetOverrides(config); - if (!didFail) { #if SDL_VERSION_ATLEAST(2, 0, 0) GBASDLSetScreensaverSuspendable(&renderer->events, opts->suspendScreensaver);@@ -247,10 +261,6 @@ }
} free(context.debugger); GBADirectorySetDeinit(&context.dirs); - GBASDLDetachPlayer(&renderer->events, &renderer->player); - GBAInputMapDeinit(inputMap); - - mSDLDeinit(renderer); return didFail; }@@ -258,19 +268,6 @@ #endif
#ifdef M_CORE_GB int mSDLRunGB(struct mSDLRenderer* renderer, struct GBAArguments* args) { - GBVideoSoftwareRendererCreate(&renderer->gb); -#ifdef BUILD_GL - mSDLGLCreateGB(renderer); -#elif defined(BUILD_GLES2) - mSDLGLES2CreateGB(renderer); -#else - mSDLSWCreateGB(renderer); -#endif - - if (!mSDLInit(renderer)) { - return 1; - } - struct LR35902Core cpu; struct GB gb;@@ -285,8 +282,6 @@
LR35902Reset(&cpu); renderer->runloop(renderer, &gb); vf->close(vf); - - mSDLDeinit(renderer); return 0; } #endif