all repos — mgba @ c364a6527a83d22bdd79e77286d0b53b0b624746

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