all repos — mgba @ 60577e83948647d36a2e6a8b4ec8f8556df3f72f

mGBA Game Boy Advance Emulator

src/platform/opengl/gl.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "gl.h"
  7
  8#include <mgba-util/math.h>
  9
 10static const GLint _glVertices[] = {
 11	0, 0,
 12	256, 0,
 13	256, 256,
 14	0, 256
 15};
 16
 17static const GLint _glTexCoords[] = {
 18	0, 0,
 19	1, 0,
 20	1, 1,
 21	0, 1
 22};
 23
 24static void mGLContextInit(struct VideoBackend* v, WHandle handle) {
 25	UNUSED(handle);
 26	struct mGLContext* context = (struct mGLContext*) v;
 27	glGenTextures(1, &context->tex);
 28	glBindTexture(GL_TEXTURE_2D, context->tex);
 29	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 30#ifndef _WIN32
 31	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 32	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 33#endif
 34}
 35
 36static void mGLContextSetDimensions(struct VideoBackend* v, unsigned width, unsigned height) {
 37	struct mGLContext* context = (struct mGLContext*) v;
 38	v->width = width;
 39	v->height = height;
 40
 41	glBindTexture(GL_TEXTURE_2D, context->tex);
 42#ifdef COLOR_16_BIT
 43#ifdef COLOR_5_6_5
 44	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
 45#else
 46	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);
 47#endif
 48#elif defined(__BIG_ENDIAN__)
 49	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
 50#else
 51	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
 52#endif
 53}
 54
 55static void mGLContextDeinit(struct VideoBackend* v) {
 56	struct mGLContext* context = (struct mGLContext*) v;
 57	glDeleteTextures(1, &context->tex);
 58}
 59
 60static void mGLContextResized(struct VideoBackend* v, unsigned w, unsigned h) {
 61	unsigned drawW = w;
 62	unsigned drawH = h;
 63	if (v->lockAspectRatio) {
 64		if (w * v->height > h * v->width) {
 65			drawW = h * v->width / v->height;
 66		} else if (w * v->height < h * v->width) {
 67			drawH = w * v->height / v->width;
 68		}
 69	}
 70	if (v->lockIntegerScaling) {
 71		drawW -= drawW % v->width;
 72		drawH -= drawH % v->height;
 73	}
 74	glMatrixMode(GL_MODELVIEW);
 75	glLoadIdentity();
 76	glClearColor(0, 0, 0, 0);
 77	glClear(GL_COLOR_BUFFER_BIT);
 78	glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
 79}
 80
 81static void mGLContextClear(struct VideoBackend* v) {
 82	UNUSED(v);
 83	glClearColor(0, 0, 0, 0);
 84	glClear(GL_COLOR_BUFFER_BIT);
 85}
 86
 87void mGLContextDrawFrame(struct VideoBackend* v) {
 88	struct mGLContext* context = (struct mGLContext*) v;
 89	glEnable(GL_TEXTURE_2D);
 90	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 91	glEnableClientState(GL_VERTEX_ARRAY);
 92	glVertexPointer(2, GL_INT, 0, _glVertices);
 93	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
 94	glMatrixMode(GL_PROJECTION);
 95	glLoadIdentity();
 96	glOrtho(0, v->width, v->height, 0, 0, 1);
 97	glMatrixMode(GL_MODELVIEW);
 98	glLoadIdentity();
 99	glBindTexture(GL_TEXTURE_2D, context->tex);
100	if (v->filter) {
101		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
102		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
103	} else {
104		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
105		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
106	}
107	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
108}
109
110void mGLContextPostFrame(struct VideoBackend* v, const void* frame) {
111	struct mGLContext* context = (struct mGLContext*) v;
112	glBindTexture(GL_TEXTURE_2D, context->tex);
113#ifdef COLOR_16_BIT
114#ifdef COLOR_5_6_5
115	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
116#else
117	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
118#endif
119#elif defined(__BIG_ENDIAN__)
120	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, frame);
121#else
122	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height,  GL_RGBA, GL_UNSIGNED_BYTE, frame);
123#endif
124}
125
126void mGLContextCreate(struct mGLContext* context) {
127	context->d.init = mGLContextInit;
128	context->d.deinit = mGLContextDeinit;
129	context->d.setDimensions = mGLContextSetDimensions;
130	context->d.resized = mGLContextResized;
131	context->d.swap = 0;
132	context->d.clear = mGLContextClear;
133	context->d.postFrame = mGLContextPostFrame;
134	context->d.drawFrame = mGLContextDrawFrame;
135	context->d.setMessage = 0;
136	context->d.clearMessage = 0;
137}