-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.cpp
More file actions
206 lines (157 loc) · 4.84 KB
/
Text.cpp
File metadata and controls
206 lines (157 loc) · 4.84 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Text.h"
#include <vector>
#include <string.h>
#include <glm/gtx/transform.hpp>
Text::Text(const Font *font, const char *string, const unsigned &len, const float &size)
{
float minx, maxx, miny, maxy;
maxx = minx = 0.5f;
miny = maxy = -0.5f;
this->font = font;
char *vertexbuffer = new char[(sizeof(float) * 2 + sizeof(unsigned short) * 2) * 4 * len];
union
{
char *u8;
unsigned short *u16;
float *f32;
} vertexp;
vertexp.u8 = vertexbuffer;
unsigned short *indexbuffer = new unsigned short[6 * len];
unsigned short *indexp = indexbuffer;
unsigned vertexsize = 0;
indexsize = 0;
maxlength = 0.0f;
height = font->LineHeight();
float cursor = 0.0f;
for(unsigned i = 0; i < len; ++i)
{
if(string[i] == '\0')
break;
else if(string[i] == '\n') // new line
{
if(cursor > maxlength)
maxlength = cursor;
cursor = 0.0f;
height += font->LineHeight();
}
else if(string[i] == '\t') // add 4 spaces
cursor += 4.0f * font->spaceadvance;
else if(string[i] == ' ') // add a space
cursor += font->spaceadvance;
else if(string[i] >= 33 && string[i] <= 126) // add a glyph
{
unsigned idx = string[i] - 33;
float x0 = font->glyph[idx].offsetx + cursor;
float y0 = font->glyph[idx].offsety - height;
float x1 = x0 + font->glyph[idx].width;
float y1 = y0 - font->glyph[idx].height;
unsigned short u0 = font->glyph[idx].u0;
unsigned short v0 = font->glyph[idx].v0;
unsigned short u1 = font->glyph[idx].u1;
unsigned short v1 = font->glyph[idx].v1;
if(x1 > maxx)
maxx = x1;
if(x0 < minx)
minx = x0;
if(y0 > maxy)
maxy = y0;
if(y1 < miny)
miny = y1;
cursor += font->glyph[idx].advance;
*indexp++ = vertexsize + 0;
*indexp++ = vertexsize + 1;
*indexp++ = vertexsize + 2;
*indexp++ = vertexsize + 2;
*indexp++ = vertexsize + 1;
*indexp++ = vertexsize + 3;
*vertexp.f32++ = x0;
*vertexp.f32++ = y0;
*vertexp.u16++ = u0;
*vertexp.u16++ = v0;
*vertexp.f32++ = x0;
*vertexp.f32++ = y1;
*vertexp.u16++ = u0;
*vertexp.u16++ = v1;
*vertexp.f32++ = x1;
*vertexp.f32++ = y0;
*vertexp.u16++ = u1;
*vertexp.u16++ = v0;
*vertexp.f32++ = x1;
*vertexp.f32++ = y1;
*vertexp.u16++ = u1;
*vertexp.u16++ = v1;
indexsize += 6;
vertexsize += 4;
}
}
if(cursor > maxlength)
maxlength = cursor;
// add bounding box
*indexp++ = vertexsize + 0;
*indexp++ = vertexsize + 1;
*indexp++ = vertexsize + 2;
*indexp++ = vertexsize + 2;
*indexp++ = vertexsize + 1;
*indexp++ = vertexsize + 3;
*vertexp.f32++ = minx + ((float)rand() / RAND_MAX - 0.5f) + font->Border();
*vertexp.f32++ = maxy + ((float)rand() / RAND_MAX - 0.5f) - font->Border();
*vertexp.u16++ = 0;
*vertexp.u16++ = 0;
*vertexp.f32++ = minx + ((float)rand() / RAND_MAX - 0.5f) + font->Border();
*vertexp.f32++ = miny + ((float)rand() / RAND_MAX - 0.5f) + font->Border();
*vertexp.u16++ = 0;
*vertexp.u16++ = 0;
*vertexp.f32++ = maxx + ((float)rand() / RAND_MAX - 0.5f) - font->Border();
*vertexp.f32++ = maxy + ((float)rand() / RAND_MAX - 0.5f) - font->Border();
*vertexp.u16++ = 0;
*vertexp.u16++ = 0;
*vertexp.f32++ = maxx + ((float)rand() / RAND_MAX - 0.5f) - font->Border();
*vertexp.f32++ = miny + ((float)rand() / RAND_MAX - 0.5f) + font->Border();
*vertexp.u16++ = 0;
*vertexp.u16++ = 0;
vertexsize += 4;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertexsize * (sizeof(float) * 2 + sizeof(unsigned short) * 2), vertexbuffer, GL_STATIC_DRAW);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (indexsize + 6) * sizeof(unsigned short), indexbuffer, GL_STATIC_DRAW);
delete [] indexbuffer;
delete [] vertexbuffer;
position[0] = -0.5f * maxlength;
position[1] = 0.5f * height;
this->size = size;
Update();
if(len != strlen(string)) // DELETE!
puts(string);
}
Text::~Text()
{
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
}
void Text::Draw()
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 12, (void*)0); // vertex
glVertexAttribPointer(1, 2, GL_UNSIGNED_SHORT, GL_TRUE, 12, (void*)8); // texture coordinate
glEnableVertexAttribArray(1);
glDrawElements(GL_TRIANGLES, indexsize, GL_UNSIGNED_SHORT, 0);
glDisableVertexAttribArray(1);
}
void Text::DrawBox()
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 12, (void*)0); // vertex
glVertexAttribPointer(1, 2, GL_UNSIGNED_SHORT, GL_TRUE, 12, (void*)8); // texture coordinate
glEnableVertexAttribArray(1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)(indexsize * 2));
glDisableVertexAttribArray(1);
}
void Text::Update()
{
m = scale(size, size, 1.0f);
m = translate(m, position[0], position[1], 0.0f);
}