all repos — mgba @ 509c80abad8221564a0bfade571d868e9e8247af

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		mCoreConfigDeinit(&renderer.core->config);
147		renderer.core->deinit(renderer.core);
148		return 1;
149	}
150
151	renderer.player.bindings = &renderer.core->inputMap;
152	mSDLInitBindingsGBA(&renderer.core->inputMap);
153	mSDLInitEvents(&renderer.events);
154	mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&renderer.core->config));
155	mSDLAttachPlayer(&renderer.events, &renderer.player);
156	mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config));
157
158#if SDL_VERSION_ATLEAST(2, 0, 0)
159	renderer.core->setPeripheral(renderer.core, mPERIPH_RUMBLE, &renderer.player.rumble.d);
160#endif
161
162	int ret;
163
164	// TODO: Use opts and config
165	ret = mSDLRun(&renderer, &args);
166	mSDLDetachPlayer(&renderer.events, &renderer.player);
167	mInputMapDeinit(&renderer.core->inputMap);
168
169	if (device) {
170		mCheatDeviceDestroy(device);
171	}
172
173	mSDLDeinit(&renderer);
174
175	freeArguments(&args);
176	mCoreConfigFreeOpts(&opts);
177	mCoreConfigDeinit(&renderer.core->config);
178	renderer.core->deinit(renderer.core);
179
180	return ret;
181}
182
183#ifdef _WIN32
184#include <mgba-util/string.h>
185
186int wmain(int argc, wchar_t** argv) {
187	char** argv8 = malloc(sizeof(char*) * argc);
188	int i;
189	for (i = 0; i < argc; ++i) {
190		argv8[i] = utf16to8((uint16_t*) argv[i], wcslen(argv[i]) * 2);
191	}
192	int ret = main(argc, argv8);
193	for (i = 0; i < argc; ++i) {
194		free(argv8[i]);
195	}
196	free(argv8);
197	return ret;
198}
199#endif
200
201int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
202	struct mCoreThread thread = {
203		.core = renderer->core
204	};
205	if (!mCoreLoadFile(renderer->core, args->fname)) {
206		return 1;
207	}
208	mCoreAutoloadSave(renderer->core);
209	mCoreAutoloadCheats(renderer->core);
210#ifdef ENABLE_SCRIPTING
211	struct mScriptBridge* bridge = mScriptBridgeCreate();
212#ifdef ENABLE_PYTHON
213	mPythonSetup(bridge);
214#endif
215#endif
216
217#ifdef USE_DEBUGGERS
218	struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core);
219	if (debugger) {
220#ifdef USE_EDITLINE
221		if (args->debuggerType == DEBUGGER_CLI) {
222			struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
223			CLIDebuggerAttachBackend(cliDebugger, CLIDebuggerEditLineBackendCreate());
224		}
225#endif
226		mDebuggerAttach(debugger, renderer->core);
227		mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL);
228 #ifdef ENABLE_SCRIPTING
229		mScriptBridgeSetDebugger(bridge, debugger);
230#endif
231	}
232#endif
233
234	if (args->patch) {
235		struct VFile* patch = VFileOpen(args->patch, O_RDONLY);
236		if (patch) {
237			renderer->core->loadPatch(renderer->core, patch);
238		}
239	} else {
240		mCoreAutoloadPatch(renderer->core);
241	}
242
243	renderer->audio.samples = renderer->core->opts.audioBuffers;
244	renderer->audio.sampleRate = 44100;
245
246	bool didFail = !mCoreThreadStart(&thread);
247	if (!didFail) {
248#if SDL_VERSION_ATLEAST(2, 0, 0)
249		renderer->core->desiredVideoDimensions(renderer->core, &renderer->width, &renderer->height);
250		unsigned width = renderer->width * renderer->ratio;
251		unsigned height = renderer->height * renderer->ratio;
252		if (width != (unsigned) renderer->viewportWidth && height != (unsigned) renderer->viewportHeight) {
253			SDL_SetWindowSize(renderer->window, width, height);
254			renderer->player.windowUpdated = 1;
255		}
256		mSDLSetScreensaverSuspendable(&renderer->events, renderer->core->opts.suspendScreensaver);
257		mSDLSuspendScreensaver(&renderer->events);
258#endif
259		if (mSDLInitAudio(&renderer->audio, &thread)) {
260			if (args->savestate) {
261				struct VFile* state = VFileOpen(args->savestate, O_RDONLY);
262				if (state) {
263					_state = state;
264					mCoreThreadRunFunction(&thread, _loadState);
265					_state = NULL;
266					state->close(state);
267				}
268			}
269			renderer->runloop(renderer, &thread);
270			mSDLPauseAudio(&renderer->audio);
271			if (mCoreThreadHasCrashed(&thread)) {
272				didFail = true;
273				printf("The game crashed!\n");
274			}
275		} else {
276			didFail = true;
277			printf("Could not initialize audio.\n");
278		}
279#if SDL_VERSION_ATLEAST(2, 0, 0)
280		mSDLResumeScreensaver(&renderer->events);
281		mSDLSetScreensaverSuspendable(&renderer->events, false);
282#endif
283
284		mCoreThreadJoin(&thread);
285	} else {
286		printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
287	}
288	renderer->core->unloadROM(renderer->core);
289
290#ifdef ENABLE_SCRIPTING
291	mScriptBridgeDestroy(bridge);
292#endif
293
294	return didFail;
295}
296
297static bool mSDLInit(struct mSDLRenderer* renderer) {
298	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
299		printf("Could not initialize video: %s\n", SDL_GetError());
300		return false;
301	}
302
303	return renderer->init(renderer);
304}
305
306static void mSDLDeinit(struct mSDLRenderer* renderer) {
307	mSDLDeinitEvents(&renderer->events);
308	mSDLDeinitAudio(&renderer->audio);
309#if SDL_VERSION_ATLEAST(2, 0, 0)
310	SDL_DestroyWindow(renderer->window);
311#endif
312
313	renderer->deinit(renderer);
314
315	SDL_Quit();
316}