all repos — mgba @ 8345e29798f8e22ce0e0053ff62d08d61dce36fa

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#if SDL_VERSION_ATLEAST(2, 0, 0)
142	renderer.core->setRumble(renderer.core, &renderer.player.rumble.d);
143#endif
144
145	int ret;
146
147	// TODO: Use opts and config
148	ret = mSDLRun(&renderer, &args);
149	mSDLDetachPlayer(&renderer.events, &renderer.player);
150	mInputMapDeinit(&renderer.core->inputMap);
151
152	mSDLDeinit(&renderer);
153
154	freeArguments(&args);
155	mCoreConfigFreeOpts(&opts);
156	mCoreConfigDeinit(&renderer.core->config);
157	renderer.core->deinit(renderer.core);
158
159	return ret;
160}
161
162int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
163	struct mCoreThread thread = {
164		.core = renderer->core
165	};
166	if (!mCoreLoadFile(renderer->core, args->fname)) {
167		return 1;
168	}
169	mCoreAutoloadSave(renderer->core);
170	struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core);
171	if (debugger) {
172		mDebuggerAttach(debugger, renderer->core);
173		mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL);
174	}
175
176	if (args->patch) {
177		struct VFile* patch = VFileOpen(args->patch, O_RDONLY);
178		if (patch) {
179			renderer->core->loadPatch(renderer->core, patch);
180		}
181	} else {
182		mCoreAutoloadPatch(renderer->core);
183	}
184
185	renderer->audio.samples = renderer->core->opts.audioBuffers;
186	renderer->audio.sampleRate = 44100;
187
188	bool didFail = !mSDLInitAudio(&renderer->audio, &thread);
189	if (!didFail) {
190#if SDL_VERSION_ATLEAST(2, 0, 0)
191		mSDLSetScreensaverSuspendable(&renderer->events, renderer->core->opts.suspendScreensaver);
192		mSDLSuspendScreensaver(&renderer->events);
193#endif
194		if (mCoreThreadStart(&thread)) {
195			renderer->runloop(renderer, &thread);
196			mSDLPauseAudio(&renderer->audio);
197			mCoreThreadJoin(&thread);
198		} else {
199			didFail = true;
200			printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
201		}
202
203#if SDL_VERSION_ATLEAST(2, 0, 0)
204		mSDLResumeScreensaver(&renderer->events);
205		mSDLSetScreensaverSuspendable(&renderer->events, false);
206#endif
207
208		if (mCoreThreadHasCrashed(&thread)) {
209			didFail = true;
210			printf("The game crashed!\n");
211		}
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}