-
Notifications
You must be signed in to change notification settings - Fork 5
/
glfontstash.h
160 lines (133 loc) · 4.61 KB
/
glfontstash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// CHANGES BY JOHN BARTHOLOMEW, DOCUMENTED AS PER LICENSE CONDITION 2:
//
// * Code (both header and source) is wrapped with extern "C" when
// built in C++ mode, to ensure compatible linkage between C/C++
// files.
//
// Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
#ifndef GLFONTSTASH_H
#define GLFONTSTASH_H
#ifdef __cplusplus
extern "C" {
#endif
struct FONScontext* glfonsCreate(int width, int height, int flags);
void glfonsDelete(struct FONScontext* ctx);
unsigned int glfonsRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
#ifdef __cplusplus
} /* extern "C" */
#endif
#ifndef GLFONTSTASH_IMPLEMENTATION
#undef FONTSTASH_IMPLEMENTATION
#include "fontstash.h"
#endif
#endif
#ifdef GLFONTSTASH_IMPLEMENTATION
#define FONTSTASH_IMPLEMENTATION
#include "fontstash.h"
#ifdef __cplusplus
extern "C" {
#endif
struct GLFONScontext {
GLuint tex;
int width, height;
};
static int glfons__renderCreate(void* userPtr, int width, int height)
{
struct GLFONScontext* gl = (struct GLFONScontext*)userPtr;
(void)height;
glGenTextures(1, &gl->tex);
if (!gl->tex) return 0;
gl->width = width;
gl->height = width;
glBindTexture(GL_TEXTURE_2D, gl->tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gl->width, gl->height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
return 1;
}
static void glfons__renderUpdate(void* userPtr, int* rect, const unsigned char* data)
{
struct GLFONScontext* gl = (struct GLFONScontext*)userPtr;
int w = rect[2] - rect[0];
int h = rect[3] - rect[1];
if (gl->tex == 0) return;
glBindTexture(GL_TEXTURE_2D, gl->tex);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, gl->width);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, rect[0]);
glPixelStorei(GL_UNPACK_SKIP_ROWS, rect[1]);
glTexSubImage2D(GL_TEXTURE_2D, 0, rect[0], rect[1], w, h, GL_ALPHA,GL_UNSIGNED_BYTE, data);
}
static void glfons__renderDraw(void* userPtr, const float* verts, const float* tcoords, const unsigned int* colors, int nverts)
{
struct GLFONScontext* gl = (struct GLFONScontext*)userPtr;
if (gl->tex == 0) return;
glBindTexture(GL_TEXTURE_2D, gl->tex);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, sizeof(float)*2, verts);
glTexCoordPointer(2, GL_FLOAT, sizeof(float)*2, tcoords);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(unsigned int), colors);
glDrawArrays(GL_TRIANGLES, 0, nverts);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
static void glfons__renderDelete(void* userPtr)
{
struct GLFONScontext* gl = (struct GLFONScontext*)userPtr;
if (gl->tex)
glDeleteTextures(1, &gl->tex);
gl->tex = 0;
free(gl);
}
struct FONScontext* glfonsCreate(int width, int height, int flags)
{
struct FONSparams params;
struct GLFONScontext* gl;
gl = (struct GLFONScontext*)malloc(sizeof(struct GLFONScontext));
if (gl == NULL) goto error;
memset(gl, 0, sizeof(struct GLFONScontext));
memset(¶ms, 0, sizeof(params));
params.width = width;
params.height = height;
params.flags = (unsigned char)flags;
params.renderCreate = glfons__renderCreate;
params.renderUpdate = glfons__renderUpdate;
params.renderDraw = glfons__renderDraw;
params.renderDelete = glfons__renderDelete;
params.userPtr = gl;
return fonsCreateInternal(¶ms);
error:
if (gl != NULL) free(gl);
return NULL;
}
void glfonsDelete(struct FONScontext* ctx)
{
fonsDeleteInternal(ctx);
}
unsigned int glfonsRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
return (r) | (g << 8) | (b << 16) | (a << 24);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif