all repos — mgba @ 6afa678a41545ab0ab4255e10fe25cb56dbbcd37

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