all repos — mgba @ b691c93416d44b89e16df3f700c00198deb7cf1f

mGBA Game Boy Advance Emulator

src/platform/sdl/gl-main.c (view raw)

  1#include "cli-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	int viewportWidth;
 27	int viewportHeight;
 28	GLuint tex;
 29};
 30
 31static int _GBASDLInit(struct GLSoftwareRenderer* renderer);
 32static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer);
 33static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer);
 34static void _GBASDLStart(struct GBAThread* context);
 35static void _GBASDLClean(struct GBAThread* context);
 36
 37static const GLint _glVertices[] = {
 38	0, 0,
 39	256, 0,
 40	256, 256,
 41	0, 256
 42};
 43
 44static const GLint _glTexCoords[] = {
 45	0, 0,
 46	1, 0,
 47	1, 1,
 48	0, 1
 49};
 50
 51int main(int argc, char** argv) {
 52	const char* fname = "test.rom";
 53	if (argc > 1) {
 54		fname = argv[1];
 55	}
 56	int fd = open(fname, O_RDONLY);
 57	if (fd < 0) {
 58		return 1;
 59	}
 60
 61	struct GLSoftwareRenderer renderer;
 62	GBAVideoSoftwareRendererCreate(&renderer.d);
 63
 64	renderer.viewportWidth = 240;
 65	renderer.viewportHeight = 160;
 66
 67	if (!_GBASDLInit(&renderer)) {
 68		return 1;
 69	}
 70
 71	struct CLIDebugger debugger;
 72	CLIDebuggerCreate(&debugger);
 73	struct GBAThread context = {
 74		.fd = fd,
 75		.biosFd = -1,
 76		.fname = fname,
 77		.debugger = &debugger.d,
 78		.renderer = &renderer.d.d,
 79		.frameskip = 0,
 80		.sync.videoFrameWait = 0,
 81		.sync.audioWait = 1,
 82		.startCallback = _GBASDLStart,
 83		.cleanCallback = _GBASDLClean,
 84		.userData = &renderer,
 85		.rewindBufferCapacity = 10,
 86		.rewindBufferInterval = 30
 87	};
 88	GBAThreadStart(&context);
 89
 90	_GBASDLRunloop(&context, &renderer);
 91
 92	GBAThreadJoin(&context);
 93	close(fd);
 94
 95	_GBASDLDeinit(&renderer);
 96
 97	return 0;
 98}
 99
100static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
101	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
102		return 0;
103	}
104
105	GBASDLInitEvents(&renderer->events);
106	GBASDLInitAudio(&renderer->audio);
107
108	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
109	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
110	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
111	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
112	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
113#ifdef COLOR_16_BIT
114	SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL);
115#else
116	SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL);
117#endif
118
119	renderer->d.outputBuffer = malloc(256 * 256 * 4);
120	renderer->d.outputBufferStride = 256;
121	glGenTextures(1, &renderer->tex);
122	glBindTexture(GL_TEXTURE_2D, renderer->tex);
123	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
124	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
125	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
126#ifndef _WIN32
127	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
128	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
129#endif
130
131	glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
132
133	return 1;
134}
135
136static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
137	SDL_Event event;
138
139	glEnable(GL_TEXTURE_2D);
140	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
141	glEnableClientState(GL_VERTEX_ARRAY);
142	glVertexPointer(2, GL_INT, 0, _glVertices);
143	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
144	glMatrixMode (GL_PROJECTION);
145	glLoadIdentity();
146	glOrtho(0, 240, 160, 0, 0, 1);
147	while (context->state < THREAD_EXITING) {
148		if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
149			glBindTexture(GL_TEXTURE_2D, renderer->tex);
150#ifdef COLOR_16_BIT
151			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
152#else
153			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
154#endif
155			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
156			if (context->sync.videoFrameWait) {
157				glFlush();
158			}
159		}
160		GBASyncWaitFrameEnd(&context->sync);
161		SDL_GL_SwapBuffers();
162
163		while (SDL_PollEvent(&event)) {
164			GBASDLHandleEvent(context, &event);
165		}
166	}
167}
168
169static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
170	free(renderer->d.outputBuffer);
171
172	GBASDLDeinitEvents(&renderer->events);
173	GBASDLDeinitAudio(&renderer->audio);
174	SDL_Quit();
175}
176
177static void _GBASDLStart(struct GBAThread* threadContext) {
178	struct GLSoftwareRenderer* renderer = threadContext->userData;
179	renderer->audio.audio = &threadContext->gba->audio;
180}
181
182static void _GBASDLClean(struct GBAThread* threadContext) {
183	struct GLSoftwareRenderer* renderer = threadContext->userData;
184	renderer->audio.audio = 0;
185}