all repos — mgba @ b865d8e479ec16288fa51a40ebf20a91c74dd57d

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