all repos — mgba @ d25ba2ec59b940940950aa1330fe563bced278bd

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