all repos — mgba @ 533e96392bde7b4852c37aaaad8e53ce1bf38b0f

mGBA Game Boy Advance Emulator

Core: Add mCoreFind
Jeffrey Pfau jeffrey@endrift.com
Sun, 07 Feb 2016 22:24:25 -0800
commit

533e96392bde7b4852c37aaaad8e53ce1bf38b0f

parent

53191d2068770348e93c0648f7c82ee7a0a64dd0

3 files changed, 87 insertions(+), 44 deletions(-)

jump to
M src/core/core.csrc/core/core.c

@@ -11,6 +11,75 @@

#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 #include "util/png-io.h" +#ifdef M_CORE_GB +#include "gb/core.h" +#include "gb/gb.h" +#endif +#ifdef M_CORE_GBA +#include "gba/core.h" +#include "gba/gba.h" +#endif + +static struct mCoreFilter { + bool (*filter)(struct VFile*); + struct mCore* (*open)(void); +} _filters[] = { +#ifdef M_CORE_GBA + { GBAIsROM, GBACoreCreate }, +#endif +#ifdef M_CORE_GB + { GBIsROM, GBCoreCreate }, +#endif + { 0, 0 } +}; + +struct mCore* mCoreFind(const char* path) { + struct VDir* archive = VDirOpenArchive(path); + struct mCore* (*open)(void) = NULL; + if (archive) { + struct VDirEntry* dirent = archive->listNext(archive); + while (dirent) { + struct VFile* vf = archive->openFile(archive, dirent->name(dirent), O_RDONLY); + if (!vf) { + dirent = archive->listNext(archive); + continue; + } + struct mCoreFilter* filter; + for (filter = &_filters[0]; filter->filter; ++filter) { + if (filter->filter(vf)) { + break; + } + } + vf->close(vf); + if (filter->open) { + open = filter->open; + break; + } + dirent = archive->listNext(archive); + } + archive->close(archive); + } else { + struct VFile* vf = VFileOpen(path, O_RDONLY); + if (!vf) { + return NULL; + } + struct mCoreFilter* filter; + for (filter = &_filters[0]; filter->filter; ++filter) { + if (filter->filter(vf)) { + break; + } + } + vf->close(vf); + if (filter->open) { + open = filter->open; + } + } + if (open) { + return open(); + } + return NULL; +} + bool mCoreLoadFile(struct mCore* core, const char* path) { struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM); if (!rom) {
M src/core/core.hsrc/core/core.h

@@ -17,7 +17,16 @@ #include "core/input.h"

#endif #include "core/interface.h" -struct VFile; +enum mPlatform { + PLATFORM_NONE = -1, +#ifdef M_CORE_GBA + PLATFORM_GBA, +#endif +#ifdef M_CORE_GB + PLATFORM_GB, +#endif +}; + struct mRTCSource; struct mCoreConfig; struct mCoreSync;

@@ -80,6 +89,7 @@ void (*setRTC)(struct mCore*, struct mRTCSource*);

}; #if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2 +struct mCore* mCoreFind(const char* path); bool mCoreLoadFile(struct mCore* core, const char* path); bool mCoreAutoloadSave(struct mCore* core);
M src/platform/sdl/main.csrc/platform/sdl/main.c

@@ -41,13 +41,6 @@ #include <sys/time.h>

#define PORT "sdl" -// TODO: Move somewhere -enum mPlatform { - PLATFORM_NONE = -1, - PLATFORM_GBA, - PLATFORM_GB -}; - static bool mSDLInit(struct mSDLRenderer* renderer); static void mSDLDeinit(struct mSDLRenderer* renderer);

@@ -82,50 +75,21 @@ freeArguments(&args);

return 0; } - enum mPlatform platform = PLATFORM_NONE; - if (args.fname) { - struct VFile* vf = VFileOpen(args.fname, O_RDONLY); - if (!vf) { - printf("Could not open game. Are you sure the file exists?\n"); + renderer.core = mCoreFind(args.fname); + if (!renderer.core) { + printf("Could not run game. Are you sure the file exists and is a compatible game?\n"); freeArguments(&args); return 1; } -#ifdef M_CORE_GBA - else if (GBAIsROM(vf)) { - platform = PLATFORM_GBA; - renderer.width = VIDEO_HORIZONTAL_PIXELS; - renderer.height = VIDEO_VERTICAL_PIXELS; - renderer.core = GBACoreCreate(); -#ifdef BUILD_GL - mSDLGLCreate(&renderer); -#elif defined(BUILD_GLES2) || defined(USE_EPOXY) - mSDLGLES2Create(&renderer); -#else - mSDLSWCreate(&renderer); -#endif - } -#endif -#ifdef M_CORE_GB - else if (GBIsROM(vf)) { - platform = PLATFORM_GB; - renderer.width = GB_VIDEO_HORIZONTAL_PIXELS; - renderer.height = GB_VIDEO_VERTICAL_PIXELS; - renderer.core = GBCoreCreate(); + renderer.core->desiredVideoDimensions(renderer.core, &renderer.width, &renderer.height); #ifdef BUILD_GL - mSDLGLCreate(&renderer); + mSDLGLCreate(&renderer); #elif defined(BUILD_GLES2) || defined(USE_EPOXY) - mSDLGLES2CreateGB(&renderer); + mSDLGLES2Create(&renderer); #else - mSDLSWCreateGB(&renderer); -#endif - } + mSDLSWCreate(&renderer); #endif - else { - printf("Could not run game. Are you sure the file exists and is a compatible game?\n"); - freeArguments(&args); - return 1; - } } renderer.ratio = graphicsOpts.multiplier;