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