all repos — mgba @ 5b300bbcff5d2dfb43671c203647ce78cd605465

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, 1)) {
 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	struct GBAThread context = {
 81		.renderer = &renderer.d.d,
 82		.startCallback = _GBASDLStart,
 83		.cleanCallback = _GBASDLClean,
 84		.sync.videoFrameWait = 0,
 85		.sync.audioWait = 1,
 86		.userData = &renderer
 87	};
 88
 89	context.debugger = createDebugger(&opts);
 90
 91	GBAMapOptionsToContext(&opts, &context);
 92
 93	GBAThreadStart(&context);
 94
 95	_GBASDLRunloop(&context, &renderer);
 96
 97	GBAThreadJoin(&context);
 98	close(opts.fd);
 99	if (opts.biosFd >= 0) {
100		close(opts.biosFd);
101	}
102	free(context.debugger);
103
104	_GBASDLDeinit(&renderer);
105
106	return 0;
107}
108
109static int _GBASDLInit(struct GLSoftwareRenderer* renderer) {
110	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
111		return 0;
112	}
113
114	GBASDLInitEvents(&renderer->events);
115	GBASDLInitAudio(&renderer->audio);
116
117#if SDL_VERSION_ATLEAST(2, 0, 0)
118	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
119#else
120	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
121#endif
122
123#ifndef COLOR_16_BIT
124	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
125	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
126	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
127#else
128	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
129#ifdef COLOR_5_6_5
130	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
131#else
132	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
133#endif
134	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
135#endif
136
137#if SDL_VERSION_ATLEAST(2, 0, 0)
138	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));
139	SDL_GL_CreateContext(renderer->window);
140	SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
141	renderer->events.window = renderer->window;
142	renderer->events.fullscreen = 0;
143#else
144#ifdef COLOR_16_BIT
145	SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 16, SDL_OPENGL);
146#else
147	SDL_SetVideoMode(renderer->viewportWidth, renderer->viewportHeight, 32, SDL_OPENGL);
148#endif
149#endif
150
151	renderer->d.outputBuffer = malloc(256 * 256 * 4);
152	renderer->d.outputBufferStride = 256;
153	glGenTextures(1, &renderer->tex);
154	glBindTexture(GL_TEXTURE_2D, renderer->tex);
155	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
156	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
157	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
158#ifndef _WIN32
159	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
160	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
161#endif
162
163	glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
164
165	return 1;
166}
167
168static void _GBASDLRunloop(struct GBAThread* context, struct GLSoftwareRenderer* renderer) {
169	SDL_Event event;
170
171	glEnable(GL_TEXTURE_2D);
172	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
173	glEnableClientState(GL_VERTEX_ARRAY);
174	glVertexPointer(2, GL_INT, 0, _glVertices);
175	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
176	glMatrixMode (GL_PROJECTION);
177	glLoadIdentity();
178	glOrtho(0, 240, 160, 0, 0, 1);
179	while (context->state < THREAD_EXITING) {
180		if (GBASyncWaitFrameStart(&context->sync, context->frameskip)) {
181			glBindTexture(GL_TEXTURE_2D, renderer->tex);
182#ifdef COLOR_16_BIT
183			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, renderer->d.outputBuffer);
184#else
185			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, renderer->d.outputBuffer);
186#endif
187			glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
188			if (context->sync.videoFrameWait) {
189				glFlush();
190			}
191		}
192		GBASyncWaitFrameEnd(&context->sync);
193#if SDL_VERSION_ATLEAST(2, 0, 0)
194		SDL_GL_SwapWindow(renderer->window);
195#else
196		SDL_GL_SwapBuffers();
197#endif
198
199		while (SDL_PollEvent(&event)) {
200#if SDL_VERSION_ATLEAST(2, 0, 0)
201			int fullscreen = renderer->events.fullscreen;
202#endif
203			GBASDLHandleEvent(context, &renderer->events, &event);
204#if SDL_VERSION_ATLEAST(2, 0, 0)
205			// Event handling can change the size of the screen
206			if (renderer->events.fullscreen != fullscreen) {
207				SDL_GetWindowSize(renderer->window, &renderer->viewportWidth, &renderer->viewportHeight);
208				glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
209			}
210#endif
211		}
212	}
213}
214
215static void _GBASDLDeinit(struct GLSoftwareRenderer* renderer) {
216	free(renderer->d.outputBuffer);
217
218	GBASDLDeinitEvents(&renderer->events);
219	GBASDLDeinitAudio(&renderer->audio);
220#if SDL_VERSION_ATLEAST(2, 0, 0)
221	SDL_DestroyWindow(renderer->window);
222#endif
223	SDL_Quit();
224}
225
226static void _GBASDLStart(struct GBAThread* threadContext) {
227	struct GLSoftwareRenderer* renderer = threadContext->userData;
228	renderer->audio.audio = &threadContext->gba->audio;
229}
230
231static void _GBASDLClean(struct GBAThread* threadContext) {
232	struct GLSoftwareRenderer* renderer = threadContext->userData;
233	renderer->audio.audio = 0;
234}