all repos — mgba @ 705b6e9b139b7c0c6fe1c10ffe21a0a44241f9a9

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