src/platform/sdl/gl-main.c (view raw)
1#ifdef USE_CLI_DEBUGGER
2#include "debugger/cli-debugger.h"
3#endif
4
5#ifdef USE_GDB_STUB
6#include "debugger/gdb-stub.h"
7#endif
8
9#include "gba-thread.h"
10#include "gba.h"
11#include "gba-config.h"
12#include "sdl-audio.h"
13#include "sdl-events.h"
14#include "renderers/video-software.h"
15#include "platform/commandline.h"
16#include "util/configuration.h"
17
18#include <SDL.h>
19#ifdef __APPLE__
20#include <OpenGL/gl.h>
21#else
22#include <GL/gl.h>
23#endif
24
25#include <errno.h>
26#include <signal.h>
27#include <sys/time.h>
28
29#define PORT "sdl-gl"
30
31struct GLSoftwareRenderer {
32 struct GBAVideoSoftwareRenderer d;
33 struct GBASDLAudio audio;
34 struct GBASDLEvents events;
35#if SDL_VERSION_ATLEAST(2, 0, 0)
36 SDL_Window* window;
37#endif
38
39 int viewportWidth;
40 int viewportHeight;
41 GLuint tex;
42};
43
44static int _GBASDLInit(struct GLSoftwareRenderer* renderer);
45static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer);
46static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer);
47static void _GBASDLStart(struct GBAThread* context);
48static void _GBASDLClean(struct GBAThread* context);
49
50static const GLint _glVertices[] = {
51 0, 0,
52 256, 0,
53 256, 256,
54 0, 256
55};
56
57static const GLint _glTexCoords[] = {
58 0, 0,
59 1, 0,
60 1, 1,
61 0, 1
62};
63
64int main(int argc, char** argv) {
65 struct GLSoftwareRenderer renderer;
66 GBAVideoSoftwareRendererCreate(&renderer.d);
67
68 struct Configuration config;
69 ConfigurationInit(&config);
70 GBAConfigLoad(&config);
71
72 struct StartupOptions opts = { };
73 struct GraphicsOpts graphicsOpts = { };
74
75 struct SubParser subparser;
76
77 GBAConfigMapStartupOpts(&config, PORT, &opts);
78 GBAConfigMapGraphicsOpts(&config, PORT, &graphicsOpts);
79
80 initParserForGraphics(&subparser, &graphicsOpts);
81 if (!parseCommandArgs(&opts, argc, argv, &subparser)) {
82 usage(argv[0], subparser.usage);
83 freeOptions(&opts);
84 return 1;
85 }
86
87 renderer.viewportWidth = graphicsOpts.width;
88 renderer.viewportHeight = graphicsOpts.height;
89#if SDL_VERSION_ATLEAST(2, 0, 0)
90 renderer.events.fullscreen = graphicsOpts.fullscreen;
91 renderer.events.windowUpdated = 0;
92#endif
93
94 if (!_GBASDLInit(&renderer)) {
95 freeOptions(&opts);
96 return 1;
97 }
98
99 struct GBAThread context = {
100 .renderer = &renderer.d.d,
101 .audioBuffers = 512,
102 .startCallback = _GBASDLStart,
103 .cleanCallback = _GBASDLClean,
104 .sync.videoFrameWait = false,
105 .sync.audioWait = true,
106 .userData = &renderer
107 };
108
109 context.debugger = createDebugger(&opts);
110
111 GBAMapOptionsToContext(&opts, &context);
112
113 renderer.audio.samples = context.audioBuffers;
114 GBASDLInitAudio(&renderer.audio);
115
116 renderer.events.bindings = &context.inputMap;
117 GBASDLInitEvents(&renderer.events);
118
119 GBAThreadStart(&context);
120
121 _GBASDLRunloop(&context, &renderer);
122
123 GBAThreadJoin(&context);
124 freeOptions(&opts);
125 free(context.debugger);
126
127 _GBASDLDeinit(&renderer);
128
129 return 0;
130}
131
132static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
133 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
134 return 0;
135 }
136
137#ifndef COLOR_16_BIT
138 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
139 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
140 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
141#else
142 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
143#ifdef COLOR_5_6_5
144 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
145#else
146 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
147#endif
148 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
149#endif
150
151#if SDL_VERSION_ATLEAST(2, 0, 0)
152 renderer->window = SDL_CreateWindow(PROJECT_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->events.fullscreen));
153 SDL_GL_CreateContext(renderer->window);
154 SDL_GL_SetSwapInterval(1);
155 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
156 renderer->events.window = renderer->window;
157#else
158 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
159#ifdef COLOR_16_BIT
160 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL);
161#else
162 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL);
163#endif
164#endif
165
166 renderer->d.outputBuffer = malloc(256 * 256 * 4);
167 renderer->d.outputBufferStride = 256;
168 glGenTextures(1, &renderer->tex);
169 glBindTexture(GL_TEXTURE_2D, renderer->tex);
170 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
171 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
172 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
173#ifndef _WIN32
174 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
175 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
176#endif
177
178 glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
179
180 return 1;
181}
182
183static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
184 SDL_Event event;
185
186 glEnable(GL_TEXTURE_2D);
187 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
188 glEnableClientState(GL_VERTEX_ARRAY);
189 glVertexPointer(2, GL_INT, 0, _glVertices);
190 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
191 glMatrixMode (GL_PROJECTION);
192 glLoadIdentity();
193 glOrtho(0, 240, 160, 0, 0, 1);
194 while (context->state < THREAD_EXITING) {
195 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
196 glBindTexture(GL_TEXTURE_2D, renderer->tex);
197#ifdef COLOR_16_BIT
198#ifdef COLOR_5_6_5
199 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, renderer->d.outputBuffer);
200#else
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
202#endif
203#else
204 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
205#endif
206 if (context->sync.videoFrameWait) {
207 glFlush();
208 }
209 }
210 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
211 GBASyncWaitFrameEnd(&context->sync);
212#if SDL_VERSION_ATLEAST(2, 0, 0)
213 SDL_GL_SwapWindow(renderer->window);
214#else
215 SDL_GL_SwapBuffers();
216#endif
217
218 while (SDL_PollEvent(&event)) {
219 GBASDLHandleEvent(context, &renderer->events, &event);
220#if SDL_VERSION_ATLEAST(2, 0, 0)
221 // Event handling can change the size of the screen
222 if (renderer->events.windowUpdated) {
223 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
224 glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
225 renderer->events.windowUpdated = 0;
226 }
227#endif
228 }
229 }
230}
231
232static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
233 free(renderer->d.outputBuffer);
234
235 GBASDLDeinitEvents(&renderer->events);
236 GBASDLDeinitAudio(&renderer->audio);
237#if SDL_VERSION_ATLEAST(2, 0, 0)
238 SDL_DestroyWindow(renderer->window);
239#endif
240 SDL_Quit();
241}
242
243static void _GBASDLStart(struct GBAThread* threadContext) {
244 struct GLSoftwareRenderer* renderer = threadContext->userData;
245 renderer->audio.audio = &threadContext->gba->audio;
246 renderer->audio.thread = threadContext;
247}
248
249static void _GBASDLClean(struct GBAThread* threadContext) {
250 struct GLSoftwareRenderer* renderer = threadContext->userData;
251 renderer->audio.audio = 0;
252}