all repos — mgba @ c937529d4ab5bb328923cc7654de5518ce8c111e

mGBA Game Boy Advance Emulator

src/platform/sdl/main.c (view raw)

  1/* Copyright (c) 2013-2014 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-thread.h"
 17#include "gba.h"
 18#include "gba-config.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, &config.configTable); // TODO: Don't use this directly
106
107	GBAThreadStart(&context);
108
109	GBASDLRunloop(&context, &renderer);
110
111	GBAThreadJoin(&context);
112	if (GBAThreadHasCrashed(&context)) {
113		printf("The game crashed!\n");
114	}
115	freeArguments(&args);
116	GBAConfigFreeOpts(&opts);
117	GBAConfigDeinit(&config);
118	free(context.debugger);
119	GBAInputMapDeinit(&inputMap);
120
121	_GBASDLDeinit(&renderer);
122
123	return 0;
124}
125
126static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer) {
127	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
128		printf("Could not initialize video: %s\n", SDL_GetError());
129		return false;
130	}
131
132	return GBASDLInit(renderer);
133}
134
135static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
136	free(renderer->d.outputBuffer);
137
138	GBASDLDeinitEvents(&renderer->events);
139	GBASDLDeinitAudio(&renderer->audio);
140#if SDL_VERSION_ATLEAST(2, 0, 0)
141	SDL_DestroyWindow(renderer->window);
142#endif
143
144	GBASDLDeinit(renderer);
145
146	SDL_Quit();
147
148}