all repos — mgba @ 103bef17c93ee88a302b6eb857877eef5fe2e86d

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