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 .audioBuffers = 1024,
61 .videoSync = false,
62 .audioSync = true,
63 .volume = 0x100,
64 };
65
66 struct mArguments args;
67 struct mGraphicsOpts graphicsOpts;
68
69 struct mSubParser subparser;
70
71 initParserForGraphics(&subparser, &graphicsOpts);
72 bool parsed = parseArguments(&args, argc, argv, &subparser);
73 if (!args.fname && !args.showVersion) {
74 parsed = false;
75 }
76 if (!parsed || args.showHelp) {
77 usage(argv[0], subparser.usage);
78 freeArguments(&args);
79 return !parsed;
80 }
81 if (args.showVersion) {
82 version(argv[0]);
83 freeArguments(&args);
84 return 0;
85 }
86
87 renderer.core = mCoreFind(args.fname);
88 if (!renderer.core) {
89 printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
90 freeArguments(&args);
91 return 1;
92 }
93
94 if (!renderer.core->init(renderer.core)) {
95 freeArguments(&args);
96 return 1;
97 }
98
99 renderer.core->desiredVideoDimensions(renderer.core, &renderer.width, &renderer.height);
100#ifdef BUILD_GL
101 mSDLGLCreate(&renderer);
102#elif defined(BUILD_GLES2) || defined(USE_EPOXY)
103 mSDLGLES2Create(&renderer);
104#else
105 mSDLSWCreate(&renderer);
106#endif
107
108 renderer.ratio = graphicsOpts.multiplier;
109 if (renderer.ratio == 0) {
110 renderer.ratio = 1;
111 }
112 opts.width = renderer.width * renderer.ratio;
113 opts.height = renderer.height * renderer.ratio;
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 renderer.player.fullscreen = renderer.core->opts.fullscreen;
135 renderer.player.windowUpdated = 0;
136
137 renderer.lockAspectRatio = renderer.core->opts.lockAspectRatio;
138 renderer.lockIntegerScaling = renderer.core->opts.lockIntegerScaling;
139 renderer.filter = renderer.core->opts.resampleVideo;
140
141 if (!mSDLInit(&renderer)) {
142 freeArguments(&args);
143 mCoreConfigDeinit(&renderer.core->config);
144 renderer.core->deinit(renderer.core);
145 return 1;
146 }
147
148 renderer.player.bindings = &renderer.core->inputMap;
149 mSDLInitBindingsGBA(&renderer.core->inputMap);
150 mSDLInitEvents(&renderer.events);
151 mSDLEventsLoadConfig(&renderer.events, mCoreConfigGetInput(&renderer.core->config));
152 mSDLAttachPlayer(&renderer.events, &renderer.player);
153 mSDLPlayerLoadConfig(&renderer.player, mCoreConfigGetInput(&renderer.core->config));
154
155#if SDL_VERSION_ATLEAST(2, 0, 0)
156 renderer.core->setPeripheral(renderer.core, mPERIPH_RUMBLE, &renderer.player.rumble.d);
157#endif
158
159 int ret;
160
161 // TODO: Use opts and config
162 ret = mSDLRun(&renderer, &args);
163 mSDLDetachPlayer(&renderer.events, &renderer.player);
164 mInputMapDeinit(&renderer.core->inputMap);
165
166 if (device) {
167 mCheatDeviceDestroy(device);
168 }
169
170 mSDLDeinit(&renderer);
171
172 freeArguments(&args);
173 mCoreConfigFreeOpts(&opts);
174 mCoreConfigDeinit(&renderer.core->config);
175 renderer.core->deinit(renderer.core);
176
177 return ret;
178}
179
180#ifdef _WIN32
181#include <mgba-util/string.h>
182
183int wmain(int argc, wchar_t** argv) {
184 char** argv8 = malloc(sizeof(char*) * argc);
185 int i;
186 for (i = 0; i < argc; ++i) {
187 argv8[i] = utf16to8((uint16_t*) argv[i], wcslen(argv[i]) * 2);
188 }
189 int ret = main(argc, argv8);
190 for (i = 0; i < argc; ++i) {
191 free(argv8[i]);
192 }
193 free(argv8);
194 return ret;
195}
196#endif
197
198int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) {
199 struct mCoreThread thread = {
200 .core = renderer->core
201 };
202 if (!mCoreLoadFile(renderer->core, args->fname)) {
203 return 1;
204 }
205 mCoreAutoloadSave(renderer->core);
206 mCoreAutoloadCheats(renderer->core);
207#ifdef ENABLE_SCRIPTING
208 struct mScriptBridge* bridge = mScriptBridgeCreate();
209#ifdef ENABLE_PYTHON
210 mPythonSetup(bridge);
211#endif
212#endif
213
214#ifdef USE_DEBUGGERS
215 struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core);
216 if (debugger) {
217#ifdef USE_EDITLINE
218 if (args->debuggerType == DEBUGGER_CLI) {
219 struct CLIDebugger* cliDebugger = (struct CLIDebugger*) debugger;
220 CLIDebuggerAttachBackend(cliDebugger, CLIDebuggerEditLineBackendCreate());
221 }
222#endif
223 mDebuggerAttach(debugger, renderer->core);
224 mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL);
225 #ifdef ENABLE_SCRIPTING
226 mScriptBridgeSetDebugger(bridge, debugger);
227#endif
228 }
229#endif
230
231 if (args->patch) {
232 struct VFile* patch = VFileOpen(args->patch, O_RDONLY);
233 if (patch) {
234 renderer->core->loadPatch(renderer->core, patch);
235 }
236 } else {
237 mCoreAutoloadPatch(renderer->core);
238 }
239
240 renderer->audio.samples = renderer->core->opts.audioBuffers;
241 renderer->audio.sampleRate = 44100;
242
243 bool didFail = !mCoreThreadStart(&thread);
244 if (!didFail) {
245#if SDL_VERSION_ATLEAST(2, 0, 0)
246 renderer->core->desiredVideoDimensions(renderer->core, &renderer->width, &renderer->height);
247 unsigned width = renderer->width * renderer->ratio;
248 unsigned height = renderer->height * renderer->ratio;
249 if (width != (unsigned) renderer->viewportWidth && height != (unsigned) renderer->viewportHeight) {
250 SDL_SetWindowSize(renderer->window, width, height);
251 renderer->player.windowUpdated = 1;
252 }
253 mSDLSetScreensaverSuspendable(&renderer->events, renderer->core->opts.suspendScreensaver);
254 mSDLSuspendScreensaver(&renderer->events);
255#endif
256 if (mSDLInitAudio(&renderer->audio, &thread)) {
257 if (args->savestate) {
258 struct VFile* state = VFileOpen(args->savestate, O_RDONLY);
259 if (state) {
260 _state = state;
261 mCoreThreadRunFunction(&thread, _loadState);
262 _state = NULL;
263 state->close(state);
264 }
265 }
266 renderer->runloop(renderer, &thread);
267 mSDLPauseAudio(&renderer->audio);
268 if (mCoreThreadHasCrashed(&thread)) {
269 didFail = true;
270 printf("The game crashed!\n");
271 }
272 } else {
273 didFail = true;
274 printf("Could not initialize audio.\n");
275 }
276#if SDL_VERSION_ATLEAST(2, 0, 0)
277 mSDLResumeScreensaver(&renderer->events);
278 mSDLSetScreensaverSuspendable(&renderer->events, false);
279#endif
280
281 mCoreThreadJoin(&thread);
282 } else {
283 printf("Could not run game. Are you sure the file exists and is a compatible game?\n");
284 }
285 renderer->core->unloadROM(renderer->core);
286
287#ifdef ENABLE_SCRIPTING
288 mScriptBridgeDestroy(bridge);
289#endif
290
291 return didFail;
292}
293
294static bool mSDLInit(struct mSDLRenderer* renderer) {
295 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
296 printf("Could not initialize video: %s\n", SDL_GetError());
297 return false;
298 }
299
300 return renderer->init(renderer);
301}
302
303static void mSDLDeinit(struct mSDLRenderer* renderer) {
304 mSDLDeinitEvents(&renderer->events);
305 mSDLDeinitAudio(&renderer->audio);
306#if SDL_VERSION_ATLEAST(2, 0, 0)
307 SDL_DestroyWindow(renderer->window);
308#endif
309
310 renderer->deinit(renderer);
311
312 SDL_Quit();
313}