all repos — mgba @ b8fe87324f8987db99ede4b3332a9ea781726e85

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		.useBios = true,
 49		.rewindEnable = true,
 50		.audioBuffers = 512,
 51		.videoSync = false,
 52		.audioSync = true,
 53	};
 54	GBAConfigLoadDefaults(&config, &opts);
 55
 56	struct GBAArguments args;
 57	struct GraphicsOpts graphicsOpts;
 58
 59	struct SubParser subparser;
 60
 61	initParserForGraphics(&subparser, &graphicsOpts);
 62	if (!parseArguments(&args, &config, argc, argv, &subparser)) {
 63		usage(argv[0], subparser.usage);
 64		freeArguments(&args);
 65		GBAConfigFreeOpts(&opts);
 66		GBAConfigDeinit(&config);
 67		return 1;
 68	}
 69
 70	GBAConfigMap(&config, &opts);
 71
 72	renderer.viewportWidth = opts.width;
 73	renderer.viewportHeight = opts.height;
 74#if SDL_VERSION_ATLEAST(2, 0, 0)
 75	renderer.events.fullscreen = opts.fullscreen;
 76	renderer.events.windowUpdated = 0;
 77#endif
 78	renderer.ratio = graphicsOpts.multiplier;
 79	if (renderer.ratio == 0) {
 80		renderer.ratio = 1;
 81	}
 82
 83	renderer.lockAspectRatio = opts.lockAspectRatio;
 84	renderer.filter = opts.resampleVideo;
 85
 86	if (!_GBASDLInit(&renderer)) {
 87		freeArguments(&args);
 88		GBAConfigFreeOpts(&opts);
 89		GBAConfigDeinit(&config);
 90		return 1;
 91	}
 92
 93	struct GBAThread context = {
 94		.renderer = &renderer.d.d,
 95		.userData = &renderer
 96	};
 97
 98	context.debugger = createDebugger(&args, &context);
 99
100	GBAMapOptionsToContext(&opts, &context);
101	GBAMapArgumentsToContext(&args, &context);
102
103	renderer.audio.samples = context.audioBuffers;
104	GBASDLInitAudio(&renderer.audio, &context);
105
106	renderer.events.bindings = &inputMap;
107	GBASDLInitBindings(&inputMap);
108	GBASDLInitEvents(&renderer.events);
109	GBASDLEventsLoadConfig(&renderer.events, GBAConfigGetInput(&config));
110	context.overrides = GBAConfigGetOverrides(&config);
111
112	int didFail = 0;
113	if (GBAThreadStart(&context)) {
114		GBASDLRunloop(&context, &renderer);
115		GBAThreadJoin(&context);
116	} else {
117		didFail = 1;
118		printf("Could not run game. Are you sure the file exists and is a Game Boy Advance game?\n");
119	}
120
121	if (GBAThreadHasCrashed(&context)) {
122		didFail = 1;
123		printf("The game crashed!\n");
124	}
125	freeArguments(&args);
126	GBAConfigFreeOpts(&opts);
127	GBAConfigDeinit(&config);
128	free(context.debugger);
129	GBAInputMapDeinit(&inputMap);
130
131	_GBASDLDeinit(&renderer);
132
133	return didFail;
134}
135
136static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer) {
137	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
138		printf("Could not initialize video: %s\n", SDL_GetError());
139		return false;
140	}
141
142	return GBASDLInit(renderer);
143}
144
145static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
146	GBASDLDeinitEvents(&renderer->events);
147	GBASDLDeinitAudio(&renderer->audio);
148#if SDL_VERSION_ATLEAST(2, 0, 0)
149	SDL_DestroyWindow(renderer->window);
150#endif
151
152	GBASDLDeinit(renderer);
153
154	SDL_Quit();
155
156}