all repos — mgba @ 2d49a41a30bca9ad47457ad8825fc13b9e82de81

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