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