SDL: More video refactoring, fix scaling and aspect ratio locking

This commit is contained in:
Jeffrey Pfau 2016-02-05 00:11:24 -08:00
parent b325376f05
commit 3863513b2a
6 changed files with 50 additions and 97 deletions

View File

@ -34,7 +34,6 @@ static bool _GBCoreInit(struct mCore* core) {
LR35902Init(cpu); LR35902Init(cpu);
GBVideoSoftwareRendererCreate(&gbcore->renderer); GBVideoSoftwareRendererCreate(&gbcore->renderer);
GBVideoAssociateRenderer(&gb->video, &gbcore->renderer.d);
gb->keySource = &gbcore->keys; gb->keySource = &gbcore->keys;
@ -110,6 +109,9 @@ static void _GBCoreUnloadROM(struct mCore* core) {
} }
static void _GBCoreReset(struct mCore* core) { 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); LR35902Reset(core->cpu);
} }
@ -184,6 +186,7 @@ static void _GBCoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
struct mCore* GBCoreCreate(void) { struct mCore* GBCoreCreate(void) {
struct GBCore* gbcore = malloc(sizeof(*gbcore)); struct GBCore* gbcore = malloc(sizeof(*gbcore));
struct mCore* core = &gbcore->d; struct mCore* core = &gbcore->d;
memset(&core->opts, 0, sizeof(core->opts));
core->cpu = 0; core->cpu = 0;
core->board = 0; core->board = 0;
core->init = _GBCoreInit; core->init = _GBCoreInit;

View File

@ -34,15 +34,12 @@ static bool _GBACoreInit(struct mCore* core) {
core->cpu = cpu; core->cpu = cpu;
core->board = gba; core->board = gba;
memset(&core->opts, 0, sizeof(core->opts));
GBACreate(gba); GBACreate(gba);
// TODO: Restore debugger and cheats // TODO: Restore debugger and cheats
ARMSetComponents(cpu, &gba->d, 0, 0); ARMSetComponents(cpu, &gba->d, 0, 0);
ARMInit(cpu); ARMInit(cpu);
GBAVideoSoftwareRendererCreate(&gbacore->renderer); GBAVideoSoftwareRendererCreate(&gbacore->renderer);
GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
gba->keySource = &gbacore->keys; gba->keySource = &gbacore->keys;
@ -140,7 +137,9 @@ static void _GBACoreUnloadROM(struct mCore* core) {
} }
static void _GBACoreReset(struct mCore* core) { static void _GBACoreReset(struct mCore* core) {
struct GBACore* gbacore = (struct GBACore*) core;
struct GBA* gba = (struct GBA*) core->board; struct GBA* gba = (struct GBA*) core->board;
GBAVideoAssociateRenderer(&gba->video, &gbacore->renderer.d);
ARMReset(core->cpu); ARMReset(core->cpu);
if (core->opts.skipBios) { if (core->opts.skipBios) {
GBASkipBIOS(core->board); GBASkipBIOS(core->board);
@ -220,6 +219,7 @@ static void _GBACoreSetRTC(struct mCore* core, struct mRTCSource* rtc) {
struct mCore* GBACoreCreate(void) { struct mCore* GBACoreCreate(void) {
struct GBACore* gbacore = malloc(sizeof(*gbacore)); struct GBACore* gbacore = malloc(sizeof(*gbacore));
struct mCore* core = &gbacore->d; struct mCore* core = &gbacore->d;
memset(&core->opts, 0, sizeof(core->opts));
core->cpu = 0; core->cpu = 0;
core->board = 0; core->board = 0;
core->init = _GBACoreInit; core->init = _GBACoreInit;

View File

@ -54,10 +54,10 @@ static void GBAGLContextResized(struct VideoBackend* v, unsigned w, unsigned h)
unsigned drawW = w; unsigned drawW = w;
unsigned drawH = h; unsigned drawH = h;
if (v->lockAspectRatio) { if (v->lockAspectRatio) {
if (w * 2 > h * 3) { if (w * v->height > h * v->width) {
drawW = h * 3 / 2; drawW = h * v->width / v->height;
} else if (w * 2 < h * 3) { } else if (w * v->height < h * v->width) {
drawH = w * 2 / 3; drawH = w * v->height / v->width;
} }
} }
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);

View File

@ -20,72 +20,33 @@ static void _doViewport(int w, int h, struct VideoBackend* v) {
v->clear(v); v->clear(v);
} }
#ifdef M_CORE_GBA static bool mSDLGLInit(struct mSDLRenderer* renderer);
#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 void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user); static void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user);
static void mSDLGLDeinit(struct mSDLRenderer* renderer); static void mSDLGLDeinit(struct mSDLRenderer* renderer);
#ifdef M_CORE_GBA void mSDLGLCreate(struct mSDLRenderer* renderer) {
void mSDLGLCreateGBA(struct mSDLRenderer* renderer) { renderer->init = mSDLGLInit;
renderer->init = mSDLGLInitGBA;
renderer->deinit = mSDLGLDeinit; renderer->deinit = mSDLGLDeinit;
renderer->runloop = mSDLGLRunloop; renderer->runloop = mSDLGLRunloop;
} }
bool mSDLGLInitGBA(struct mSDLRenderer* renderer) { bool mSDLGLInit(struct mSDLRenderer* renderer) {
mSDLGLCommonInit(renderer); mSDLGLCommonInit(renderer);
renderer->outputBuffer = malloc(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); renderer->outputBuffer = malloc(renderer->width * renderer->height * BYTES_PER_PIXEL);
memset(renderer->outputBuffer, 0, VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * BYTES_PER_PIXEL); memset(renderer->outputBuffer, 0, renderer->width * renderer->height * BYTES_PER_PIXEL);
renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, VIDEO_HORIZONTAL_PIXELS); renderer->core->setVideoBuffer(renderer->core, renderer->outputBuffer, renderer->width);
GBAGLContextCreate(&renderer->gl); GBAGLContextCreate(&renderer->gl);
renderer->gl.d.user = renderer; renderer->gl.d.user = renderer;
renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio; renderer->gl.d.lockAspectRatio = renderer->lockAspectRatio;
renderer->gl.d.filter = renderer->filter; renderer->gl.d.filter = renderer->filter;
renderer->gl.d.swap = mSDLGLCommonSwap; 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); _doViewport(renderer->viewportWidth, renderer->viewportHeight, &renderer->gl.d);
return true; 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) { void mSDLGLRunloop(struct mSDLRenderer* renderer, void* user) {
struct mCoreThread* context = user; struct mCoreThread* context = user;

View File

@ -57,8 +57,6 @@ int main(int argc, char** argv) {
struct mSDLRenderer renderer = {}; struct mSDLRenderer renderer = {};
struct mCoreOptions opts = { struct mCoreOptions opts = {
.width = 0,
.height = 0,
.useBios = true, .useBios = true,
.rewindEnable = true, .rewindEnable = true,
.audioBuffers = 512, .audioBuffers = 512,
@ -75,12 +73,12 @@ int main(int argc, char** argv) {
bool parsed = parseArguments(&args, argc, argv, &subparser); bool parsed = parseArguments(&args, argc, argv, &subparser);
if (!parsed || args.showHelp) { if (!parsed || args.showHelp) {
usage(argv[0], subparser.usage); usage(argv[0], subparser.usage);
mCoreConfigFreeOpts(&opts); freeArguments(&args);
return !parsed; return !parsed;
} }
if (args.showVersion) { if (args.showVersion) {
version(argv[0]); version(argv[0]);
mCoreConfigFreeOpts(&opts); freeArguments(&args);
return 0; return 0;
} }
@ -91,21 +89,16 @@ int main(int argc, char** argv) {
if (!vf) { if (!vf) {
printf("Could not open game. Are you sure the file exists?\n"); printf("Could not open game. Are you sure the file exists?\n");
freeArguments(&args); freeArguments(&args);
mCoreConfigFreeOpts(&opts);
return 1; return 1;
} }
#ifdef M_CORE_GBA #ifdef M_CORE_GBA
else if (GBAIsROM(vf)) { else if (GBAIsROM(vf)) {
platform = PLATFORM_GBA; platform = PLATFORM_GBA;
if (!opts.width) { renderer.width = VIDEO_HORIZONTAL_PIXELS;
opts.width = VIDEO_HORIZONTAL_PIXELS; renderer.height = VIDEO_VERTICAL_PIXELS;
}
if (!opts.height) {
opts.height = VIDEO_VERTICAL_PIXELS;
}
renderer.core = GBACoreCreate(); renderer.core = GBACoreCreate();
#ifdef BUILD_GL #ifdef BUILD_GL
mSDLGLCreateGBA(&renderer); mSDLGLCreate(&renderer);
#elif defined(BUILD_GLES2) || defined(USE_EPOXY) #elif defined(BUILD_GLES2) || defined(USE_EPOXY)
mSDLGLES2Create(&renderer); mSDLGLES2Create(&renderer);
#else #else
@ -116,15 +109,11 @@ int main(int argc, char** argv) {
#ifdef M_CORE_GB #ifdef M_CORE_GB
else if (GBIsROM(vf)) { else if (GBIsROM(vf)) {
platform = PLATFORM_GB; platform = PLATFORM_GB;
if (!opts.width) { renderer.width = GB_VIDEO_HORIZONTAL_PIXELS;
opts.width = GB_VIDEO_HORIZONTAL_PIXELS; renderer.height = GB_VIDEO_VERTICAL_PIXELS;
}
if (!opts.height) {
opts.height = GB_VIDEO_VERTICAL_PIXELS;
}
renderer.core = GBCoreCreate(); renderer.core = GBCoreCreate();
#ifdef BUILD_GL #ifdef BUILD_GL
mSDLGLCreateGB(&renderer); mSDLGLCreate(&renderer);
#elif defined(BUILD_GLES2) || defined(USE_EPOXY) #elif defined(BUILD_GLES2) || defined(USE_EPOXY)
mSDLGLES2CreateGB(&renderer); mSDLGLES2CreateGB(&renderer);
#else #else
@ -135,48 +124,47 @@ int main(int argc, char** argv) {
else { else {
printf("Could not run game. Are you sure the file exists and is a compatible game?\n"); printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
freeArguments(&args); freeArguments(&args);
mCoreConfigFreeOpts(&opts);
return 1; 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); mInputMapInit(&renderer.core->inputMap, &GBAInputInfo);
mCoreInitConfig(renderer.core, PORT); mCoreInitConfig(renderer.core, PORT);
applyArguments(&args, &subparser, &renderer.core->config); applyArguments(&args, &subparser, &renderer.core->config);
mCoreConfigLoadDefaults(&renderer.core->config, &opts); mCoreConfigLoadDefaults(&renderer.core->config, &opts);
mCoreLoadConfig(renderer.core);
// TODO: Load from config renderer.viewportWidth = renderer.core->opts.width;
renderer.viewportWidth = opts.width; renderer.viewportHeight = renderer.core->opts.height;
renderer.viewportHeight = opts.height;
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
renderer.player.fullscreen = opts.fullscreen; renderer.player.fullscreen = renderer.core->opts.fullscreen;
renderer.player.windowUpdated = 0; renderer.player.windowUpdated = 0;
#else #else
renderer.fullscreen = opts.fullscreen; renderer.fullscreen = renderer.core->opts.fullscreen;
#endif #endif
renderer.ratio = graphicsOpts.multiplier; renderer.lockAspectRatio = renderer.core->opts.lockAspectRatio;
if (renderer.ratio == 0) { renderer.filter = renderer.core->opts.resampleVideo;
renderer.ratio = 1;
}
renderer.lockAspectRatio = opts.lockAspectRatio;
renderer.filter = opts.resampleVideo;
if (!mSDLInit(&renderer)) { if (!mSDLInit(&renderer)) {
freeArguments(&args); freeArguments(&args);
mCoreConfigFreeOpts(&opts); renderer.core->deinit(renderer.core);
mCoreConfigDeinit(&renderer.core->config);
return 1; return 1;
} }
if (renderer.core) {
// TODO: Check return code
renderer.core->init(renderer.core);
}
mCoreLoadConfig(renderer.core);
renderer.player.bindings = &renderer.core->inputMap; renderer.player.bindings = &renderer.core->inputMap;
mSDLInitBindingsGBA(&renderer.core->inputMap); mSDLInitBindingsGBA(&renderer.core->inputMap);
mSDLInitEvents(&renderer.events); mSDLInitEvents(&renderer.events);

View File

@ -62,6 +62,8 @@ struct mSDLRenderer {
bool fullscreen; bool fullscreen;
#endif #endif
unsigned width;
unsigned height;
int viewportWidth; int viewportWidth;
int viewportHeight; int viewportHeight;
int ratio; int ratio;
@ -98,8 +100,7 @@ struct mSDLRenderer {
void mSDLSWCreate(struct mSDLRenderer* renderer); void mSDLSWCreate(struct mSDLRenderer* renderer);
#ifdef BUILD_GL #ifdef BUILD_GL
void mSDLGLCreateGBA(struct mSDLRenderer* renderer); void mSDLGLCreate(struct mSDLRenderer* renderer);
void mSDLGLCreateGB(struct mSDLRenderer* renderer);
#endif #endif
#if defined(BUILD_GLES2) || defined(USE_EPOXY) #if defined(BUILD_GLES2) || defined(USE_EPOXY)