all repos — mgba @ b4fa4fe77e23ed774e749608144fc0d1330b941c

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	glMatrixMode(GL_MODELVIEW);
 71	glLoadIdentity();
 72	glClearColor(0, 0, 0, 0);
 73	glClear(GL_COLOR_BUFFER_BIT);
 74	glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
 75}
 76
 77static void mGLContextClear(struct VideoBackend* v) {
 78	UNUSED(v);
 79	glClearColor(0, 0, 0, 0);
 80	glClear(GL_COLOR_BUFFER_BIT);
 81}
 82
 83void mGLContextDrawFrame(struct VideoBackend* v) {
 84	struct mGLContext* context = (struct mGLContext*) v;
 85	glEnable(GL_TEXTURE_2D);
 86	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 87	glEnableClientState(GL_VERTEX_ARRAY);
 88	glVertexPointer(2, GL_INT, 0, context->vertices);
 89	glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
 90	glMatrixMode(GL_PROJECTION);
 91	glLoadIdentity();
 92	glOrtho(0, v->width, v->height, 0, 0, 1);
 93	glMatrixMode(GL_MODELVIEW);
 94	glLoadIdentity();
 95	glBindTexture(GL_TEXTURE_2D, context->tex);
 96	if (v->filter) {
 97		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 98		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 99	} else {
100		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
101		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
102	}
103	glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
104}
105
106void mGLContextPostFrame(struct VideoBackend* v, const void* frame) {
107	struct mGLContext* context = (struct mGLContext*) v;
108	glBindTexture(GL_TEXTURE_2D, context->tex);
109#ifdef COLOR_16_BIT
110#ifdef COLOR_5_6_5
111	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
112#else
113	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
114#endif
115#else
116	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height,  GL_RGBA, GL_UNSIGNED_BYTE, frame);
117#endif
118}
119
120void mGLContextCreate(struct mGLContext* context) {
121	context->d.init = mGLContextInit;
122	context->d.deinit = mGLContextDeinit;
123	context->d.setDimensions = mGLContextSetDimensions;
124	context->d.resized = mGLContextResized;
125	context->d.swap = 0;
126	context->d.clear = mGLContextClear;
127	context->d.postFrame = mGLContextPostFrame;
128	context->d.drawFrame = mGLContextDrawFrame;
129	context->d.setMessage = 0;
130	context->d.clearMessage = 0;
131}