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#include <mgba/internal/debugger/cli-debugger.h>
9
10#ifdef USE_GDB_STUB
11#include <mgba/internal/debugger/gdb-stub.h>
12#endif
13#ifdef USE_EDITLINE
14#include "feature/editline/cli-el-backend.h"
15#endif
16#ifdef ENABLE_SCRIPTING
17#include <mgba/core/scripting.h>
18
19#ifdef ENABLE_PYTHON
20#include "platform/python/engine.h"
21#endif
22#endif
23
24#include <mgba/core/cheats.h>
25#include <mgba/core/core.h>
26#include <mgba/core/config.h>
27#include <mgba/core/input.h>
28#include <mgba/core/serialize.h>
29#include <mgba/core/thread.h>
30#include <mgba/internal/gba/input.h>
31
32#include <mgba/feature/commandline.h>
33#include <mgba-util/vfs.h>
34
35#include <SDL.h>
36
37#include <errno.h>
38#include <signal.h>
39
40#define PORT "sdl"
41
42static bool mSDLInit(struct mSDLRenderer* renderer);
43static void mSDLDeinit(struct mSDLRenderer* renderer);
44
45static int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args);
46
47static struct VFile* _state = NULL;
48
49static void _loadState(struct mCoreThread* thread) {
50 mCoreLoadStateNamed(thread->core, _state, SAVESTATE_RTC);
51}
52
53int main(int argc, char** argv) {
54 struct mSDLRenderer renderer = {0};
55
56 struct mCoreOptions opts = {
57 .useBios = true,
58 .rewindEnable = true,
59 .rewindBufferCapacity = 600,
60 .rewindSave = true,
61 .audioBuffers = 1024,
62 .videoSync = false,
63 .audioSync = true,
64 .volume = 0x100,
65 };
66
67 struct mArguments args;
68 struct mGraphicsOpts graphicsOpts;
69
70 struct mSubParser subparser;
71
72 initParserForGraphics(&subparser, &graphicsOpts);
73 bool parsed = parseArguments(&args, argc, argv, &subparser);
74 if (!args.fname && !args.showVersion) {
75 parsed = false;
76 }
77 if (!parsed || args.showHelp) {
78 usage(argv[0], subparser.usage);
79 freeArguments(&args);
80 return !parsed;
81 }
82 if (args.showVersion) {
83 version(argv[0]);
84 freeArguments(&args);
85 return 0;
86 }
87
88 renderer.core = mCoreFind(args.fname);
89 if (!renderer.core) {
90 printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
91 freeArguments(&args);
92 return 1;
93 }
94 renderer.core->desiredVideoDimensions(renderer.core, &renderer.width, &renderer.height);
95#ifdef BUILD_GL
96 mSDLGLCreate(&renderer);
97#elif defined(BUILD_GLES2) || defined(USE_EPOXY)
98 mSDLGLES2Create(&renderer);
99#else
100 mSDLSWCreate(&renderer);
101#endif
102
103 renderer.ratio = graphicsOpts.multiplier;
104 if (renderer.ratio == 0) {
105 renderer.ratio = 1;
106 }
107 opts.width = renderer.width * renderer.ratio;
108 opts.height = renderer.height * renderer.ratio;
109
110 if (!renderer.core->init(renderer.core)) {
111 freeArguments(&args);
112 return 1;
113 }
114
115 struct mCheatDevice* device = NULL;
116 if (args.cheatsFile && (device = renderer.core->cheatDevice(renderer.core))) {
117 struct VFile* vf = VFileOpen(args.cheatsFile, O_RDONLY);
118 if (vf) {
119 mCheatDeviceClear(device);
120 mCheatParseFile(device, vf);
121 vf->close(vf);
122 }
123 }
124
125 mInputMapInit(&renderer.core->inputMap, &GBAInputInfo);
126 mCoreInitConfig(renderer.core, PORT);
127 applyArguments(&args, &subparser, &renderer.core->config);
128
129 mCoreConfigLoadDefaults(&renderer.core->config, &opts);
130 mCoreLoadConfig(renderer.core);
131
132 renderer.viewportWidth = renderer.core->opts.width;
133 renderer.viewportHeight = renderer.core->opts.height;
134#if SDL_VERSION_ATLEAST(2, 0, 0)
135 renderer.player.fullscreen = renderer.core->opts.fullscreen;
136 renderer.player.windowUpdated = 0;
137#else
138 renderer.fullscreen = renderer.core->opts.fullscreen;
139#endif
140
141 renderer.lockAspectRatio = renderer.core->opts.lockAspectRatio;
142 renderer.lockIntegerScaling = renderer.core->opts.lockIntegerScaling;
143 renderer.filter = renderer.core->opts.resampleVideo;
144
145 if (!mSDLInit(&renderer)) {
146 freeArguments(&args);
147 renderer.core->deinit(renderer.core);
148 return 1;
149 }
150
151 renderer.player.bindings = &renderer.core->inputMap;
152 mSDLInitBindingsGBA(&renderer.core->inputMap);
153 mSDLInitEvents(&renderer.events);
154 mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&renderer.core->config));
155 mSDLAttachPlayer(&renderer.events, &renderer.player);
156 mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config));
157
158#if SDL_VERSION_ATLEAST(2, 0, 0)
159 renderer.core->setPeripheral(renderer.core, mPERIPH_RUMBLE, &renderer.player.rumble.d);
160#endif
161
162 int ret;
163
164 // TODO: Use opts and config
165 ret = mSDLRun(&renderer, &args);
166 mSDLDetachPlayer(&renderer.events, &renderer.player);
167 mInputMapDeinit(&renderer.core->inputMap);
168
169 if (device) {
170 mCheatDeviceDestroy(device);
171 }
172
173 mSDLDeinit(&renderer);
174
175 freeArguments(&args);
176 mCoreConfigFreeOpts(&opts);
177 mCoreConfigDeinit(&renderer.core->config);
178 renderer.core->deinit(renderer.core);
179
180 return ret;
181}
182
183int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
184 struct mCoreThread thread = {
185 .core = renderer->core
186 };
187 if (!mCoreLoadFile(renderer->core, args->fname)) {
188 return 1;
189 }
190 mCoreAutoloadSave(renderer->core);
191 mCoreAutoloadCheats(renderer->core);
192#ifdef ENABLE_SCRIPTING
193 struct mScriptBridge* bridge = mScriptBridgeCreate();
194#ifdef ENABLE_PYTHON
195 mPythonSetup(bridge);
196#endif
197#endif
198
199#ifdef USE_DEBUGGERS
200 struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core);
201 if (debugger) {
202#ifdef USE_EDITLINE
203 if (args->debuggerType == DEBUGGER_CLI) {
204 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
205 CLIDebuggerAttachBackend(cliDebugger, CLIDebuggerEditLineBackendCreate());
206 }
207#endif
208 mDebuggerAttach(debugger, renderer->core);
209 mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL);
210 #ifdef ENABLE_SCRIPTING
211 mScriptBridgeSetDebugger(bridge, debugger);
212#endif
213 }
214#endif
215
216 if (args->patch) {
217 struct VFile* patch = VFileOpen(args->patch, O_RDONLY);
218 if (patch) {
219 renderer->core->loadPatch(renderer->core, patch);
220 }
221 } else {
222 mCoreAutoloadPatch(renderer->core);
223 }
224
225 renderer->audio.samples = renderer->core->opts.audioBuffers;
226 renderer->audio.sampleRate = 44100;
227
228 bool didFail = !mCoreThreadStart(&thread);
229 if (!didFail) {
230#if SDL_VERSION_ATLEAST(2, 0, 0)
231 renderer->core->desiredVideoDimensions(renderer->core, &renderer->width, &renderer->height);
232 unsigned width = renderer->width * renderer->ratio;
233 unsigned height = renderer->height * renderer->ratio;
234 if (width != (unsigned) renderer->viewportWidth && height != (unsigned) renderer->viewportHeight) {
235 SDL_SetWindowSize(renderer->window, width, height);
236 renderer->player.windowUpdated = 1;
237 }
238 mSDLSetScreensaverSuspendable(&renderer->events, renderer->core->opts.suspendScreensaver);
239 mSDLSuspendScreensaver(&renderer->events);
240#endif
241 if (mSDLInitAudio(&renderer->audio, &thread)) {
242 if (args->savestate) {
243 struct VFile* state = VFileOpen(args->savestate, O_RDONLY);
244 if (state) {
245 _state = state;
246 mCoreThreadRunFunction(&thread, _loadState);
247 _state = NULL;
248 state->close(state);
249 }
250 }
251 renderer->runloop(renderer, &thread);
252 mSDLPauseAudio(&renderer->audio);
253 if (mCoreThreadHasCrashed(&thread)) {
254 didFail = true;
255 printf("The game crashed!\n");
256 }
257 } else {
258 didFail = true;
259 printf("Could not initialize audio.\n");
260 }
261#if SDL_VERSION_ATLEAST(2, 0, 0)
262 mSDLResumeScreensaver(&renderer->events);
263 mSDLSetScreensaverSuspendable(&renderer->events, false);
264#endif
265
266 mCoreThreadJoin(&thread);
267 } else {
268 printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
269 }
270 renderer->core->unloadROM(renderer->core);
271
272#ifdef ENABLE_SCRIPTING
273 mScriptBridgeDestroy(bridge);
274#endif
275
276 return didFail;
277}
278
279static bool mSDLInit(struct mSDLRenderer* renderer) {
280 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
281 printf("Could not initialize video: %s\n", SDL_GetError());
282 return false;
283 }
284
285 return renderer->init(renderer);
286}
287
288static void mSDLDeinit(struct mSDLRenderer* renderer) {
289 mSDLDeinitEvents(&renderer->events);
290 mSDLDeinitAudio(&renderer->audio);
291#if SDL_VERSION_ATLEAST(2, 0, 0)
292 SDL_DestroyWindow(renderer->window);
293#endif
294
295 renderer->deinit(renderer);
296
297 SDL_Quit();
298}