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 bool parsed = parseArguments(&args, &config, argc, argv, &subparser);
63 if (!parsed || args.showHelp) {
64 usage(argv[0], subparser.usage);
65 freeArguments(&args);
66 GBAConfigFreeOpts(&opts);
67 GBAConfigDeinit(&config);
68 return !parsed;
69 }
70
71 GBAConfigMap(&config, &opts);
72
73 renderer.viewportWidth = opts.width;
74 renderer.viewportHeight = opts.height;
75#if SDL_VERSION_ATLEAST(2, 0, 0)
76 renderer.player.fullscreen = opts.fullscreen;
77 renderer.player.windowUpdated = 0;
78#endif
79 renderer.ratio = graphicsOpts.multiplier;
80 if (renderer.ratio == 0) {
81 renderer.ratio = 1;
82 }
83
84 renderer.lockAspectRatio = opts.lockAspectRatio;
85 renderer.filter = opts.resampleVideo;
86
87#ifdef BUILD_GL
88 GBASDLGLCreate(&renderer);
89#elif defined(BUILD_GLES2)
90 GBASDLGLES2Create(&renderer);
91#else
92 GBASDLSWCreate(&renderer);
93#endif
94
95 if (!GBASDLInit(&renderer)) {
96 freeArguments(&args);
97 GBAConfigFreeOpts(&opts);
98 GBAConfigDeinit(&config);
99 return 1;
100 }
101
102 struct GBAThread context = {
103 .renderer = &renderer.d.d,
104 .userData = &renderer
105 };
106
107 context.debugger = createDebugger(&args, &context);
108
109 GBAMapOptionsToContext(&opts, &context);
110 GBAMapArgumentsToContext(&args, &context);
111
112 bool didFail = false;
113
114 renderer.audio.samples = context.audioBuffers;
115 renderer.audio.sampleRate = 44100;
116 if (opts.sampleRate) {
117 renderer.audio.sampleRate = opts.sampleRate;
118 }
119 if (!GBASDLInitAudio(&renderer.audio, &context)) {
120 didFail = true;
121 }
122
123 renderer.player.bindings = &inputMap;
124 GBASDLInitBindings(&inputMap);
125 GBASDLInitEvents(&renderer.events);
126 GBASDLEventsLoadConfig(&renderer.events, GBAConfigGetInput(&config));
127 GBASDLAttachPlayer(&renderer.events, &renderer.player);
128 GBASDLPlayerLoadConfig(&renderer.player, GBAConfigGetInput(&config));
129 context.overrides = GBAConfigGetOverrides(&config);
130
131 if (!didFail) {
132#if SDL_VERSION_ATLEAST(2, 0, 0)
133 GBASDLSetScreensaverSuspendable(&renderer.events, opts.suspendScreensaver);
134 GBASDLSuspendScreensaver(&renderer.events);
135#endif
136 if (GBAThreadStart(&context)) {
137 renderer.runloop(&context, &renderer);
138 GBAThreadJoin(&context);
139 } else {
140 didFail = true;
141 printf("Could not run game. Are you sure the file exists and is a Game Boy Advance game?\n");
142 }
143
144#if SDL_VERSION_ATLEAST(2, 0, 0)
145 GBASDLResumeScreensaver(&renderer.events);
146 GBASDLSetScreensaverSuspendable(&renderer.events, false);
147#endif
148
149 if (GBAThreadHasCrashed(&context)) {
150 didFail = true;
151 printf("The game crashed!\n");
152 }
153 }
154 freeArguments(&args);
155 GBAConfigFreeOpts(&opts);
156 GBAConfigDeinit(&config);
157 free(context.debugger);
158 GBASDLDetachPlayer(&renderer.events, &renderer.player);
159 GBAInputMapDeinit(&inputMap);
160
161 GBASDLDeinit(&renderer);
162
163 return didFail;
164}
165
166static bool GBASDLInit(struct SDLSoftwareRenderer* renderer) {
167 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
168 printf("Could not initialize video: %s\n", SDL_GetError());
169 return false;
170 }
171
172 return renderer->init(renderer);
173}
174
175static void GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
176 GBASDLDeinitEvents(&renderer->events);
177 GBASDLDeinitAudio(&renderer->audio);
178#if SDL_VERSION_ATLEAST(2, 0, 0)
179 SDL_DestroyWindow(renderer->window);
180#endif
181
182 renderer->deinit(renderer);
183
184 SDL_Quit();
185}