all repos — mgba @ 32d1f5bbfb8630834feaa015cf77a499d569ca82

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