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 return 1;
71 }
72
73 renderer.viewportWidth = graphicsOpts.width;
74 renderer.viewportHeight = graphicsOpts.height;
75#if SDL_VERSION_ATLEAST(2, 0, 0)
76 renderer.events.fullscreen = graphicsOpts.fullscreen;
77 renderer.events.windowUpdated = 0;
78#endif
79
80 if (!_GBASDLInit(&renderer)) {
81 return 1;
82 }
83
84 struct GBAThread context = {
85 .renderer = &renderer.d.d,
86 .startCallback = _GBASDLStart,
87 .cleanCallback = _GBASDLClean,
88 .sync.videoFrameWait = 0,
89 .sync.audioWait = 1,
90 .userData = &renderer
91 };
92
93 context.debugger = createDebugger(&opts);
94
95 GBAMapOptionsToContext(&opts, &context);
96
97 GBAThreadStart(&context);
98
99 _GBASDLRunloop(&context, &renderer);
100
101 GBAThreadJoin(&context);
102 close(opts.fd);
103 if (opts.biosFd >= 0) {
104 close(opts.biosFd);
105 }
106 free(context.debugger);
107
108 _GBASDLDeinit(&renderer);
109
110 return 0;
111}
112
113static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
114 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
115 return 0;
116 }
117
118 GBASDLInitEvents(&renderer->events);
119 GBASDLInitAudio(&renderer->audio);
120
121#if SDL_VERSION_ATLEAST(2, 0, 0)
122 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
123#else
124 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
125#endif
126
127#ifndef COLOR_16_BIT
128 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
129 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
130 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
131#else
132 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
133#ifdef COLOR_5_6_5
134 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
135#else
136 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
137#endif
138 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
139#endif
140
141#if SDL_VERSION_ATLEAST(2, 0, 0)
142 renderer->window = SDL_CreateWindow("GBAc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->events.fullscreen));
143 SDL_GL_CreateContext(renderer->window);
144 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
145 renderer->events.window = renderer->window;
146#else
147#ifdef COLOR_16_BIT
148 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL);
149#else
150 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL);
151#endif
152#endif
153
154 renderer->d.outputBuffer = malloc(256 * 256 * 4);
155 renderer->d.outputBufferStride = 256;
156 glGenTextures(1, &renderer->tex);
157 glBindTexture(GL_TEXTURE_2D, renderer->tex);
158 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
159 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
160 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
161#ifndef _WIN32
162 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
163 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
164#endif
165
166 glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
167
168 return 1;
169}
170
171static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
172 SDL_Event event;
173
174 glEnable(GL_TEXTURE_2D);
175 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
176 glEnableClientState(GL_VERTEX_ARRAY);
177 glVertexPointer(2, GL_INT, 0, _glVertices);
178 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
179 glMatrixMode (GL_PROJECTION);
180 glLoadIdentity();
181 glOrtho(0, 240, 160, 0, 0, 1);
182 while (context->state < THREAD_EXITING) {
183 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
184 glBindTexture(GL_TEXTURE_2D, renderer->tex);
185#ifdef COLOR_16_BIT
186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
187#else
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
189#endif
190 if (context->sync.videoFrameWait) {
191 glFlush();
192 }
193 }
194 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
195 GBASyncWaitFrameEnd(&context->sync);
196#if SDL_VERSION_ATLEAST(2, 0, 0)
197 SDL_GL_SwapWindow(renderer->window);
198#else
199 SDL_GL_SwapBuffers();
200#endif
201
202 while (SDL_PollEvent(&event)) {
203 GBASDLHandleEvent(context, &renderer->events, &event);
204#if SDL_VERSION_ATLEAST(2, 0, 0)
205 // Event handling can change the size of the screen
206 if (renderer->events.windowUpdated) {
207 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
208 glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
209 renderer->events.windowUpdated = 0;
210 }
211#endif
212 }
213 }
214}
215
216static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
217 free(renderer->d.outputBuffer);
218
219 GBASDLDeinitEvents(&renderer->events);
220 GBASDLDeinitAudio(&renderer->audio);
221#if SDL_VERSION_ATLEAST(2, 0, 0)
222 SDL_DestroyWindow(renderer->window);
223#endif
224 SDL_Quit();
225}
226
227static void _GBASDLStart(struct GBAThread* threadContext) {
228 struct GLSoftwareRenderer* renderer = threadContext->userData;
229 renderer->audio.audio = &threadContext->gba->audio;
230}
231
232static void _GBASDLClean(struct GBAThread* threadContext) {
233 struct GLSoftwareRenderer* renderer = threadContext->userData;
234 renderer->audio.audio = 0;
235}