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 "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 GBAGLContextInit(struct VideoBackend* v, unsigned width, unsigned height, WHandle handle) {
25 UNUSED(handle);
26 struct GBAGLContext* context = (struct GBAGLContext*) 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 v->width = width;
35 v->height = height;
36
37#ifdef COLOR_16_BIT
38#ifdef COLOR_5_6_5
39 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 0);
40#else
41 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, 0);
42#endif
43#else
44 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, toPow2(width), toPow2(height), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
45#endif
46}
47
48static void GBAGLContextDeinit(struct VideoBackend* v) {
49 struct GBAGLContext* context = (struct GBAGLContext*) v;
50 glDeleteTextures(1, &context->tex);
51}
52
53static void GBAGLContextResized(struct VideoBackend* v, unsigned w, unsigned h) {
54 unsigned drawW = w;
55 unsigned drawH = h;
56 if (v->lockAspectRatio) {
57 if (w * v->height > h * v->width) {
58 drawW = h * v->width / v->height;
59 } else if (w * v->height < h * v->width) {
60 drawH = w * v->height / v->width;
61 }
62 }
63 glMatrixMode(GL_MODELVIEW);
64 glLoadIdentity();
65 glClearColor(0, 0, 0, 0);
66 glClear(GL_COLOR_BUFFER_BIT);
67 glViewport((w - drawW) / 2, (h - drawH) / 2, drawW, drawH);
68}
69
70static void GBAGLContextClear(struct VideoBackend* v) {
71 UNUSED(v);
72 glClearColor(0, 0, 0, 0);
73 glClear(GL_COLOR_BUFFER_BIT);
74}
75
76void GBAGLContextDrawFrame(struct VideoBackend* v) {
77 struct GBAGLContext* context = (struct GBAGLContext*) v;
78 glEnable(GL_TEXTURE_2D);
79 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
80 glEnableClientState(GL_VERTEX_ARRAY);
81 glVertexPointer(2, GL_INT, 0, _glVertices);
82 glTexCoordPointer(2, GL_INT, 0, _glTexCoords);
83 glMatrixMode(GL_PROJECTION);
84 glLoadIdentity();
85 glOrtho(0, v->width, v->height, 0, 0, 1);
86 glMatrixMode(GL_MODELVIEW);
87 glLoadIdentity();
88 glBindTexture(GL_TEXTURE_2D, context->tex);
89 if (v->filter) {
90 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
92 } else {
93 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
94 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
95 }
96 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
97}
98
99void GBAGLContextPostFrame(struct VideoBackend* v, const void* frame) {
100 struct GBAGLContext* context = (struct GBAGLContext*) v;
101 glBindTexture(GL_TEXTURE_2D, context->tex);
102#ifdef COLOR_16_BIT
103#ifdef COLOR_5_6_5
104 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, frame);
105#else
106 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame);
107#endif
108#else
109 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, v->width, v->height, GL_RGBA, GL_UNSIGNED_BYTE, frame);
110#endif
111}
112
113void GBAGLContextCreate(struct GBAGLContext* context) {
114 context->d.init = GBAGLContextInit;
115 context->d.deinit = GBAGLContextDeinit;
116 context->d.resized = GBAGLContextResized;
117 context->d.swap = 0;
118 context->d.clear = GBAGLContextClear;
119 context->d.postFrame = GBAGLContextPostFrame;
120 context->d.drawFrame = GBAGLContextDrawFrame;
121 context->d.setMessage = 0;
122 context->d.clearMessage = 0;
123}