src/platform/sdl/gl-main.c (view raw)
1#include "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
26 GLuint tex;
27};
28
29static int _GBASDLInit(struct GLSoftwareRenderer* renderer);
30static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer);
31static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer);
32static void _GBASDLStart(struct GBAThread* context);
33static void _GBASDLClean(struct GBAThread* context);
34
35static const GLint _glVertices[] = {
36 0, 0,
37 256, 0,
38 256, 256,
39 0, 256
40};
41
42static const GLint _glTexCoords[] = {
43 0, 0,
44 1, 0,
45 1, 1,
46 0, 1
47};
48
49int main(int argc, char** argv) {
50 const char* fname = "test.rom";
51 if (argc > 1) {
52 fname = argv[1];
53 }
54 int fd = open(fname, O_RDONLY);
55 if (fd < 0) {
56 return 1;
57 }
58
59 struct GBAThread context;
60 struct GLSoftwareRenderer renderer;
61 GBAVideoSoftwareRendererCreate(&renderer.d);
62
63 if (!_GBASDLInit(&renderer)) {
64 return 1;
65 }
66
67 context.fd = fd;
68 context.fname = fname;
69 context.useDebugger = 1;
70 context.renderer = &renderer.d.d;
71 context.frameskip = 0;
72 context.sync.videoFrameWait = 0;
73 context.sync.audioWait = 1;
74 context.startCallback = _GBASDLStart;
75 context.cleanCallback = _GBASDLClean;
76 context.userData = &renderer;
77 GBAThreadStart(&context);
78
79 _GBASDLRunloop(&context, &renderer);
80
81 GBAThreadJoin(&context);
82 close(fd);
83
84 _GBASDLDeinit(&renderer);
85
86 return 0;
87}
88
89static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
90 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
91 return 0;
92 }
93
94 GBASDLInitEvents(&renderer->events);
95 GBASDLInitAudio(&renderer->audio);
96
97 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
98 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
99 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
100 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
101 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
102#ifdef COLOR_16_BIT
103 SDL_SetVideoMode(240, 160, 16, SDL_OPENGL);
104#else
105 SDL_SetVideoMode(240, 160, 32, SDL_OPENGL);
106#endif
107
108 renderer->d.outputBuffer = malloc(256 * 256 * 4);
109 renderer->d.outputBufferStride = 256;
110 glGenTextures(1, &renderer->tex);
111 glBindTexture(GL_TEXTURE_2D, renderer->tex);
112 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
113 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
114 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
115#ifndef _WIN32
116 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
117 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
118#endif
119
120 glViewport(0, 0, 240, 160);
121
122 return 1;
123}
124
125static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
126 SDL_Event event;
127
128 glEnable(GL_TEXTURE_2D);
129 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
130 glEnableClientState(GL_VERTEX_ARRAY);
131 glVertexPointer(2, GL_INT, 0, _glVertices);
132 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
133 glMatrixMode (GL_PROJECTION);
134 glLoadIdentity();
135 glOrtho(0, 240, 160, 0, 0, 1);
136 while (context->state < THREAD_EXITING) {
137 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
138 glBindTexture(GL_TEXTURE_2D, renderer->tex);
139#ifdef COLOR_16_BIT
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
141#else
142 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
143#endif
144 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
145 glFlush();
146 }
147 GBASyncWaitFrameEnd(&context->sync);
148 SDL_GL_SwapBuffers();
149
150 while (SDL_PollEvent(&event)) {
151 GBASDLHandleEvent(context, &event);
152 }
153 }
154}
155
156static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
157 free(renderer->d.outputBuffer);
158
159 GBASDLDeinitEvents(&renderer->events);
160 GBASDLDeinitAudio(&renderer->audio);
161 SDL_Quit();
162}
163
164static void _GBASDLStart(struct GBAThread* threadContext) {
165 struct GLSoftwareRenderer* renderer = threadContext->userData;
166 renderer->audio.audio = &threadContext->gba->audio;
167}
168
169static void _GBASDLClean(struct GBAThread* threadContext) {
170 struct GLSoftwareRenderer* renderer = threadContext->userData;
171 renderer->audio.audio = 0;
172}