all repos — mgba @ 3ed2993e8ced10194017d2f10527f1aae864e9ac

mGBA Game Boy Advance Emulator

GBA: Add configuration loader
Jeffrey Pfau jeffrey@endrift.com
Sat, 01 Nov 2014 03:04:10 -0700
commit

3ed2993e8ced10194017d2f10527f1aae864e9ac

parent

5f440b6a027991dcb4356d78c7b559d50e429841

A src/gba/gba-config.c

@@ -0,0 +1,50 @@

+#include "gba-config.h" + +#include "platform/commandline.h" +#include "util/configuration.h" + +#define SECTION_NAME_MAX 128 + +static const char* _lookupValue(const struct Configuration* config, const char* key, const char* port) { + if (port) { + char sectionName[SECTION_NAME_MAX]; + snprintf(sectionName, SECTION_NAME_MAX, "ports.%s", port); + sectionName[SECTION_NAME_MAX - 1] = '\0'; + const char* value = ConfigurationGetValue(config, sectionName, key); + if (value) { + return value; + } + } + return ConfigurationGetValue(config, 0, key); +} + +static bool _lookupIntValue(const struct Configuration* config, const char* key, const char* port, int* out) { + const char* charValue = _lookupValue(config, key, port); + if (!charValue) { + return false; + } + char* end; + long value = strtol(charValue, &end, 10); + if (*end) { + return false; + } + *out = value; + return true; +} + +bool GBAConfigLoad(struct Configuration* config) { + return ConfigurationRead(config, BINARY_NAME ".ini"); +} + +void GBAConfigMapStartupOpts(const struct Configuration* config, const char* port, struct StartupOptions* opts) { + _lookupIntValue(config, "logLevel", port, &opts->logLevel); + _lookupIntValue(config, "frameskip", port, &opts->frameskip); + _lookupIntValue(config, "rewindBufferCapacity", port, &opts->rewindBufferCapacity); + _lookupIntValue(config, "rewindBufferInterval", port, &opts->rewindBufferInterval); +} + +void GBAConfigMapGraphicsOpts(const struct Configuration* config, const char* port, struct GraphicsOpts* opts) { + _lookupIntValue(config, "fullscreen", port, &opts->fullscreen); + _lookupIntValue(config, "width", port, &opts->width); + _lookupIntValue(config, "height", port, &opts->height); +}
A src/gba/gba-config.h

@@ -0,0 +1,15 @@

+#ifndef GBA_CONFIG_H +#define GBA_CONFIG_H + +#include "util/common.h" + +struct Configuration; +struct StartupOptions; +struct GraphicsOpts; + +bool GBAConfigLoad(struct Configuration*); + +void GBAConfigMapStartupOpts(const struct Configuration*, const char* port, struct StartupOptions*); +void GBAConfigMapGraphicsOpts(const struct Configuration*, const char* port, struct GraphicsOpts*); + +#endif
M src/platform/commandline.csrc/platform/commandline.c

@@ -10,12 +10,15 @@ #ifdef USE_GDB_STUB

#include "debugger/gdb-stub.h" #endif +#include "gba/gba-video.h" + #include <fcntl.h> #include <getopt.h> -#define GRAPHICS_OPTIONS "234f" +#define GRAPHICS_OPTIONS "1234f" #define GRAPHICS_USAGE \ "\nGraphics options:\n" \ + " -1 1x viewport\n" \ " -2 2x viewport\n" \ " -3 3x viewport\n" \ " -4 4x viewport\n" \

@@ -38,8 +41,6 @@

bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg); bool parseCommandArgs(struct StartupOptions* opts, int argc, char* const* argv, struct SubParser* subparser) { - memset(opts, 0, sizeof(*opts)); - int ch; char options[64] = "b:Dl:p:s:"

@@ -121,10 +122,7 @@ parser->usage = GRAPHICS_USAGE;

parser->opts = opts; parser->parse = _parseGraphicsArg; parser->extraOptions = GRAPHICS_OPTIONS; - opts->multiplier = 1; - opts->fullscreen = 0; - opts->width = 240; - opts->height = 160; + opts->multiplier = 0; } bool _parseGraphicsArg(struct SubParser* parser, int option, const char* arg) {

@@ -134,29 +132,16 @@ switch (option) {

case 'f': graphicsOpts->fullscreen = 1; return true; + case '1': case '2': - if (graphicsOpts->multiplier != 1) { - return false; - } - graphicsOpts->multiplier = 2; - graphicsOpts->width *= graphicsOpts->multiplier; - graphicsOpts->height *= graphicsOpts->multiplier; - return true; case '3': - if (graphicsOpts->multiplier != 1) { - return false; - } - graphicsOpts->multiplier = 3; - graphicsOpts->width *= graphicsOpts->multiplier; - graphicsOpts->height *= graphicsOpts->multiplier; - return true; case '4': - if (graphicsOpts->multiplier != 1) { + if (graphicsOpts->multiplier) { return false; } - graphicsOpts->multiplier = 4; - graphicsOpts->width *= graphicsOpts->multiplier; - graphicsOpts->height *= graphicsOpts->multiplier; + graphicsOpts->multiplier = option - '0'; + graphicsOpts->width = VIDEO_HORIZONTAL_PIXELS * graphicsOpts->multiplier; + graphicsOpts->height = VIDEO_VERTICAL_PIXELS * graphicsOpts->multiplier; return true; default: return false;
M src/platform/commandline.hsrc/platform/commandline.h

@@ -15,17 +15,20 @@ DEBUGGER_MAX

}; struct StartupOptions { + // Passed only char* fname; char* bios; char* patch; bool dirmode; + + enum DebuggerType debuggerType; + bool debugAtStart; + + // Configurable int logLevel; int frameskip; int rewindBufferCapacity; int rewindBufferInterval; - - enum DebuggerType debuggerType; - int debugAtStart; }; struct SubParser {

@@ -36,7 +39,10 @@ void* opts;

}; struct GraphicsOpts { + // Passed only int multiplier; + + // Configurable int fullscreen; int width; int height;
M src/platform/perf-main.csrc/platform/perf-main.c

@@ -43,7 +43,7 @@ .extraOptions = PERF_OPTIONS,

.opts = &perfOpts }; - struct StartupOptions opts; + struct StartupOptions opts = {}; if (!parseCommandArgs(&opts, argc, argv, &subparser)) { usage(argv[0], PERF_USAGE); return 1;
M src/platform/sdl/gl-main.csrc/platform/sdl/gl-main.c

@@ -8,10 +8,12 @@ #endif

#include "gba-thread.h" #include "gba.h" +#include "gba-config.h" #include "sdl-audio.h" #include "sdl-events.h" #include "renderers/video-software.h" #include "platform/commandline.h" +#include "util/configuration.h" #include <SDL.h> #ifdef __APPLE__

@@ -23,6 +25,8 @@

#include <errno.h> #include <signal.h> #include <sys/time.h> + +#define PORT "sdl-gl" struct GLSoftwareRenderer { struct GBAVideoSoftwareRenderer d;

@@ -61,9 +65,18 @@ int main(int argc, char** argv) {

struct GLSoftwareRenderer renderer; GBAVideoSoftwareRendererCreate(&renderer.d); - struct StartupOptions opts; + struct Configuration config; + ConfigurationInit(&config); + GBAConfigLoad(&config); + + struct StartupOptions opts = { }; + struct GraphicsOpts graphicsOpts = { }; + struct SubParser subparser; - struct GraphicsOpts graphicsOpts; + + GBAConfigMapStartupOpts(&config, PORT, &opts); + GBAConfigMapGraphicsOpts(&config, PORT, &graphicsOpts); + initParserForGraphics(&subparser, &graphicsOpts); if (!parseCommandArgs(&opts, argc, argv, &subparser)) { usage(argv[0], subparser.usage);