all repos — mgba @ c12eca835f8f8c2550fd17ef3870ebc511fe6e23

mGBA Game Boy Advance Emulator

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