all repos — mgba @ ecb1939ff1ecbb190a28a9eb3544c2a58d210305

mGBA Game Boy Advance Emulator

src/platform/glsl-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
 19static int _GBASDLInit(void);
 20static void _GBASDLDeinit(void);
 21static void _GBASDLRunloop(struct GBAThread* context, struct GBAVideoGLSLRenderer* renderer);
 22
 23int main(int argc, char** argv) {
 24	const char* fname = "test.rom";
 25	if (argc > 1) {
 26		fname = argv[1];
 27	}
 28	int fd = open(fname, O_RDONLY);
 29	if (fd < 0) {
 30		return 1;
 31	}
 32
 33	struct GBAThread context;
 34	struct GBAVideoGLSLRenderer renderer;
 35
 36	if (!_GBASDLInit()) {
 37		return 1;
 38	}
 39	GBAVideoGLSLRendererCreate(&renderer);
 40
 41	context.fd = fd;
 42	context.renderer = &renderer.d;
 43	GBAThreadStart(&context);
 44
 45	_GBASDLRunloop(&context, &renderer);
 46
 47	GBAThreadJoin(&context);
 48	close(fd);
 49
 50	_GBASDLDeinit();
 51
 52	return 0;
 53}
 54
 55static int _GBASDLInit() {
 56	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
 57		return 0;
 58	}
 59
 60	GBASDLInitEvents();
 61
 62	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
 63	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
 64	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
 65	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
 66	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
 67	SDL_SetVideoMode(240, 160, 32, SDL_OPENGL);
 68
 69	glViewport(0, 0, 240, 160);
 70
 71	return 1;
 72}
 73
 74static void _GBASDLRunloop(struct GBAThread* context, struct GBAVideoGLSLRenderer* renderer) {
 75	SDL_Event event;
 76
 77	glEnable(GL_TEXTURE_2D);
 78	while (context->state < THREAD_EXITING) {
 79		GBAVideoGLSLRendererProcessEvents(renderer);
 80		pthread_mutex_lock(&renderer->mutex);
 81		if (renderer->d.framesPending) {
 82			renderer->d.framesPending = 0;
 83			pthread_mutex_unlock(&renderer->mutex);
 84
 85			SDL_GL_SwapBuffers();
 86
 87			while (SDL_PollEvent(&event)) {
 88				GBASDLHandleEvent(context, &event);
 89			}
 90			pthread_mutex_lock(&renderer->mutex);
 91			pthread_cond_broadcast(&renderer->downCond);
 92		} else {
 93			pthread_cond_broadcast(&renderer->downCond);
 94			pthread_cond_wait(&renderer->upCond, &renderer->mutex);
 95		}
 96		pthread_mutex_unlock(&renderer->mutex);
 97	}
 98}
 99
100static void _GBASDLDeinit() {
101	GBASDLDeinitEvents();
102	SDL_Quit();
103}