all repos — mgba @ 3cfdad192c2e54ad6a94430008bf8f99c1c831cc

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 "core/core.h"
 17#include "core/config.h"
 18#include "core/input.h"
 19#include "core/thread.h"
 20#include "gba/input.h"
 21#ifdef M_CORE_GBA
 22#include "gba/core.h"
 23#include "gba/gba.h"
 24#include "gba/video.h"
 25#endif
 26#ifdef M_CORE_GB
 27#include "gb/core.h"
 28#include "gb/gb.h"
 29#include "gb/video.h"
 30#endif
 31#include "feature/commandline.h"
 32#include "util/configuration.h"
 33#include "util/vfs.h"
 34
 35#include <SDL.h>
 36
 37#include <errno.h>
 38#include <signal.h>
 39#include <sys/time.h>
 40
 41#define PORT "sdl"
 42
 43static bool mSDLInit(struct mSDLRenderer* renderer);
 44static void mSDLDeinit(struct mSDLRenderer* renderer);
 45
 46static int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args);
 47
 48int main(int argc, char** argv) {
 49	struct mSDLRenderer renderer = {};
 50
 51	struct mCoreOptions opts = {
 52		.useBios = true,
 53		.rewindEnable = true,
 54		.rewindBufferCapacity = 600,
 55		.audioBuffers = 1024,
 56		.videoSync = false,
 57		.audioSync = true,
 58		.volume = 0x100,
 59	};
 60
 61	struct mArguments args;
 62	struct mGraphicsOpts graphicsOpts;
 63
 64	struct mSubParser subparser;
 65
 66	initParserForGraphics(&subparser, &graphicsOpts);
 67	bool parsed = parseArguments(&args, argc, argv, &subparser);
 68	if (!args.fname) {
 69		parsed = false;
 70	}
 71	if (!parsed || args.showHelp) {
 72		usage(argv[0], subparser.usage);
 73		freeArguments(&args);
 74		return !parsed;
 75	}
 76	if (args.showVersion) {
 77		version(argv[0]);
 78		freeArguments(&args);
 79		return 0;
 80	}
 81
 82	renderer.core = mCoreFind(args.fname);
 83	if (!renderer.core) {
 84		printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
 85		freeArguments(&args);
 86		return 1;
 87	}
 88	renderer.core->desiredVideoDimensions(renderer.core, &renderer.width, &renderer.height);
 89#ifdef BUILD_GL
 90	mSDLGLCreate(&renderer);
 91#elif defined(BUILD_GLES2) || defined(USE_EPOXY)
 92	mSDLGLES2Create(&renderer);
 93#else
 94	mSDLSWCreate(&renderer);
 95#endif
 96
 97	renderer.ratio = graphicsOpts.multiplier;
 98	if (renderer.ratio == 0) {
 99		renderer.ratio = 1;
100	}
101	opts.width = renderer.width * renderer.ratio;
102	opts.height = renderer.height * renderer.ratio;
103
104	if (!renderer.core->init(renderer.core)) {
105		freeArguments(&args);
106		return 1;
107	}
108
109	mInputMapInit(&renderer.core->inputMap, &GBAInputInfo);
110	mCoreInitConfig(renderer.core, PORT);
111	applyArguments(&args, &subparser, &renderer.core->config);
112
113	mCoreConfigLoadDefaults(&renderer.core->config, &opts);
114	mCoreLoadConfig(renderer.core);
115
116	renderer.viewportWidth = renderer.core->opts.width;
117	renderer.viewportHeight = renderer.core->opts.height;
118#if SDL_VERSION_ATLEAST(2, 0, 0)
119	renderer.player.fullscreen = renderer.core->opts.fullscreen;
120	renderer.player.windowUpdated = 0;
121#else
122	renderer.fullscreen = renderer.core->opts.fullscreen;
123#endif
124
125	renderer.lockAspectRatio = renderer.core->opts.lockAspectRatio;
126	renderer.filter = renderer.core->opts.resampleVideo;
127
128	if (!mSDLInit(&renderer)) {
129		freeArguments(&args);
130		renderer.core->deinit(renderer.core);
131		return 1;
132	}
133
134	renderer.player.bindings = &renderer.core->inputMap;
135	mSDLInitBindingsGBA(&renderer.core->inputMap);
136	mSDLInitEvents(&renderer.events);
137	mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&renderer.core->config));
138	mSDLAttachPlayer(&renderer.events, &renderer.player);
139	mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config));
140
141	int ret;
142
143	// TODO: Use opts and config
144	ret = mSDLRun(&renderer, &args);
145	mSDLDetachPlayer(&renderer.events, &renderer.player);
146	mInputMapDeinit(&renderer.core->inputMap);
147
148	mSDLDeinit(&renderer);
149
150	freeArguments(&args);
151	mCoreConfigFreeOpts(&opts);
152	mCoreConfigDeinit(&renderer.core->config);
153	renderer.core->deinit(renderer.core);
154
155	return ret;
156}
157
158int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
159	struct mCoreThread thread = {
160		.core = renderer->core
161	};
162	if (!mCoreLoadFile(renderer->core, args->fname)) {
163		return 1;
164	}
165	mCoreAutoloadSave(renderer->core);
166	struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core);
167	if (debugger) {
168		mDebuggerAttach(debugger, renderer->core);
169		mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL);
170	}
171
172	if (args->patch) {
173		struct VFile* patch = VFileOpen(args->patch, O_RDONLY);
174		if (patch) {
175			renderer->core->loadPatch(renderer->core, patch);
176		}
177	} else {
178		mCoreAutoloadPatch(renderer->core);
179	}
180
181	renderer->audio.samples = renderer->core->opts.audioBuffers;
182	renderer->audio.sampleRate = 44100;
183
184	bool didFail = !mSDLInitAudio(&renderer->audio, &thread);
185	if (!didFail) {
186#if SDL_VERSION_ATLEAST(2, 0, 0)
187		mSDLSetScreensaverSuspendable(&renderer->events, renderer->core->opts.suspendScreensaver);
188		mSDLSuspendScreensaver(&renderer->events);
189#endif
190		if (mCoreThreadStart(&thread)) {
191			renderer->runloop(renderer, &thread);
192			mSDLPauseAudio(&renderer->audio);
193			mCoreThreadJoin(&thread);
194		} else {
195			didFail = true;
196			printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
197		}
198
199#if SDL_VERSION_ATLEAST(2, 0, 0)
200		mSDLResumeScreensaver(&renderer->events);
201		mSDLSetScreensaverSuspendable(&renderer->events, false);
202#endif
203
204		if (mCoreThreadHasCrashed(&thread)) {
205			didFail = true;
206			printf("The game crashed!\n");
207		}
208	}
209	renderer->core->unloadROM(renderer->core);
210	return didFail;
211}
212
213static bool mSDLInit(struct mSDLRenderer* renderer) {
214	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
215		printf("Could not initialize video: %s\n", SDL_GetError());
216		return false;
217	}
218
219	return renderer->init(renderer);
220}
221
222static void mSDLDeinit(struct mSDLRenderer* renderer) {
223	mSDLDeinitEvents(&renderer->events);
224	mSDLDeinitAudio(&renderer->audio);
225#if SDL_VERSION_ATLEAST(2, 0, 0)
226	SDL_DestroyWindow(renderer->window);
227#endif
228
229	renderer->deinit(renderer);
230
231	SDL_Quit();
232}