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#include "core/input.h"
19#include "core/thread.h"
20#include "gba/input.h"
21#ifdef M_CORE_GBA
22#include "gba/core.h"
23#include "gba/gba.h"
24#include "gba/video.h"
25#endif
26#ifdef M_CORE_GB
27#include "gb/core.h"
28#include "gb/gb.h"
29#include "gb/video.h"
30#endif
31#include "platform/commandline.h"
32#include "util/configuration.h"
33#include "util/vfs.h"
34
35#include <SDL.h>
36
37#include <errno.h>
38#include <signal.h>
39#include <sys/time.h>
40
41#define PORT "sdl"
42
43static bool mSDLInit(struct mSDLRenderer* renderer);
44static void mSDLDeinit(struct mSDLRenderer* renderer);
45
46static int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args);
47
48int main(int argc, char** argv) {
49 struct mSDLRenderer renderer = {};
50
51 struct mCoreOptions opts = {
52 .useBios = true,
53 .rewindEnable = true,
54 .audioBuffers = 512,
55 .videoSync = false,
56 .audioSync = true,
57 };
58
59 struct mArguments args;
60 struct mGraphicsOpts graphicsOpts;
61
62 struct mSubParser subparser;
63
64 initParserForGraphics(&subparser, &graphicsOpts);
65 bool parsed = parseArguments(&args, argc, argv, &subparser);
66 if (!parsed || args.showHelp) {
67 usage(argv[0], subparser.usage);
68 freeArguments(&args);
69 return !parsed;
70 }
71 if (args.showVersion) {
72 version(argv[0]);
73 freeArguments(&args);
74 return 0;
75 }
76
77 if (args.fname) {
78 renderer.core = mCoreFind(args.fname);
79 if (!renderer.core) {
80 printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
81 freeArguments(&args);
82 return 1;
83 }
84 renderer.core->desiredVideoDimensions(renderer.core, &renderer.width, &renderer.height);
85#ifdef BUILD_GL
86 mSDLGLCreate(&renderer);
87#elif defined(BUILD_GLES2) || defined(USE_EPOXY)
88 mSDLGLES2Create(&renderer);
89#else
90 mSDLSWCreate(&renderer);
91#endif
92 }
93
94 renderer.ratio = graphicsOpts.multiplier;
95 if (renderer.ratio == 0) {
96 renderer.ratio = 1;
97 }
98 opts.width = renderer.width * renderer.ratio;
99 opts.height = renderer.height * renderer.ratio;
100
101 if (!renderer.core->init(renderer.core)) {
102 freeArguments(&args);
103 return 1;
104 }
105
106 mInputMapInit(&renderer.core->inputMap, &GBAInputInfo);
107 mCoreInitConfig(renderer.core, PORT);
108 applyArguments(&args, &subparser, &renderer.core->config);
109
110 mCoreConfigLoadDefaults(&renderer.core->config, &opts);
111 mCoreLoadConfig(renderer.core);
112
113 renderer.viewportWidth = renderer.core->opts.width;
114 renderer.viewportHeight = renderer.core->opts.height;
115#if SDL_VERSION_ATLEAST(2, 0, 0)
116 renderer.player.fullscreen = renderer.core->opts.fullscreen;
117 renderer.player.windowUpdated = 0;
118#else
119 renderer.fullscreen = renderer.core->opts.fullscreen;
120#endif
121
122 renderer.lockAspectRatio = renderer.core->opts.lockAspectRatio;
123 renderer.filter = renderer.core->opts.resampleVideo;
124
125 if (!mSDLInit(&renderer)) {
126 freeArguments(&args);
127 renderer.core->deinit(renderer.core);
128 return 1;
129 }
130
131 renderer.player.bindings = &renderer.core->inputMap;
132 mSDLInitBindingsGBA(&renderer.core->inputMap);
133 mSDLInitEvents(&renderer.events);
134 mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&renderer.core->config));
135 mSDLAttachPlayer(&renderer.events, &renderer.player);
136 mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config));
137
138 int ret;
139
140 // TODO: Use opts and config
141 ret = mSDLRun(&renderer, &args);
142 mSDLDetachPlayer(&renderer.events, &renderer.player);
143 mInputMapDeinit(&renderer.core->inputMap);
144
145 mSDLDeinit(&renderer);
146
147 freeArguments(&args);
148 mCoreConfigFreeOpts(&opts);
149 mCoreConfigDeinit(&renderer.core->config);
150
151 return ret;
152}
153
154int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
155 struct mCoreThread thread = {
156 .core = renderer->core
157 };
158 if (!mCoreLoadFile(renderer->core, args->fname)) {
159 return 1;
160 }
161 mCoreAutoloadSave(renderer->core);
162 // TODO: Put back debugger
163
164 if (args->patch) {
165 struct VFile* patch = VFileOpen(args->patch, O_RDONLY);
166 if (patch) {
167 renderer->core->loadPatch(renderer->core, patch);
168 }
169 } else {
170 mCoreAutoloadPatch(renderer->core);
171 }
172
173 renderer->audio.samples = renderer->core->opts.audioBuffers;
174 renderer->audio.sampleRate = 44100;
175
176 bool didFail = !mSDLInitAudio(&renderer->audio, &thread);
177 if (!didFail) {
178#if SDL_VERSION_ATLEAST(2, 0, 0)
179 mSDLSetScreensaverSuspendable(&renderer->events, renderer->core->opts.suspendScreensaver);
180 mSDLSuspendScreensaver(&renderer->events);
181#endif
182 if (mCoreThreadStart(&thread)) {
183 renderer->runloop(renderer, &thread);
184 mCoreThreadJoin(&thread);
185 } else {
186 didFail = true;
187 printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
188 }
189
190#if SDL_VERSION_ATLEAST(2, 0, 0)
191 mSDLResumeScreensaver(&renderer->events);
192 mSDLSetScreensaverSuspendable(&renderer->events, false);
193#endif
194
195 if (mCoreThreadHasCrashed(&thread)) {
196 didFail = true;
197 printf("The game crashed!\n");
198 }
199 }
200 renderer->core->unloadROM(renderer->core);
201 return didFail;
202}
203
204static bool mSDLInit(struct mSDLRenderer* renderer) {
205 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
206 printf("Could not initialize video: %s\n", SDL_GetError());
207 return false;
208 }
209
210 return renderer->init(renderer);
211}
212
213static void mSDLDeinit(struct mSDLRenderer* renderer) {
214 mSDLDeinitEvents(&renderer->events);
215 mSDLDeinitAudio(&renderer->audio);
216#if SDL_VERSION_ATLEAST(2, 0, 0)
217 SDL_DestroyWindow(renderer->window);
218#endif
219
220 renderer->deinit(renderer);
221
222 SDL_Quit();
223}