all repos — mgba @ 6a77d2aaeafd999f2b7258db70f377d44305ef08

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 "feature/commandline.h"
 24#include <mgba-util/vfs.h>
 25
 26#include <SDL.h>
 27
 28#include <errno.h>
 29#include <signal.h>
 30#include <sys/time.h>
 31
 32#define PORT "sdl"
 33
 34static bool mSDLInit(struct mSDLRenderer* renderer);
 35static void mSDLDeinit(struct mSDLRenderer* renderer);
 36
 37static int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args);
 38
 39int main(int argc, char** argv) {
 40	struct mSDLRenderer renderer = {};
 41
 42	struct mCoreOptions opts = {
 43		.useBios = true,
 44		.rewindEnable = true,
 45		.rewindBufferCapacity = 600,
 46		.rewindSave = true,
 47		.audioBuffers = 1024,
 48		.videoSync = false,
 49		.audioSync = true,
 50		.volume = 0x100,
 51	};
 52
 53	struct mArguments args;
 54	struct mGraphicsOpts graphicsOpts;
 55
 56	struct mSubParser subparser;
 57
 58	initParserForGraphics(&subparser, &graphicsOpts);
 59	bool parsed = parseArguments(&args, argc, argv, &subparser);
 60	if (!args.fname) {
 61		parsed = false;
 62	}
 63	if (!parsed || args.showHelp) {
 64		usage(argv[0], subparser.usage);
 65		freeArguments(&args);
 66		return !parsed;
 67	}
 68	if (args.showVersion) {
 69		version(argv[0]);
 70		freeArguments(&args);
 71		return 0;
 72	}
 73
 74	renderer.core = mCoreFind(args.fname);
 75	if (!renderer.core) {
 76		printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
 77		freeArguments(&args);
 78		return 1;
 79	}
 80	renderer.core->desiredVideoDimensions(renderer.core, &renderer.width, &renderer.height);
 81#ifdef BUILD_GL
 82	mSDLGLCreate(&renderer);
 83#elif defined(BUILD_GLES2) || defined(USE_EPOXY)
 84	mSDLGLES2Create(&renderer);
 85#else
 86	mSDLSWCreate(&renderer);
 87#endif
 88
 89	renderer.ratio = graphicsOpts.multiplier;
 90	if (renderer.ratio == 0) {
 91		renderer.ratio = 1;
 92	}
 93	opts.width = renderer.width * renderer.ratio;
 94	opts.height = renderer.height * renderer.ratio;
 95
 96	if (!renderer.core->init(renderer.core)) {
 97		freeArguments(&args);
 98		return 1;
 99	}
100
101	mInputMapInit(&renderer.core->inputMap, &GBAInputInfo);
102	mCoreInitConfig(renderer.core, PORT);
103	applyArguments(&args, &subparser, &renderer.core->config);
104
105	mCoreConfigLoadDefaults(&renderer.core->config, &opts);
106	mCoreLoadConfig(renderer.core);
107
108	renderer.viewportWidth = renderer.core->opts.width;
109	renderer.viewportHeight = renderer.core->opts.height;
110#if SDL_VERSION_ATLEAST(2, 0, 0)
111	renderer.player.fullscreen = renderer.core->opts.fullscreen;
112	renderer.player.windowUpdated = 0;
113#else
114	renderer.fullscreen = renderer.core->opts.fullscreen;
115#endif
116
117	renderer.lockAspectRatio = renderer.core->opts.lockAspectRatio;
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->setRumble(renderer.core, &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}