src/sdl/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.renderer = &renderer.d.d;
69 GBAThreadStart(&context);
70
71 _GBASDLRunloop(&context, &renderer);
72
73 GBAThreadJoin(&context);
74 close(fd);
75
76 _GBASDLDeinit(&renderer);
77
78 return 0;
79}
80
81static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
82 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
83 return 0;
84 }
85
86 GBASDLInitEvents();
87
88 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
89 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
90 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
91 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
92 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
93 SDL_SetVideoMode(240, 160, 32, SDL_OPENGL);
94
95 renderer->d.outputBuffer = malloc(256 * 256 * 4);
96 renderer->d.outputBufferStride = 256;
97 glGenTextures(1, &renderer->tex);
98 glBindTexture(GL_TEXTURE_2D, renderer->tex);
99 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
100 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
101 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
102 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
103 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
104
105 glViewport(0, 0, 240, 160);
106
107 return 1;
108}
109
110static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
111 SDL_Event event;
112
113 int err;
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) {
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 pthread_mutex_unlock(&renderer->d.mutex);
139 } else {
140 while (!renderer->d.d.framesPending) {
141 struct timeval tv;
142 struct timespec ts;
143 gettimeofday(&tv, 0);
144 ts.tv_sec = tv.tv_sec;
145 ts.tv_nsec = tv.tv_usec * 1000 + 800000;
146 err = pthread_cond_timedwait(&renderer->d.upCond, &renderer->d.mutex, &ts);
147 if (err == ETIMEDOUT) {
148 break;
149 }
150 }
151 pthread_mutex_unlock(&renderer->d.mutex);
152 }
153 }
154}
155
156static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
157 free(renderer->d.outputBuffer);
158
159 GBASDLDeinitEvents();
160 SDL_Quit();
161}