all repos — mgba @ 81f38fd76f8a0d1db377243e0e69f568118dde1f

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