src/gl-main.c (view raw)
1#include "debugger.h"
2#include "gba-thread.h"
3#include "gba.h"
4#include "sdl-events.h"
5#include "renderers/video-software.h"
6
7#include <SDL.h>
8#ifdef __APPLE__
9#include <OpenGL/gl.h>
10#else
11#include <GL/gl.h>
12#endif
13
14#include <fcntl.h>
15#include <errno.h>
16#include <signal.h>
17#include <sys/time.h>
18#include <unistd.h>
19
20struct GLSoftwareRenderer {
21 struct GBAVideoSoftwareRenderer d;
22
23 GLuint tex;
24};
25
26static int _GBASDLInit(struct GLSoftwareRenderer* renderer);
27static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer);
28static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer);
29
30static const GLint _glVertices[] = {
31 0, 0,
32 256, 0,
33 256, 256,
34 0, 256
35};
36
37static const GLint _glTexCoords[] = {
38 0, 0,
39 1, 0,
40 1, 1,
41 0, 1
42};
43
44int main(int argc, char** argv) {
45 const char* fname = "test.rom";
46 if (argc > 1) {
47 fname = argv[1];
48 }
49 int fd = open(fname, O_RDONLY);
50 if (fd < 0) {
51 return 1;
52 }
53
54 sigset_t signals;
55 sigaddset(&signals, SIGINT);
56 sigaddset(&signals, SIGTRAP);
57 pthread_sigmask(SIG_BLOCK, &signals, 0);
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.useDebugger = 0;
69 context.renderer = &renderer.d.d;
70 GBAThreadStart(&context);
71
72 _GBASDLRunloop(&context, &renderer);
73
74 GBAThreadJoin(&context);
75 close(fd);
76
77 _GBASDLDeinit(&renderer);
78
79 return 0;
80}
81
82static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
83 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
84 return 0;
85 }
86
87 GBASDLInitEvents();
88
89 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
90 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
91 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
92 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
93 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
94 SDL_SetVideoMode(240, 160, 32, SDL_OPENGL);
95
96 renderer->d.outputBuffer = malloc(256 * 256 * 4);
97 renderer->d.outputBufferStride = 256;
98 glGenTextures(1, &renderer->tex);
99 glBindTexture(GL_TEXTURE_2D, renderer->tex);
100 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
101 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
102 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
103 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
104 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
105
106 glViewport(0, 0, 240, 160);
107
108 return 1;
109}
110
111static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
112 SDL_Event event;
113
114 glEnable(GL_TEXTURE_2D);
115 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
116 glEnableClientState(GL_VERTEX_ARRAY);
117 glVertexPointer(2, GL_INT, 0, _glVertices);
118 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
119 glMatrixMode (GL_PROJECTION);
120 glLoadIdentity();
121 glOrtho(0, 240, 160, 0, 0, 1);
122 while (context->started && (!context->debugger || context->debugger->state != DEBUGGER_EXITING)) {
123 pthread_mutex_lock(&renderer->d.mutex);
124 if (renderer->d.d.framesPending) {
125 renderer->d.d.framesPending = 0;
126 pthread_mutex_unlock(&renderer->d.mutex);
127 glBindTexture(GL_TEXTURE_2D, renderer->tex);
128 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
129 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
130
131 SDL_GL_SwapBuffers();
132
133 while (SDL_PollEvent(&event)) {
134 GBASDLHandleEvent(context, &event);
135 }
136 pthread_mutex_lock(&renderer->d.mutex);
137 pthread_cond_broadcast(&renderer->d.downCond);
138 } else {
139 pthread_cond_broadcast(&renderer->d.downCond);
140 pthread_cond_wait(&renderer->d.upCond, &renderer->d.mutex);
141 }
142 pthread_mutex_unlock(&renderer->d.mutex);
143 }
144}
145
146static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
147 free(renderer->d.outputBuffer);
148
149 GBASDLDeinitEvents();
150 SDL_Quit();
151}