all repos — mgba @ a8dae9b9d5546a2fdfdea3674e813a7883d913df

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#ifdef USE_CLI_DEBUGGER
  9#include "debugger/cli-debugger.h"
 10#endif
 11
 12#ifdef USE_GDB_STUB
 13#include "debugger/gdb-stub.h"
 14#endif
 15
 16#include "gba/gba.h"
 17#include "gba/supervisor/config.h"
 18#include "gba/supervisor/thread.h"
 19#include "gba/video.h"
 20#include "platform/commandline.h"
 21#include "util/configuration.h"
 22
 23#include <SDL.h>
 24
 25#include <errno.h>
 26#include <signal.h>
 27#include <sys/time.h>
 28
 29#define PORT "sdl"
 30
 31static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer);
 32static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer);
 33
 34int main(int argc, char** argv) {
 35	struct SDLSoftwareRenderer renderer;
 36	GBAVideoSoftwareRendererCreate(&renderer.d);
 37
 38	struct GBAInputMap inputMap;
 39	GBAInputMapInit(&inputMap);
 40
 41	struct GBAConfig config;
 42	GBAConfigInit(&config, PORT);
 43	GBAConfigLoad(&config);
 44
 45	struct GBAOptions opts = {
 46		.width = VIDEO_HORIZONTAL_PIXELS,
 47		.height = VIDEO_VERTICAL_PIXELS,
 48		.rewindEnable = true,
 49		.audioBuffers = 512,
 50		.videoSync = false,
 51		.audioSync = true,
 52	};
 53	GBAConfigLoadDefaults(&config, &opts);
 54
 55	struct GBAArguments args;
 56	struct GraphicsOpts graphicsOpts;
 57
 58	struct SubParser subparser;
 59
 60	initParserForGraphics(&subparser, &graphicsOpts);
 61	if (!parseArguments(&args, &config, argc, argv, &subparser)) {
 62		usage(argv[0], subparser.usage);
 63		freeArguments(&args);
 64		GBAConfigFreeOpts(&opts);
 65		GBAConfigDeinit(&config);
 66		return 1;
 67	}
 68
 69	GBAConfigMap(&config, &opts);
 70
 71	renderer.viewportWidth = opts.width;
 72	renderer.viewportHeight = opts.height;
 73#if SDL_VERSION_ATLEAST(2, 0, 0)
 74	renderer.events.fullscreen = opts.fullscreen;
 75	renderer.events.windowUpdated = 0;
 76#endif
 77	renderer.ratio = graphicsOpts.multiplier;
 78
 79	renderer.lockAspectRatio = opts.lockAspectRatio;
 80	renderer.filter = opts.resampleVideo;
 81
 82	if (!_GBASDLInit(&renderer)) {
 83		freeArguments(&args);
 84		GBAConfigFreeOpts(&opts);
 85		GBAConfigDeinit(&config);
 86		return 1;
 87	}
 88
 89	struct GBAThread context = {
 90		.renderer = &renderer.d.d,
 91		.userData = &renderer
 92	};
 93
 94	context.debugger = createDebugger(&args, &context);
 95
 96	GBAMapOptionsToContext(&opts, &context);
 97	GBAMapArgumentsToContext(&args, &context);
 98
 99	renderer.audio.samples = context.audioBuffers;
100	GBASDLInitAudio(&renderer.audio, &context);
101
102	renderer.events.bindings = &inputMap;
103	GBASDLInitBindings(&inputMap);
104	GBASDLInitEvents(&renderer.events);
105	GBASDLEventsLoadConfig(&renderer.events, GBAConfigGetInput(&config));
106	context.overrides = GBAConfigGetOverrides(&config);
107
108	int didFail = 0;
109	if (GBAThreadStart(&context)) {
110		GBASDLRunloop(&context, &renderer);
111		GBAThreadJoin(&context);
112	} else {
113		didFail = 1;
114		printf("Could not run game. Are you sure the file exists and is a Game Boy Advance game?\n");
115	}
116
117	if (GBAThreadHasCrashed(&context)) {
118		didFail = 1;
119		printf("The game crashed!\n");
120	}
121	freeArguments(&args);
122	GBAConfigFreeOpts(&opts);
123	GBAConfigDeinit(&config);
124	free(context.debugger);
125	GBAInputMapDeinit(&inputMap);
126
127	_GBASDLDeinit(&renderer);
128
129	return didFail;
130}
131
132static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer) {
133	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
134		printf("Could not initialize video: %s\n", SDL_GetError());
135		return false;
136	}
137
138	return GBASDLInit(renderer);
139}
140
141static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
142	free(renderer->d.outputBuffer);
143
144	GBASDLDeinitEvents(&renderer->events);
145	GBASDLDeinitAudio(&renderer->audio);
146#if SDL_VERSION_ATLEAST(2, 0, 0)
147	SDL_DestroyWindow(renderer->window);
148#endif
149
150	GBASDLDeinit(renderer);
151
152	SDL_Quit();
153
154}