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 .rewindEnable = true,
49 .audioBuffers = 512,
50 .videoSync = false,
51 .audioSync = true,
52 };
53 GBAConfigLoadDefaults(&config, &opts);
54
55 struct GBAArguments args = {};
56 struct GraphicsOpts graphicsOpts = {};
57
58 struct SubParser subparser;
59
60 initParserForGraphics(&subparser, &graphicsOpts);
61 if (!parseArguments(&args, &config, argc, argv, &subparser)) {
62 usage(argv[0], subparser.usage);
63 freeArguments(&args);
64 GBAConfigFreeOpts(&opts);
65 GBAConfigDeinit(&config);
66 return 1;
67 }
68
69 GBAConfigMap(&config, &opts);
70
71 renderer.viewportWidth = opts.width;
72 renderer.viewportHeight = opts.height;
73#if SDL_VERSION_ATLEAST(2, 0, 0)
74 renderer.events.fullscreen = opts.fullscreen;
75 renderer.events.windowUpdated = 0;
76#endif
77 renderer.ratio = graphicsOpts.multiplier;
78
79 renderer.lockAspectRatio = opts.lockAspectRatio;
80 renderer.filter = opts.resampleVideo;
81
82 if (!_GBASDLInit(&renderer)) {
83 freeArguments(&args);
84 GBAConfigFreeOpts(&opts);
85 GBAConfigDeinit(&config);
86 return 1;
87 }
88
89 struct GBAThread context = {
90 .renderer = &renderer.d.d,
91 .userData = &renderer
92 };
93
94 context.debugger = createDebugger(&args, &context);
95
96 GBAMapOptionsToContext(&opts, &context);
97 GBAMapArgumentsToContext(&args, &context);
98
99 renderer.audio.samples = context.audioBuffers;
100 GBASDLInitAudio(&renderer.audio, &context);
101
102 renderer.events.bindings = &inputMap;
103 GBASDLInitBindings(&inputMap);
104 GBASDLInitEvents(&renderer.events);
105 GBASDLEventsLoadConfig(&renderer.events, &config.configTable); // TODO: Don't use this directly
106 context.overrides = &config.configTable;
107
108 GBAThreadStart(&context);
109
110 GBASDLRunloop(&context, &renderer);
111
112 GBAThreadJoin(&context);
113 if (GBAThreadHasCrashed(&context)) {
114 printf("The game crashed!\n");
115 }
116 freeArguments(&args);
117 GBAConfigFreeOpts(&opts);
118 GBAConfigDeinit(&config);
119 free(context.debugger);
120 GBAInputMapDeinit(&inputMap);
121
122 _GBASDLDeinit(&renderer);
123
124 return 0;
125}
126
127static bool _GBASDLInit(struct SDLSoftwareRenderer* renderer) {
128 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
129 printf("Could not initialize video: %s\n", SDL_GetError());
130 return false;
131 }
132
133 return GBASDLInit(renderer);
134}
135
136static void _GBASDLDeinit(struct SDLSoftwareRenderer* renderer) {
137 free(renderer->d.outputBuffer);
138
139 GBASDLDeinitEvents(&renderer->events);
140 GBASDLDeinitAudio(&renderer->audio);
141#if SDL_VERSION_ATLEAST(2, 0, 0)
142 SDL_DestroyWindow(renderer->window);
143#endif
144
145 GBASDLDeinit(renderer);
146
147 SDL_Quit();
148
149}