all repos — mgba @ 38a4e9988f8c6c210b2fd70790ff1f0f7006515b

mGBA Game Boy Advance Emulator

src/platform/sdl/main.c (view raw)

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "main.h"
  7
  8#ifdef USE_CLI_DEBUGGER
  9#include "debugger/cli-debugger.h"
 10#endif
 11
 12#ifdef USE_GDB_STUB
 13#include "debugger/gdb-stub.h"
 14#endif
 15
 16#include "gba-thread.h"
 17#include "gba.h"
 18#include "gba-config.h"
 19#include "gba-video.h"
 20#include "platform/commandline.h"
 21#include "util/configuration.h"
 22
 23#include <SDL.h>
 24
 25#include <errno.h>
 26#include <signal.h>
 27#include <sys/time.h>
 28
 29#define PORT "sdl"
 30
 31static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer);
 32static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer);
 33
 34int main(int argc, char** argv) {
 35	struct SDLSoftwareRenderer renderer;
 36	GBAVideoSoftwareRendererCreate(&renderer.d);
 37
 38	struct GBAInputMap inputMap;
 39	GBAInputMapInit(&inputMap);
 40
 41	struct GBAConfig config;
 42	GBAConfigInit(&config, PORT);
 43	GBAConfigLoad(&config);
 44
 45	struct GBAOptions opts = {
 46		.width = VIDEO_HORIZONTAL_PIXELS,
 47		.height = VIDEO_VERTICAL_PIXELS,
 48		.audioBuffers = 512,
 49		.videoSync = false,
 50		.audioSync = true,
 51	};
 52	GBAConfigLoadDefaults(&config, &opts);
 53
 54	struct GBAArguments args = {};
 55	struct GraphicsOpts graphicsOpts = {};
 56
 57	struct SubParser subparser;
 58
 59	initParserForGraphics(&subparser, &graphicsOpts);
 60	if (!parseArguments(&args, &config, argc, argv, &subparser)) {
 61		usage(argv[0], subparser.usage);
 62		freeArguments(&args);
 63		GBAConfigFreeOpts(&opts);
 64		GBAConfigDeinit(&config);
 65		return 1;
 66	}
 67
 68	GBAConfigMap(&config, &opts);
 69
 70	renderer.viewportWidth = opts.width;
 71	renderer.viewportHeight = opts.height;
 72#if SDL_VERSION_ATLEAST(2, 0, 0)
 73	renderer.events.fullscreen = opts.fullscreen;
 74	renderer.events.windowUpdated = 0;
 75#endif
 76	renderer.ratio = graphicsOpts.multiplier;
 77
 78	renderer.lockAspectRatio = opts.lockAspectRatio;
 79	renderer.filter = opts.resampleVideo;
 80
 81	if (!_GBASDLInit(&renderer)) {
 82		freeArguments(&args);
 83		GBAConfigFreeOpts(&opts);
 84		GBAConfigDeinit(&config);
 85		return 1;
 86	}
 87
 88	struct GBAThread context = {
 89		.renderer = &renderer.d.d,
 90		.userData = &renderer
 91	};
 92
 93	context.debugger = createDebugger(&args, &context);
 94
 95	GBAMapOptionsToContext(&opts, &context);
 96	GBAMapArgumentsToContext(&args, &context);
 97
 98	renderer.audio.samples = context.audioBuffers;
 99	GBASDLInitAudio(&renderer.audio, &context);
100
101	renderer.events.bindings = &inputMap;
102	GBASDLInitBindings(&inputMap);
103	GBASDLInitEvents(&renderer.events);
104	GBASDLEventsLoadConfig(&renderer.events, &config.configTable); // TODO: Don't use this directly
105
106	GBAThreadStart(&context);
107
108	GBASDLRunloop(&context, &renderer);
109
110	GBAThreadJoin(&context);
111	freeArguments(&args);
112	GBAConfigFreeOpts(&opts);
113	GBAConfigDeinit(&config);
114	free(context.debugger);
115	GBAInputMapDeinit(&inputMap);
116
117	_GBASDLDeinit(&renderer);
118
119	return 0;
120}
121
122static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer) {
123	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
124		return false;
125	}
126
127	return GBASDLInit(renderer);
128}
129
130static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
131	free(renderer->d.outputBuffer);
132
133	GBASDLDeinitEvents(&renderer->events);
134	GBASDLDeinitAudio(&renderer->audio);
135#if SDL_VERSION_ATLEAST(2, 0, 0)
136	SDL_DestroyWindow(renderer->window);
137#endif
138
139	GBASDLDeinit(renderer);
140
141	SDL_Quit();
142
143}