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