all repos — mgba @ 49de0fb52e8da117efe0cd076d1bd097844b1f54

mGBA Game Boy Advance Emulator

src/gl-main.c (view raw)

  1#include "debugger.h"
  2#include "gba-thread.h"
  3#include "gba.h"
  4#include "renderers/video-glsl.h"
  5#include "sdl-events.h"
  6
  7#include <SDL.h>
  8#ifdef __APPLE__
  9#include <OpenGL/gl.h>
 10#else
 11#include <GL/gl.h>
 12#endif
 13
 14#include <fcntl.h>
 15#include <errno.h>
 16#include <signal.h>
 17#include <sys/time.h>
 18#include <unistd.h>
 19
 20static int _GBASDLInit(void);
 21static void _GBASDLDeinit(void);
 22static void _GBASDLRunloop(struct GBAThread* context, struct GBAVideoGLSLRenderer* renderer);
 23
 24int main(int argc, char** argv) {
 25	const char* fname = "test.rom";
 26	if (argc > 1) {
 27		fname = argv[1];
 28	}
 29	int fd = open(fname, O_RDONLY);
 30	if (fd < 0) {
 31		return 1;
 32	}
 33
 34	struct GBAThread context;
 35	struct GBAVideoGLSLRenderer renderer;
 36
 37	if (!_GBASDLInit()) {
 38		return 1;
 39	}
 40	GBAVideoGLSLRendererCreate(&renderer);
 41
 42	context.fd = fd;
 43	context.renderer = &renderer.d;
 44	GBAThreadStart(&context);
 45
 46	_GBASDLRunloop(&context, &renderer);
 47
 48	GBAThreadJoin(&context);
 49	close(fd);
 50
 51	_GBASDLDeinit();
 52
 53	return 0;
 54}
 55
 56static int _GBASDLInit() {
 57	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
 58		return 0;
 59	}
 60
 61	GBASDLInitEvents();
 62
 63	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
 64	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
 65	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
 66	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
 67	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
 68	SDL_SetVideoMode(240, 160, 32, SDL_OPENGL);
 69
 70	glViewport(0, 0, 240, 160);
 71
 72	return 1;
 73}
 74
 75static void _GBASDLRunloop(struct GBAThread* context, struct GBAVideoGLSLRenderer* renderer) {
 76	SDL_Event event;
 77
 78	glEnable(GL_TEXTURE_2D);
 79	while (context->started && context->debugger->state != DEBUGGER_EXITING) {
 80		GBAVideoGLSLRendererProcessEvents(renderer);
 81		pthread_mutex_lock(&renderer->mutex);
 82		if (renderer->d.framesPending) {
 83			renderer->d.framesPending = 0;
 84			pthread_mutex_unlock(&renderer->mutex);
 85
 86			SDL_GL_SwapBuffers();
 87
 88			while (SDL_PollEvent(&event)) {
 89				GBASDLHandleEvent(context, &event);
 90			}
 91			pthread_mutex_lock(&renderer->mutex);
 92			pthread_cond_broadcast(&renderer->downCond);
 93		} else {
 94			pthread_cond_broadcast(&renderer->downCond);
 95			pthread_cond_wait(&renderer->upCond, &renderer->mutex);
 96		}
 97		pthread_mutex_unlock(&renderer->mutex);
 98	}
 99}
100
101static void _GBASDLDeinit() {
102	GBASDLDeinitEvents();
103	SDL_Quit();
104}