all repos — mgba @ 89ccb41b03c9e4e95d4a8f66fa0a6bc57666e8b5

mGBA Game Boy Advance Emulator

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

  1#ifdef USE_CLI_DEBUGGER
  2#include "debugger/cli-debugger.h"
  3#endif
  4
  5#ifdef USE_GDB_STUB
  6#include "debugger/gdb-stub.h"
  7#endif
  8
  9#include "gba-thread.h"
 10#include "gba.h"
 11#include "sdl-audio.h"
 12#include "sdl-events.h"
 13#include "renderers/video-software.h"
 14#include "platform/commandline.h"
 15
 16#include <SDL.h>
 17#ifdef __APPLE__
 18#include <OpenGL/gl.h>
 19#else
 20#include <GL/gl.h>
 21#endif
 22
 23#include <errno.h>
 24#include <signal.h>
 25#include <sys/time.h>
 26
 27struct GLSoftwareRenderer {
 28	struct GBAVideoSoftwareRenderer d;
 29	struct GBASDLAudio audio;
 30	struct GBASDLEvents events;
 31#if SDL_VERSION_ATLEAST(2, 0, 0)
 32	SDL_Window* window;
 33#endif
 34
 35	int viewportWidth;
 36	int viewportHeight;
 37	GLuint tex;
 38};
 39
 40static int _GBASDLInit(struct GLSoftwareRenderer* renderer);
 41static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer);
 42static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer);
 43static void _GBASDLStart(struct GBAThread* context);
 44static void _GBASDLClean(struct GBAThread* context);
 45
 46static const GLint _glVertices[] = {
 47	0, 0,
 48	256, 0,
 49	256, 256,
 50	0, 256
 51};
 52
 53static const GLint _glTexCoords[] = {
 54	0, 0,
 55	1, 0,
 56	1, 1,
 57	0, 1
 58};
 59
 60int main(int argc, char** argv) {
 61	struct GLSoftwareRenderer renderer;
 62	GBAVideoSoftwareRendererCreate(&renderer.d);
 63
 64	struct StartupOptions opts;
 65	if (!parseCommandArgs(&opts, argc, argv)) {
 66		usage(argv[0]);
 67		return 1;
 68	}
 69
 70	renderer.viewportWidth = opts.width;
 71	renderer.viewportHeight = opts.height;
 72#if SDL_VERSION_ATLEAST(2, 0, 0)
 73	renderer.events.fullscreen = opts.fullscreen;
 74#endif
 75
 76	if (!_GBASDLInit(&renderer)) {
 77		return 1;
 78	}
 79
 80	union {
 81		struct ARMDebugger d;
 82#ifdef USE_CLI_DEBUGGER
 83		struct CLIDebugger cli;
 84#endif
 85#ifdef USE_GDB_STUB
 86		struct GDBStub gdb;
 87#endif
 88	} debugger;
 89
 90	struct GBAThread context = {
 91		.debugger = &debugger.d,
 92		.renderer = &renderer.d.d,
 93		.startCallback = _GBASDLStart,
 94		.cleanCallback = _GBASDLClean,
 95		.sync.videoFrameWait = 0,
 96		.sync.audioWait = 1,
 97		.userData = &renderer
 98	};
 99
100	switch (opts.debuggerType) {
101#ifdef USE_CLI_DEBUGGER
102	case DEBUGGER_CLI:
103		CLIDebuggerCreate(&debugger.cli);
104		break;
105#endif
106#ifdef USE_GDB_STUB
107	case DEBUGGER_GDB:
108		GDBStubCreate(&debugger.gdb);
109		break;
110#endif
111	case DEBUGGER_NONE:
112	case DEBUGGER_MAX:
113		context.debugger = 0;
114		break;
115	}
116
117	GBAMapOptionsToContext(&opts, &context);
118
119	GBAThreadStart(&context);
120
121	_GBASDLRunloop(&context, &renderer);
122
123	GBAThreadJoin(&context);
124	close(opts.fd);
125	if (opts.biosFd >= 0) {
126		close(opts.biosFd);
127	}
128
129	_GBASDLDeinit(&renderer);
130
131	return 0;
132}
133
134static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
135	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
136		return 0;
137	}
138
139	GBASDLInitEvents(&renderer->events);
140	GBASDLInitAudio(&renderer->audio);
141
142#if SDL_VERSION_ATLEAST(2, 0, 0)
143	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
144#else
145	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
146#endif
147
148#ifndef COLOR_16_BIT
149	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
150	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
151	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
152#else
153	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
154#ifdef COLOR_5_6_5
155	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
156#else
157	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
158#endif
159	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
160#endif
161
162#if SDL_VERSION_ATLEAST(2, 0, 0)
163	renderer->window = SDL_CreateWindow("GBAc", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, renderer->viewportWidth, renderer->viewportHeight, SDL_WINDOW_OPENGL | (SDL_WINDOW_FULLSCREEN_DESKTOP * renderer->events.fullscreen));
164	SDL_GL_CreateContext(renderer->window);
165	SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
166	renderer->events.window = renderer->window;
167	renderer->events.fullscreen = 0;
168#else
169#ifdef COLOR_16_BIT
170	SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL);
171#else
172	SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL);
173#endif
174#endif
175
176	renderer->d.outputBuffer = malloc(256 * 256 * 4);
177	renderer->d.outputBufferStride = 256;
178	glGenTextures(1, &renderer->tex);
179	glBindTexture(GL_TEXTURE_2D, renderer->tex);
180	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
181	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
182	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
183#ifndef _WIN32
184	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
185	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
186#endif
187
188	glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
189
190	return 1;
191}
192
193static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
194	SDL_Event event;
195
196	glEnable(GL_TEXTURE_2D);
197	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
198	glEnableClientState(GL_VERTEX_ARRAY);
199	glVertexPointer(2, GL_INT, 0, _glVertices);
200	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
201	glMatrixMode (GL_PROJECTION);
202	glLoadIdentity();
203	glOrtho(0, 240, 160, 0, 0, 1);
204	while (context->state < THREAD_EXITING) {
205		if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
206			glBindTexture(GL_TEXTURE_2D, renderer->tex);
207#ifdef COLOR_16_BIT
208			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
209#else
210			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
211#endif
212			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
213			if (context->sync.videoFrameWait) {
214				glFlush();
215			}
216		}
217		GBASyncWaitFrameEnd(&context->sync);
218#if SDL_VERSION_ATLEAST(2, 0, 0)
219		SDL_GL_SwapWindow(renderer->window);
220#else
221		SDL_GL_SwapBuffers();
222#endif
223
224		while (SDL_PollEvent(&event)) {
225#if SDL_VERSION_ATLEAST(2, 0, 0)
226			int fullscreen = renderer->events.fullscreen;
227#endif
228			GBASDLHandleEvent(context, &renderer->events, &event);
229#if SDL_VERSION_ATLEAST(2, 0, 0)
230			// Event handling can change the size of the screen
231			if (renderer->events.fullscreen != fullscreen) {
232				SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
233				glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
234			}
235#endif
236		}
237	}
238}
239
240static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
241	free(renderer->d.outputBuffer);
242
243	GBASDLDeinitEvents(&renderer->events);
244	GBASDLDeinitAudio(&renderer->audio);
245#if SDL_VERSION_ATLEAST(2, 0, 0)
246	SDL_DestroyWindow(renderer->window);
247#endif
248	SDL_Quit();
249}
250
251static void _GBASDLStart(struct GBAThread* threadContext) {
252	struct GLSoftwareRenderer* renderer = threadContext->userData;
253	renderer->audio.audio = &threadContext->gba->audio;
254}
255
256static void _GBASDLClean(struct GBAThread* threadContext) {
257	struct GLSoftwareRenderer* renderer = threadContext->userData;
258	renderer->audio.audio = 0;
259}