src/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 sigset_t signals;
60 sigaddset(&signals, SIGINT);
61 sigaddset(&signals, SIGTRAP);
62 pthread_sigmask(SIG_BLOCK, &signals, 0);
63
64 struct GBAThread context;
65 struct GLSoftwareRenderer renderer;
66 GBAVideoSoftwareRendererCreate(&renderer.d);
67
68 if (!_GBASDLInit(&renderer)) {
69 return 1;
70 }
71
72 context.fd = fd;
73 context.fname = fname;
74 context.useDebugger = 1;
75 context.renderer = &renderer.d.d;
76 context.frameskip = 0;
77 context.sync.videoFrameWait = 0;
78 context.sync.audioWait = 1;
79 context.startCallback = _GBASDLStart;
80 context.cleanCallback = _GBASDLClean;
81 context.userData = &renderer;
82 GBAThreadStart(&context);
83
84 _GBASDLRunloop(&context, &renderer);
85
86 GBAThreadJoin(&context);
87 close(fd);
88
89 _GBASDLDeinit(&renderer);
90
91 return 0;
92}
93
94static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
95 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
96 return 0;
97 }
98
99 GBASDLInitEvents(&renderer->events);
100 GBASDLInitAudio(&renderer->audio);
101
102 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
103 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
104 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
105 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
106 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
107#ifdef COLOR_16_BIT
108 SDL_SetVideoMode(240, 160, 16, SDL_OPENGL);
109#else
110 SDL_SetVideoMode(240, 160, 32, SDL_OPENGL);
111#endif
112
113 renderer->d.outputBuffer = malloc(256 * 256 * 4);
114 renderer->d.outputBufferStride = 256;
115 glGenTextures(1, &renderer->tex);
116 glBindTexture(GL_TEXTURE_2D, renderer->tex);
117 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
118 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
119 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
121 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
122
123 glViewport(0, 0, 240, 160);
124
125 return 1;
126}
127
128static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
129 SDL_Event event;
130
131 glEnable(GL_TEXTURE_2D);
132 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
133 glEnableClientState(GL_VERTEX_ARRAY);
134 glVertexPointer(2, GL_INT, 0, _glVertices);
135 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
136 glMatrixMode (GL_PROJECTION);
137 glLoadIdentity();
138 glOrtho(0, 240, 160, 0, 0, 1);
139 while (context->state < THREAD_EXITING) {
140 if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
141 glBindTexture(GL_TEXTURE_2D, renderer->tex);
142#ifdef COLOR_16_BIT
143 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
144#else
145 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
146#endif
147 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
148
149 SDL_GL_SwapBuffers();
150 }
151 GBASyncWaitFrameEnd(&context->sync);
152
153 while (SDL_PollEvent(&event)) {
154 GBASDLHandleEvent(context, &event);
155 }
156 }
157}
158
159static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
160 free(renderer->d.outputBuffer);
161
162 GBASDLDeinitEvents(&renderer->events);
163 GBASDLDeinitAudio(&renderer->audio);
164 SDL_Quit();
165}
166
167static void _GBASDLStart(struct GBAThread* threadContext) {
168 struct GLSoftwareRenderer* renderer = threadContext->userData;
169 renderer->audio.audio = &threadContext->gba->audio;
170}
171
172static void _GBASDLClean(struct GBAThread* threadContext) {
173 struct GLSoftwareRenderer* renderer = threadContext->userData;
174 renderer->audio.audio = 0;
175}