all repos — mgba @ 2f9bcf63b73e3edfd80a572981c3885bb30a09b9

mGBA Game Boy Advance Emulator

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