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