all repos — mgba @ 97447ffa40d6f90ae5f1daf84987e6ff7688d0b8

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 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/gba.h"
 17#include "gba/supervisor/config.h"
 18#include "gba/supervisor/thread.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		.useBios = true,
 49		.rewindEnable = true,
 50		.audioBuffers = 512,
 51		.videoSync = false,
 52		.audioSync = true,
 53	};
 54	GBAConfigLoadDefaults(&config, &opts);
 55
 56	struct GBAArguments args;
 57	struct GraphicsOpts graphicsOpts;
 58
 59	struct SubParser subparser;
 60
 61	initParserForGraphics(&subparser, &graphicsOpts);
 62	bool parsed = parseArguments(&args, &config, argc, argv, &subparser);
 63	if (!parsed || args.showHelp) {
 64		usage(argv[0], subparser.usage);
 65		freeArguments(&args);
 66		GBAConfigFreeOpts(&opts);
 67		GBAConfigDeinit(&config);
 68		return !parsed;
 69	}
 70
 71	GBAConfigMap(&config, &opts);
 72
 73	renderer.viewportWidth = opts.width;
 74	renderer.viewportHeight = opts.height;
 75#if SDL_VERSION_ATLEAST(2, 0, 0)
 76	renderer.player.fullscreen = opts.fullscreen;
 77	renderer.player.windowUpdated = 0;
 78#else
 79	renderer.fullscreen = opts.fullscreen;
 80#endif
 81	renderer.ratio = graphicsOpts.multiplier;
 82	if (renderer.ratio == 0) {
 83		renderer.ratio = 1;
 84	}
 85
 86	renderer.lockAspectRatio = opts.lockAspectRatio;
 87	renderer.filter = opts.resampleVideo;
 88
 89#ifdef BUILD_GL
 90	GBASDLGLCreate(&renderer);
 91#elif defined(BUILD_GLES2)
 92	GBASDLGLES2Create(&renderer);
 93#else
 94	GBASDLSWCreate(&renderer);
 95#endif
 96
 97	if (!GBASDLInit(&renderer)) {
 98		freeArguments(&args);
 99		GBAConfigFreeOpts(&opts);
100		GBAConfigDeinit(&config);
101		return 1;
102	}
103
104	struct GBAThread context = {
105		.renderer = &renderer.d.d,
106		.userData = &renderer
107	};
108
109	context.debugger = createDebugger(&args, &context);
110
111	GBAMapOptionsToContext(&opts, &context);
112	GBAMapArgumentsToContext(&args, &context);
113
114	bool didFail = false;
115
116	renderer.audio.samples = context.audioBuffers;
117	renderer.audio.sampleRate = 44100;
118	if (opts.sampleRate) {
119		renderer.audio.sampleRate = opts.sampleRate;
120	}
121	if (!GBASDLInitAudio(&renderer.audio, &context)) {
122		didFail = true;
123	}
124
125	renderer.player.bindings = &inputMap;
126	GBASDLInitBindings(&inputMap);
127	GBASDLInitEvents(&renderer.events);
128	GBASDLEventsLoadConfig(&renderer.events, GBAConfigGetInput(&config));
129	GBASDLAttachPlayer(&renderer.events, &renderer.player);
130	GBASDLPlayerLoadConfig(&renderer.player, GBAConfigGetInput(&config));
131	context.overrides = GBAConfigGetOverrides(&config);
132
133	if (!didFail) {
134#if SDL_VERSION_ATLEAST(2, 0, 0)
135		GBASDLSetScreensaverSuspendable(&renderer.events, opts.suspendScreensaver);
136		GBASDLSuspendScreensaver(&renderer.events);
137#endif
138		if (GBAThreadStart(&context)) {
139			renderer.runloop(&context, &renderer);
140			GBAThreadJoin(&context);
141		} else {
142			didFail = true;
143			printf("Could not run game. Are you sure the file exists and is a Game Boy Advance game?\n");
144		}
145
146#if SDL_VERSION_ATLEAST(2, 0, 0)
147		GBASDLResumeScreensaver(&renderer.events);
148		GBASDLSetScreensaverSuspendable(&renderer.events, false);
149#endif
150
151		if (GBAThreadHasCrashed(&context)) {
152			didFail = true;
153			printf("The game crashed!\n");
154		}
155	}
156	freeArguments(&args);
157	GBAConfigFreeOpts(&opts);
158	GBAConfigDeinit(&config);
159	free(context.debugger);
160	GBASDLDetachPlayer(&renderer.events, &renderer.player);
161	GBAInputMapDeinit(&inputMap);
162
163	GBASDLDeinit(&renderer);
164
165	return didFail;
166}
167
168static bool GBASDLInit(struct SDLSoftwareRenderer* renderer) {
169	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
170		printf("Could not initialize video: %s\n", SDL_GetError());
171		return false;
172	}
173
174	return renderer->init(renderer);
175}
176
177static void GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
178	GBASDLDeinitEvents(&renderer->events);
179	GBASDLDeinitAudio(&renderer->audio);
180#if SDL_VERSION_ATLEAST(2, 0, 0)
181	SDL_DestroyWindow(renderer->window);
182#endif
183
184	renderer->deinit(renderer);
185
186	SDL_Quit();
187}