src/platform/sdl/main.c (view raw)
1/* Copyright (c) 2013-2014 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 "gba-thread.h"
17#include "gba.h"
18#include "gba-config.h"
19#include "gba-video.h"
20#include "platform/commandline.h"
21#include "util/configuration.h"
22
23#include <SDL.h>
24
25#include <errno.h>
26#include <signal.h>
27#include <sys/time.h>
28
29#define PORT "sdl"
30
31static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer);
32static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer);
33
34int main(int argc, char** argv) {
35 struct SDLSoftwareRenderer renderer;
36 GBAVideoSoftwareRendererCreate(&renderer.d);
37
38 struct GBAInputMap inputMap;
39 GBAInputMapInit(&inputMap);
40
41 struct GBAConfig config;
42 GBAConfigInit(&config, PORT);
43 GBAConfigLoad(&config);
44
45 struct GBAOptions opts = {
46 .width = VIDEO_HORIZONTAL_PIXELS,
47 .height = VIDEO_VERTICAL_PIXELS,
48 .audioBuffers = 512,
49 .videoSync = false,
50 .audioSync = true,
51 };
52 GBAConfigLoadDefaults(&config, &opts);
53
54 struct GBAArguments args = {};
55 struct GraphicsOpts graphicsOpts = {};
56
57 struct SubParser subparser;
58
59 initParserForGraphics(&subparser, &graphicsOpts);
60 if (!parseArguments(&args, &config, argc, argv, &subparser)) {
61 usage(argv[0], subparser.usage);
62 freeArguments(&args);
63 GBAConfigFreeOpts(&opts);
64 GBAConfigDeinit(&config);
65 return 1;
66 }
67
68 GBAConfigMap(&config, &opts);
69
70 renderer.viewportWidth = opts.width;
71 renderer.viewportHeight = opts.height;
72#if SDL_VERSION_ATLEAST(2, 0, 0)
73 renderer.events.fullscreen = opts.fullscreen;
74 renderer.events.windowUpdated = 0;
75#endif
76 renderer.ratio = graphicsOpts.multiplier;
77
78 if (!_GBASDLInit(&renderer)) {
79 freeArguments(&args);
80 GBAConfigFreeOpts(&opts);
81 GBAConfigDeinit(&config);
82 return 1;
83 }
84
85 struct GBAThread context = {
86 .renderer = &renderer.d.d,
87 .userData = &renderer
88 };
89
90 context.debugger = createDebugger(&args, &context);
91
92 GBAMapOptionsToContext(&opts, &context);
93 GBAMapArgumentsToContext(&args, &context);
94
95 renderer.audio.samples = context.audioBuffers;
96 GBASDLInitAudio(&renderer.audio, &context);
97
98 renderer.events.bindings = &inputMap;
99 GBASDLInitBindings(&inputMap);
100 GBASDLInitEvents(&renderer.events);
101 GBASDLEventsLoadConfig(&renderer.events, &config.configTable); // TODO: Don't use this directly
102
103 GBAThreadStart(&context);
104
105 GBASDLRunloop(&context, &renderer);
106
107 GBAThreadJoin(&context);
108 freeArguments(&args);
109 GBAConfigFreeOpts(&opts);
110 GBAConfigDeinit(&config);
111 free(context.debugger);
112 GBAInputMapDeinit(&inputMap);
113
114 _GBASDLDeinit(&renderer);
115
116 return 0;
117}
118
119static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer) {
120 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
121 return false;
122 }
123
124 return GBASDLInit(renderer);
125}
126
127static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
128 free(renderer->d.outputBuffer);
129
130 GBASDLDeinitEvents(&renderer->events);
131 GBASDLDeinitAudio(&renderer->audio);
132#if SDL_VERSION_ATLEAST(2, 0, 0)
133 SDL_DestroyWindow(renderer->window);
134#endif
135
136 GBASDLDeinit(renderer);
137
138 SDL_Quit();
139
140}