all repos — mgba @ 3889fd4d3f893b9a2445cea2057dbd23c997bf3c

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 <mgba/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.lockIntegerScaling = renderer.core->opts.lockIntegerScaling;
118	renderer.filter = renderer.core->opts.resampleVideo;
119
120	if (!mSDLInit(&renderer)) {
121		freeArguments(&args);
122		renderer.core->deinit(renderer.core);
123		return 1;
124	}
125
126	renderer.player.bindings = &renderer.core->inputMap;
127	mSDLInitBindingsGBA(&renderer.core->inputMap);
128	mSDLInitEvents(&renderer.events);
129	mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&renderer.core->config));
130	mSDLAttachPlayer(&renderer.events, &renderer.player);
131	mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config));
132
133#if SDL_VERSION_ATLEAST(2, 0, 0)
134	renderer.core->setPeripheral(renderer.core, mPERIPH_RUMBLE, &renderer.player.rumble.d);
135#endif
136
137	int ret;
138
139	// TODO: Use opts and config
140	ret = mSDLRun(&renderer, &args);
141	mSDLDetachPlayer(&renderer.events, &renderer.player);
142	mInputMapDeinit(&renderer.core->inputMap);
143
144	mSDLDeinit(&renderer);
145
146	freeArguments(&args);
147	mCoreConfigFreeOpts(&opts);
148	mCoreConfigDeinit(&renderer.core->config);
149	renderer.core->deinit(renderer.core);
150
151	return ret;
152}
153
154int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
155	struct mCoreThread thread = {
156		.core = renderer->core
157	};
158	if (!mCoreLoadFile(renderer->core, args->fname)) {
159		return 1;
160	}
161	mCoreAutoloadSave(renderer->core);
162#ifdef USE_DEBUGGERS
163	struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core);
164	if (debugger) {
165#ifdef USE_EDITLINE
166		if (args->debuggerType == DEBUGGER_CLI) {
167			struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
168			CLIDebuggerAttachBackend(cliDebugger, CLIDebuggerEditLineBackendCreate());
169		}
170#endif
171		mDebuggerAttach(debugger, renderer->core);
172		mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL);
173	}
174#endif
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 = !mCoreThreadStart(&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 (mSDLInitAudio(&renderer->audio, &thread)) {
195			renderer->runloop(renderer, &thread);
196			mSDLPauseAudio(&renderer->audio);
197			if (mCoreThreadHasCrashed(&thread)) {
198				didFail = true;
199				printf("The game crashed!\n");
200			}
201		} else {
202			didFail = true;
203			printf("Could not initialize audio.\n");
204		}
205#if SDL_VERSION_ATLEAST(2, 0, 0)
206		mSDLResumeScreensaver(&renderer->events);
207		mSDLSetScreensaverSuspendable(&renderer->events, false);
208#endif
209
210		mCoreThreadJoin(&thread);
211	} else {
212		printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
213	}
214	renderer->core->unloadROM(renderer->core);
215	return didFail;
216}
217
218static bool mSDLInit(struct mSDLRenderer* renderer) {
219	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
220		printf("Could not initialize video: %s\n", SDL_GetError());
221		return false;
222	}
223
224	return renderer->init(renderer);
225}
226
227static void mSDLDeinit(struct mSDLRenderer* renderer) {
228	mSDLDeinitEvents(&renderer->events);
229	mSDLDeinitAudio(&renderer->audio);
230#if SDL_VERSION_ATLEAST(2, 0, 0)
231	SDL_DestroyWindow(renderer->window);
232#endif
233
234	renderer->deinit(renderer);
235
236	SDL_Quit();
237}