all repos — mgba @ 154dd2e8e7c064568b901387de5fdfb4341b0577

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 _glTexCoords[] = {
 11	0, 0,
 12	1, 0,
 13	1, 1,
 14	0, 1
 15};
 16
 17static void mGLContextInit(struct VideoBackend* v, WHandle handle) {
 18	UNUSED(handle);
 19	struct mGLContext* context = (struct mGLContext*) v;
 20	glGenTextures(1, &context->tex);
 21	glBindTexture(GL_TEXTURE_2D, context->tex);
 22	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 23#ifndef _WIN32
 24	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
 25	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
 26#endif
 27}
 28
 29static void mGLContextSetDimensions(struct VideoBackend* v, unsigned width, unsigned height) {
 30	struct mGLContext* context = (struct mGLContext*) v;
 31	v->width = width;
 32	v->height = height;
 33
 34	glBindTexture(GL_TEXTURE_2D, context->tex);
 35#ifdef COLOR_16_BIT
 36#ifdef COLOR_5_6_5
 37	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
 38#else
 39	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);
 40#endif
 41#else
 42	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
 43#endif
 44
 45	context->vertices[0] = 0;
 46	context->vertices[1] = 0;
 47	context->vertices[2] = toPow2(width);
 48	context->vertices[3] = 0;
 49	context->vertices[4] = toPow2(width);
 50	context->vertices[5] = toPow2(height);
 51	context->vertices[6] = 0;
 52	context->vertices[7] = toPow2(height);
 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, context->vertices);
 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#else
120	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height,  GL_RGBA, GL_UNSIGNED_BYTE, frame);
121#endif
122}
123
124void mGLContextCreate(struct mGLContext* context) {
125	context->d.init = mGLContextInit;
126	context->d.deinit = mGLContextDeinit;
127	context->d.setDimensions = mGLContextSetDimensions;
128	context->d.resized = mGLContextResized;
129	context->d.swap = 0;
130	context->d.clear = mGLContextClear;
131	context->d.postFrame = mGLContextPostFrame;
132	context->d.drawFrame = mGLContextDrawFrame;
133	context->d.setMessage = 0;
134	context->d.clearMessage = 0;
135}