all repos — mgba @ fc905657adeba406a2c280bbb94b09533a89e3f8

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