src/platform/sdl/gl-main.c (view raw)
1#include "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 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
117 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
118 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
119 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
120#if SDL_VERSION_ATLEAST(2, 0, 0)
121 renderer->window = SDL_CreateWindow("GBAc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL);
122 SDL_GL_CreateContext(renderer->window);
123 SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
124#else
125#ifdef COLOR_16_BIT
126 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL);
127#else
128 SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL);
129#endif
130#endif
131
132 renderer->d.outputBuffer = malloc(256 * 256 * 4);
133 renderer->d.outputBufferStride = 256;
134 glGenTextures(1, &renderer->tex);
135 glBindTexture(GL_TEXTURE_2D, renderer->tex);
136 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
137 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
138 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
139#ifndef _WIN32
140 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
141 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
142#endif
143
144 glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
145
146 return 1;
147}
148
149static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
150 SDL_Event event;
151
152 glEnable(GL_TEXTURE_2D);
153 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
154 glEnableClientState(GL_VERTEX_ARRAY);
155 glVertexPointer(2, GL_INT, 0, _glVertices);
156 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
157 glMatrixMode (GL_PROJECTION);
158 glLoadIdentity();
159 glOrtho(0, 240, 160, 0, 0, 1);
160 while (context->state < THREAD_EXITING) {
161 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
162 glBindTexture(GL_TEXTURE_2D, renderer->tex);
163#ifdef COLOR_16_BIT
164 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
165#else
166 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
167#endif
168 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
169 if (context->sync.videoFrameWait) {
170 glFlush();
171 }
172 }
173 GBASyncWaitFrameEnd(&context->sync);
174#if SDL_VERSION_ATLEAST(2, 0, 0)
175 SDL_GL_SwapWindow(renderer->window);
176#else
177 SDL_GL_SwapBuffers();
178#endif
179
180 while (SDL_PollEvent(&event)) {
181 GBASDLHandleEvent(context, &event);
182 }
183 }
184}
185
186static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
187 free(renderer->d.outputBuffer);
188
189 GBASDLDeinitEvents(&renderer->events);
190 GBASDLDeinitAudio(&renderer->audio);
191#if SDL_VERSION_ATLEAST(2, 0, 0)
192 SDL_DestroyWindow(renderer->window);
193#endif
194 SDL_Quit();
195}
196
197static void _GBASDLStart(struct GBAThread* threadContext) {
198 struct GLSoftwareRenderer* renderer = threadContext->userData;
199 renderer->audio.audio = &threadContext->gba->audio;
200}
201
202static void _GBASDLClean(struct GBAThread* threadContext) {
203 struct GLSoftwareRenderer* renderer = threadContext->userData;
204 renderer->audio.audio = 0;
205}