all repos — mgba @ ad38ae63ec3615da4a9e485affe38b7371bf47ea

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