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