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 v->width = 1;
28 v->height = 1;
29 glGenTextures(2, context->tex);
30 glBindTexture(GL_TEXTURE_2D, context->tex[0]);
31 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
32#ifndef _WIN32
33 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
34 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
35#endif
36 glBindTexture(GL_TEXTURE_2D, context->tex[1]);
37 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
38#ifndef _WIN32
39 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
40 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
41#endif
42 context->activeTex = 0;
43}
44
45static void mGLContextSetDimensions(struct VideoBackend* v, unsigned width, unsigned height) {
46 struct mGLContext* context = (struct mGLContext*) v;
47 if (width == v->width && height == v->height) {
48 return;
49 }
50 v->width = width;
51 v->height = height;
52
53 glBindTexture(GL_TEXTURE_2D, context->tex[0]);
54#ifdef COLOR_16_BIT
55#ifdef COLOR_5_6_5
56 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
57#else
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);
59#endif
60#elif defined(__BIG_ENDIAN__)
61 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
62#else
63 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
64#endif
65
66 glBindTexture(GL_TEXTURE_2D, context->tex[1]);
67#ifdef COLOR_16_BIT
68#ifdef COLOR_5_6_5
69 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
70#else
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);
72#endif
73#elif defined(__BIG_ENDIAN__)
74 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 0);
75#else
76 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
77#endif
78}
79
80static void mGLContextDeinit(struct VideoBackend* v) {
81 struct mGLContext* context = (struct mGLContext*) v;
82 glDeleteTextures(2, context->tex);
83}
84
85static void mGLContextResized(struct VideoBackend* v, unsigned w, unsigned h) {
86 unsigned drawW = w;
87 unsigned drawH = h;
88 if (v->lockAspectRatio) {
89 lockAspectRatioUInt(v->width, v->height, &drawW, &drawH);
90 }
91 if (v->lockIntegerScaling) {
92 lockIntegerRatioUInt(v->width, &drawW);
93 lockIntegerRatioUInt(v->height, &drawH);
94 }
95 glMatrixMode(GL_MODELVIEW);
96 glLoadIdentity();
97 glClearColor(0, 0, 0, 0);
98 glClear(GL_COLOR_BUFFER_BIT);
99 glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
100}
101
102static void mGLContextClear(struct VideoBackend* v) {
103 UNUSED(v);
104 glClearColor(0, 0, 0, 0);
105 glClear(GL_COLOR_BUFFER_BIT);
106}
107
108void mGLContextDrawFrame(struct VideoBackend* v) {
109 struct mGLContext* context = (struct mGLContext*) v;
110 glEnable(GL_TEXTURE_2D);
111 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
112 glEnableClientState(GL_VERTEX_ARRAY);
113 glVertexPointer(2, GL_INT, 0, _glVertices);
114 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
115 glMatrixMode(GL_PROJECTION);
116 glLoadIdentity();
117 glOrtho(0, v->width, v->height, 0, 0, 1);
118 glMatrixMode(GL_MODELVIEW);
119 glLoadIdentity();
120 if (v->interframeBlending) {
121 glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA);
122 glBlendColor(1, 1, 1, 0.5);
123 glBindTexture(GL_TEXTURE_2D, context->tex[context->activeTex ^ 1]);
124 if (v->filter) {
125 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
126 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
127 } else {
128 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
129 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
130 }
131 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
132 glEnable(GL_BLEND);
133 }
134 glBindTexture(GL_TEXTURE_2D, context->tex[context->activeTex]);
135 if (v->filter) {
136 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
137 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
138 } else {
139 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
140 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
141 }
142 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
143 glDisable(GL_BLEND);
144}
145
146void mGLContextPostFrame(struct VideoBackend* v, const void* frame) {
147 struct mGLContext* context = (struct mGLContext*) v;
148 context->activeTex ^= 1;
149 glBindTexture(GL_TEXTURE_2D, context->tex[context->activeTex]);
150#ifdef COLOR_16_BIT
151#ifdef COLOR_5_6_5
152 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
153#else
154 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
155#endif
156#elif defined(__BIG_ENDIAN__)
157 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, frame);
158#else
159 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_BYTE, frame);
160#endif
161}
162
163void mGLContextCreate(struct mGLContext* context) {
164 context->d.init = mGLContextInit;
165 context->d.deinit = mGLContextDeinit;
166 context->d.setDimensions = mGLContextSetDimensions;
167 context->d.resized = mGLContextResized;
168 context->d.swap = 0;
169 context->d.clear = mGLContextClear;
170 context->d.postFrame = mGLContextPostFrame;
171 context->d.drawFrame = mGLContextDrawFrame;
172 context->d.setMessage = 0;
173 context->d.clearMessage = 0;
174}