SDL: More video refactoring, fix scaling and aspect ratio locking
Jeffrey Pfau jeffrey@endrift.com
Fri, 05 Feb 2016 00:11:24 -0800
6 files changed,
50 insertions(+),
97 deletions(-)
M
src/gb/core.c
→
src/gb/core.c
@@ -34,7 +34,6 @@ LR35902SetComponents(cpu, &gb->d, 0, 0);
LR35902Init(cpu); GBVideoSoftwareRendererCreate(&gbcore->renderer); - GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d); gb->keySource = &gbcore->keys;@@ -110,6 +109,9 @@ return GBUnloadROM(core->board);
} static void _GBCoreReset(struct mCore* core) { + struct GBCore* gbcore = (struct GBCore*) core; + struct GB* gb = (struct GB*) core->board; + GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d); LR35902Reset(core->cpu); }@@ -184,6 +186,7 @@
struct mCore* GBCoreCreate(void) { struct GBCore* gbcore = malloc(sizeof(*gbcore)); struct mCore* core = &gbcore->d; + memset(&core->opts, 0, sizeof(core->opts)); core->cpu = 0; core->board = 0; core->init = _GBCoreInit;
M
src/gba/core.c
→
src/gba/core.c
@@ -34,15 +34,12 @@ }
core->cpu = cpu; core->board = gba; - memset(&core->opts, 0, sizeof(core->opts)); - GBACreate(gba); // TODO: Restore debugger and cheats ARMSetComponents(cpu, &gba->d, 0, 0); ARMInit(cpu); GBAVideoSoftwareRendererCreate(&gbacore->renderer); - GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d); gba->keySource = &gbacore->keys;@@ -140,7 +137,9 @@ return GBAUnloadROM(core->board);
} static void _GBACoreReset(struct mCore* core) { + struct GBACore* gbacore = (struct GBACore*) core; struct GBA* gba = (struct GBA*) core->board; + GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d); ARMReset(core->cpu); if (core->opts.skipBios) { GBASkipBIOS(core->board);@@ -220,6 +219,7 @@
struct mCore* GBACoreCreate(void) { struct GBACore* gbacore = malloc(sizeof(*gbacore)); struct mCore* core = &gbacore->d; + memset(&core->opts, 0, sizeof(core->opts)); core->cpu = 0; core->board = 0; core->init = _GBACoreInit;
M
src/platform/opengl/gl.c
→
src/platform/opengl/gl.c
@@ -54,10 +54,10 @@ static void GBAGLContextResized(struct VideoBackend* v, unsigned w, unsigned h) {
unsigned drawW = w; unsigned drawH = h; if (v->lockAspectRatio) { - if (w * 2 > h * 3) { - drawW = h * 3 / 2; - } else if (w * 2 < h * 3) { - drawH = w * 2 / 3; + if (w * v->height > h * v->width) { + drawW = h * v->width / v->height; + } else if (w * v->height < h * v->width) { + drawH = w * v->height / v->width; } } glMatrixMode(GL_MODELVIEW);
M
src/platform/sdl/gl-sdl.c
→
src/platform/sdl/gl-sdl.c
@@ -20,72 +20,33 @@ v->swap(v);
v->clear(v); } -#ifdef M_CORE_GBA -#include "gba/gba.h" - -static bool mSDLGLInitGBA(struct mSDLRenderer* renderer); -#endif -#ifdef M_CORE_GB -#include "gb/gb.h" - -static bool mSDLGLInitGB(struct mSDLRenderer* renderer); -#endif - +static bool mSDLGLInit(struct mSDLRenderer* renderer); static void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user); static void mSDLGLDeinit(struct mSDLRenderer* renderer); -#ifdef M_CORE_GBA -void mSDLGLCreateGBA(struct mSDLRenderer* renderer) { - renderer->init = mSDLGLInitGBA; +void mSDLGLCreate(struct mSDLRenderer* renderer) { + renderer->init = mSDLGLInit; renderer->deinit = mSDLGLDeinit; renderer->runloop = mSDLGLRunloop; } -bool mSDLGLInitGBA(struct mSDLRenderer* renderer) { +bool mSDLGLInit(struct mSDLRenderer* renderer) { mSDLGLCommonInit(renderer); - renderer->outputBuffer = malloc(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); - memset(renderer->outputBuffer, 0, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); - renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, VIDEO_HORIZONTAL_PIXELS); + renderer->outputBuffer = malloc(renderer->width * renderer->height * BYTES_PER_PIXEL); + memset(renderer->outputBuffer, 0, renderer->width * renderer->height * BYTES_PER_PIXEL); + renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width); GBAGLContextCreate(&renderer->gl); renderer->gl.d.user = renderer; renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio; renderer->gl.d.filter = renderer->filter; renderer->gl.d.swap = mSDLGLCommonSwap; - renderer->gl.d.init(&renderer->gl.d, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 0); + renderer->gl.d.init(&renderer->gl.d, renderer->width, renderer->height, 0); _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d); return true; } -#endif - -#ifdef M_CORE_GB -void mSDLGLCreateGB(struct mSDLRenderer* renderer) { - renderer->init = mSDLGLInitGB; - renderer->deinit = mSDLGLDeinit; - renderer->runloop = mSDLGLRunloop; -} - -bool mSDLGLInitGB(struct mSDLRenderer* renderer) { - mSDLGLCommonInit(renderer); - - // TODO: Pass texture size along - renderer->outputBuffer = malloc(GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); - memset(renderer->outputBuffer, 0, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); - renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, GB_VIDEO_HORIZONTAL_PIXELS); - - GBAGLContextCreate(&renderer->gl); - renderer->gl.d.user = renderer; - renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio; - renderer->gl.d.filter = renderer->filter; - renderer->gl.d.swap = mSDLGLCommonSwap; - renderer->gl.d.init(&renderer->gl.d, GB_VIDEO_HORIZONTAL_PIXELS, GB_VIDEO_VERTICAL_PIXELS, 0); - - _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d); - return true; -} -#endif void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) { struct mCoreThread* context = user;
M
src/platform/sdl/main.c
→
src/platform/sdl/main.c
@@ -57,8 +57,6 @@ int main(int argc, char** argv) {
struct mSDLRenderer renderer = {}; struct mCoreOptions opts = { - .width = 0, - .height = 0, .useBios = true, .rewindEnable = true, .audioBuffers = 512,@@ -75,12 +73,12 @@ initParserForGraphics(&subparser, &graphicsOpts);
bool parsed = parseArguments(&args, argc, argv, &subparser); if (!parsed || args.showHelp) { usage(argv[0], subparser.usage); - mCoreConfigFreeOpts(&opts); + freeArguments(&args); return !parsed; } if (args.showVersion) { version(argv[0]); - mCoreConfigFreeOpts(&opts); + freeArguments(&args); return 0; }@@ -91,21 +89,16 @@ struct VFile* vf = VFileOpen(args.fname, O_RDONLY);
if (!vf) { printf("Could not open game. Are you sure the file exists?\n"); freeArguments(&args); - mCoreConfigFreeOpts(&opts); return 1; } #ifdef M_CORE_GBA else if (GBAIsROM(vf)) { platform = PLATFORM_GBA; - if (!opts.width) { - opts.width = VIDEO_HORIZONTAL_PIXELS; - } - if (!opts.height) { - opts.height = VIDEO_VERTICAL_PIXELS; - } + renderer.width = VIDEO_HORIZONTAL_PIXELS; + renderer.height = VIDEO_VERTICAL_PIXELS; renderer.core = GBACoreCreate(); #ifdef BUILD_GL - mSDLGLCreateGBA(&renderer); + mSDLGLCreate(&renderer); #elif defined(BUILD_GLES2) || defined(USE_EPOXY) mSDLGLES2Create(&renderer); #else@@ -116,15 +109,11 @@ #endif
#ifdef M_CORE_GB else if (GBIsROM(vf)) { platform = PLATFORM_GB; - if (!opts.width) { - opts.width = GB_VIDEO_HORIZONTAL_PIXELS; - } - if (!opts.height) { - opts.height = GB_VIDEO_VERTICAL_PIXELS; - } + renderer.width = GB_VIDEO_HORIZONTAL_PIXELS; + renderer.height = GB_VIDEO_VERTICAL_PIXELS; renderer.core = GBCoreCreate(); #ifdef BUILD_GL - mSDLGLCreateGB(&renderer); + mSDLGLCreate(&renderer); #elif defined(BUILD_GLES2) || defined(USE_EPOXY) mSDLGLES2CreateGB(&renderer); #else@@ -135,47 +124,46 @@ #endif
else { printf("Could not run game. Are you sure the file exists and is a compatible game?\n"); freeArguments(&args); - mCoreConfigFreeOpts(&opts); return 1; } } + renderer.ratio = graphicsOpts.multiplier; + if (renderer.ratio == 0) { + renderer.ratio = 1; + } + opts.width = renderer.width * renderer.ratio; + opts.height = renderer.height * renderer.ratio; + + if (!renderer.core->init(renderer.core)) { + freeArguments(&args); + return 1; + } + mInputMapInit(&renderer.core->inputMap, &GBAInputInfo); mCoreInitConfig(renderer.core, PORT); applyArguments(&args, &subparser, &renderer.core->config); mCoreConfigLoadDefaults(&renderer.core->config, &opts); + mCoreLoadConfig(renderer.core); - // TODO: Load from config - renderer.viewportWidth = opts.width; - renderer.viewportHeight = opts.height; + renderer.viewportWidth = renderer.core->opts.width; + renderer.viewportHeight = renderer.core->opts.height; #if SDL_VERSION_ATLEAST(2, 0, 0) - renderer.player.fullscreen = opts.fullscreen; + renderer.player.fullscreen = renderer.core->opts.fullscreen; renderer.player.windowUpdated = 0; #else - renderer.fullscreen = opts.fullscreen; + renderer.fullscreen = renderer.core->opts.fullscreen; #endif - renderer.ratio = graphicsOpts.multiplier; - if (renderer.ratio == 0) { - renderer.ratio = 1; - } - - renderer.lockAspectRatio = opts.lockAspectRatio; - renderer.filter = opts.resampleVideo; + renderer.lockAspectRatio = renderer.core->opts.lockAspectRatio; + renderer.filter = renderer.core->opts.resampleVideo; if (!mSDLInit(&renderer)) { freeArguments(&args); - mCoreConfigFreeOpts(&opts); - mCoreConfigDeinit(&renderer.core->config); + renderer.core->deinit(renderer.core); return 1; } - - if (renderer.core) { - // TODO: Check return code - renderer.core->init(renderer.core); - } - mCoreLoadConfig(renderer.core); renderer.player.bindings = &renderer.core->inputMap; mSDLInitBindingsGBA(&renderer.core->inputMap);
M
src/platform/sdl/main.h
→
src/platform/sdl/main.h
@@ -62,6 +62,8 @@ #else
bool fullscreen; #endif + unsigned width; + unsigned height; int viewportWidth; int viewportHeight; int ratio;@@ -98,8 +100,7 @@
void mSDLSWCreate(struct mSDLRenderer* renderer); #ifdef BUILD_GL -void mSDLGLCreateGBA(struct mSDLRenderer* renderer); -void mSDLGLCreateGB(struct mSDLRenderer* renderer); +void mSDLGLCreate(struct mSDLRenderer* renderer); #endif #if defined(BUILD_GLES2) || defined(USE_EPOXY)