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 "core/core.h"
17#include "core/config.h"
18#ifdef M_CORE_GBA
19#include "gba/gba.h"
20#include "gba/supervisor/thread.h"
21#include "gba/video.h"
22#endif
23#ifdef M_CORE_GB
24#include "gb/core.h"
25#include "gb/gb.h"
26#include "gb/video.h"
27#endif
28#include "platform/commandline.h"
29#include "util/configuration.h"
30#include "util/vfs.h"
31
32#include <SDL.h>
33
34#include <errno.h>
35#include <signal.h>
36#include <sys/time.h>
37
38#define PORT "sdl"
39
40// TODO: Move somewhere
41enum mPlatform {
42 PLATFORM_NONE = -1,
43 PLATFORM_GBA,
44 PLATFORM_GB
45};
46
47static bool mSDLInit(struct mSDLRenderer* renderer);
48static void mSDLDeinit(struct mSDLRenderer* renderer);
49
50// TODO: Clean up signatures
51#ifdef M_CORE_GBA
52static int mSDLRunGBA(struct mSDLRenderer* renderer, struct GBAArguments* args, struct GBAOptions* opts, struct mCoreConfig* config);
53#endif
54#ifdef M_CORE_GB
55static int mSDLRunGB(struct mSDLRenderer* renderer, struct GBAArguments* args);
56#endif
57
58
59int main(int argc, char** argv) {
60 struct mSDLRenderer renderer = {};
61
62 struct mInputMap inputMap;
63 mInputMapInit(&inputMap, &GBAInputInfo);
64
65 struct mCoreConfig config;
66 mCoreConfigInit(&config, PORT);
67 mCoreConfigLoad(&config);
68
69 struct GBAOptions opts = {
70 .width = 0,
71 .height = 0,
72 .useBios = true,
73 .rewindEnable = true,
74 .audioBuffers = 512,
75 .videoSync = false,
76 .audioSync = true,
77 };
78
79 struct GBAArguments args;
80 struct GraphicsOpts graphicsOpts;
81
82 struct SubParser subparser;
83
84 initParserForGraphics(&subparser, &graphicsOpts);
85 bool parsed = parseArguments(&args, &config, argc, argv, &subparser);
86 if (!parsed || args.showHelp) {
87 usage(argv[0], subparser.usage);
88 freeArguments(&args);
89 mCoreConfigFreeOpts(&opts);
90 mCoreConfigDeinit(&config);
91 return !parsed;
92 }
93 if (args.showVersion) {
94 version(argv[0]);
95 freeArguments(&args);
96 mCoreConfigFreeOpts(&opts);
97 mCoreConfigDeinit(&config);
98 return 0;
99 }
100
101 enum mPlatform platform = PLATFORM_NONE;
102
103 if (args.fname) {
104 struct VFile* vf = VFileOpen(args.fname, O_RDONLY);
105 if (!vf) {
106 printf("Could not open game. Are you sure the file exists?\n");
107 freeArguments(&args);
108 mCoreConfigFreeOpts(&opts);
109 mCoreConfigDeinit(&config);
110 return 1;
111 }
112#ifdef M_CORE_GBA
113 else if (GBAIsROM(vf)) {
114 platform = PLATFORM_GBA;
115 if (!opts.width) {
116 opts.width = VIDEO_HORIZONTAL_PIXELS;
117 }
118 if (!opts.height) {
119 opts.height = VIDEO_VERTICAL_PIXELS;
120 }
121 GBAVideoSoftwareRendererCreate(&renderer.d);
122#ifdef BUILD_GL
123 mSDLGLCreate(&renderer);
124#elif defined(BUILD_GLES2) || defined(USE_EPOXY)
125 mSDLGLES2Create(&renderer);
126#else
127 mSDLSWCreate(&renderer);
128#endif
129 }
130#endif
131#ifdef M_CORE_GB
132 else if (GBIsROM(vf)) {
133 platform = PLATFORM_GB;
134 if (!opts.width) {
135 opts.width = /*GB_*/VIDEO_HORIZONTAL_PIXELS;
136 }
137 if (!opts.height) {
138 opts.height = /*GB_*/VIDEO_VERTICAL_PIXELS;
139 }
140 renderer.core = GBCoreCreate();
141#ifdef BUILD_GL
142 mSDLGLCreateGB(&renderer);
143#elif defined(BUILD_GLES2) || defined(USE_EPOXY)
144 mSDLGLES2CreateGB(&renderer);
145#else
146 mSDLSWCreateGB(&renderer);
147#endif
148 }
149#endif
150 else {
151 printf("Could not run game. Are you sure the file exists and is a Game Boy Advance game?\n");
152 freeArguments(&args);
153 mCoreConfigFreeOpts(&opts);
154 mCoreConfigDeinit(&config);
155 return 1;
156 }
157 }
158
159 mCoreConfigLoadDefaults(&config, &opts);
160 mCoreConfigMap(&config, &opts);
161
162 renderer.viewportWidth = opts.width;
163 renderer.viewportHeight = opts.height;
164#if SDL_VERSION_ATLEAST(2, 0, 0)
165 renderer.player.fullscreen = opts.fullscreen;
166 renderer.player.windowUpdated = 0;
167#else
168 renderer.fullscreen = opts.fullscreen;
169#endif
170 renderer.ratio = graphicsOpts.multiplier;
171 if (renderer.ratio == 0) {
172 renderer.ratio = 1;
173 }
174
175 renderer.lockAspectRatio = opts.lockAspectRatio;
176 renderer.filter = opts.resampleVideo;
177
178 if (!mSDLInit(&renderer)) {
179 freeArguments(&args);
180 mCoreConfigFreeOpts(&opts);
181 mCoreConfigDeinit(&config);
182 return 1;
183 }
184
185 if (renderer.core) {
186 // TODO: Check return code
187 renderer.core->init(renderer.core);
188 }
189
190 renderer.player.bindings = &inputMap;
191 GBASDLInitBindings(&inputMap);
192 GBASDLInitEvents(&renderer.events);
193 GBASDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&config));
194 GBASDLAttachPlayer(&renderer.events, &renderer.player);
195 GBASDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&config));
196
197 int ret;
198
199 switch (platform) {
200 case PLATFORM_GBA:
201 ret = mSDLRunGBA(&renderer, &args, &opts, &config);
202 break;
203 case PLATFORM_GB:
204 ret = mSDLRunGB(&renderer, &args);
205 break;
206 default:
207 ret = 1;
208 break;
209 }
210 GBASDLDetachPlayer(&renderer.events, &renderer.player);
211 mInputMapDeinit(&inputMap);
212
213 mSDLDeinit(&renderer);
214
215 freeArguments(&args);
216 mCoreConfigFreeOpts(&opts);
217 mCoreConfigDeinit(&config);
218
219 return ret;
220}
221
222#ifdef M_CORE_GBA
223int mSDLRunGBA(struct mSDLRenderer* renderer, struct GBAArguments* args, struct GBAOptions* opts, struct mCoreConfig* config) {
224 struct GBAThread context = {
225 .renderer = &renderer->d.d,
226 .userData = renderer
227 };
228
229 context.debugger = createDebugger(args, &context);
230
231 GBAMapOptionsToContext(opts, &context);
232 GBAMapArgumentsToContext(args, &context);
233 context.overrides = mCoreConfigGetOverrides(config);
234
235 bool didFail = false;
236
237 renderer->audio.samples = context.audioBuffers;
238 renderer->audio.sampleRate = 44100;
239 if (opts->sampleRate) {
240 renderer->audio.sampleRate = opts->sampleRate;
241 }
242 if (!GBSDLInitAudio(&renderer->audio, &context)) {
243 didFail = true;
244 }
245
246 if (!didFail) {
247#if SDL_VERSION_ATLEAST(2, 0, 0)
248 GBASDLSetScreensaverSuspendable(&renderer->events, opts->suspendScreensaver);
249 GBASDLSuspendScreensaver(&renderer->events);
250#endif
251 if (GBAThreadStart(&context)) {
252 renderer->audio.psg = &context.gba->audio.psg;
253 renderer->runloop(renderer, &context);
254 renderer->audio.psg = 0;
255 GBAThreadJoin(&context);
256 } else {
257 didFail = true;
258 printf("Could not run game. Are you sure the file exists and is a Game Boy Advance game?\n");
259 }
260
261#if SDL_VERSION_ATLEAST(2, 0, 0)
262 GBASDLResumeScreensaver(&renderer->events);
263 GBASDLSetScreensaverSuspendable(&renderer->events, false);
264#endif
265
266 if (GBAThreadHasCrashed(&context)) {
267 didFail = true;
268 printf("The game crashed!\n");
269 }
270 }
271 free(context.debugger);
272 mDirectorySetDeinit(&context.dirs);
273
274 return didFail;
275}
276#endif
277
278#ifdef M_CORE_GB
279int mSDLRunGB(struct mSDLRenderer* renderer, struct GBAArguments* args) {
280 struct VFile* vf = VFileOpen(args->fname, O_RDONLY);
281 struct VFile* savVf = 0;
282
283 renderer->audio.samples = 1024;
284 renderer->audio.sampleRate = 44100;
285
286 GBSDLInitAudio(&renderer->audio, 0);
287
288 {
289 char savepath[PATH_MAX];
290 char dirname[PATH_MAX];
291 char basename[PATH_MAX];
292 separatePath(args->fname, dirname, basename, 0);
293 snprintf(savepath, sizeof(savepath), "%s" PATH_SEP "%s.sav", dirname, basename);
294 savVf = VFileOpen(savepath, O_RDWR | O_CREAT);
295 }
296
297 renderer->core->loadROM(renderer->core, vf, savVf, args->fname);
298 renderer->core->reset(renderer->core);
299 renderer->audio.psg = 0;
300 GBSDLResumeAudio(&renderer->audio);
301 renderer->runloop(renderer, NULL);
302 renderer->core->unloadROM(renderer->core);
303 vf->close(vf);
304 return 0;
305}
306#endif
307
308static bool mSDLInit(struct mSDLRenderer* renderer) {
309 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
310 printf("Could not initialize video: %s\n", SDL_GetError());
311 return false;
312 }
313
314 return renderer->init(renderer);
315}
316
317static void mSDLDeinit(struct mSDLRenderer* renderer) {
318 GBASDLDeinitEvents(&renderer->events);
319 GBSDLDeinitAudio(&renderer->audio);
320#if SDL_VERSION_ATLEAST(2, 0, 0)
321 SDL_DestroyWindow(renderer->window);
322#endif
323
324 renderer->deinit(renderer);
325
326 SDL_Quit();
327}