forked from githole/Live-Coder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapFontGL.h
286 lines (256 loc) · 7.59 KB
/
BitmapFontGL.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef _BITMAP_FONT_GL_
#define _BITMAP_FONT_GL_
#ifdef __WIN32__
#include <windows.h>
#endif __WIN32__
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include <string.h>
#include "lcfont_bw112x66.h"
#include "inconsolata_224x123.h"
#include "inconsolata_160x90.h"
namespace LiveCoder {
#define SyntaxTableNum 38
static const char* syntaxTable[] = {
"if", "break", "int", "case", "continue", "return", "default", "do",
"else", "struct", "switch", "float", "for", "unsigned", "goto", "while", "void", "const", "signed",
"vec2", "vec3", "vec4", "mat2", "mat3", "mat4", "precision", "lowp", "mediump", "highp", "uniform",
"attribute", "varying", "gl_Vertex", "gl_FragCoord", "gl_FragColor", "sampler2D", "#ifdef", "#endif",
};
class BitmapFontGL
{
private:
unsigned int texName;
BitmapFontGL(){};
bool IsComment;
int fontWidth;
int fontHeight;
public:
static BitmapFontGL* Instance() {
static BitmapFontGL instance; // —Bˆê‚̃Cƒ“ƒXƒ^ƒ“ƒX
return &instance;
}
void SetFontSize(int width, int height) {
fontWidth = width;
fontHeight = height;
}
void CreateTexture()
{
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 160, 90,
0, GL_LUMINANCE, GL_UNSIGNED_BYTE, inconsolata_160x90);
glPopAttrib();
}
void DrawCursor(int col, int row, float aspect, float width) {
unsigned int ddx = row * fontWidth;
unsigned int ddy = -col * fontHeight;
unsigned int w = 2;
glPushMatrix();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_1D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
glScalef(0.25f * 8.0f / width,
0.25f * 8.0f / width * aspect,
0.25f * 8.0f / width);
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex2i (ddx , ddy );
glVertex2i (ddx + w , ddy );
glVertex2i (ddx + w , ddy - fontHeight );
glVertex2i (ddx , ddy - fontHeight );
glEnd();
glPopAttrib();
glPopMatrix();
}
void DrawSelect(float aspect, float width, int line, int start, int end, float red = 1.0f, float green = 1.0f, float blue = 1.0f) {
unsigned int ddx = start * fontWidth;
unsigned int ddy = -line * fontHeight;
unsigned int w = 4 + (end - start) * fontWidth;
glPushMatrix();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_1D);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
glScalef(0.25f * 8.0f / width,
0.25f * 8.0f / width * aspect,
0.25f * 8.0f / width);
glBegin(GL_QUADS);
glColor4f(1.0, 1.0, 1.0, 1.0);
glVertex2i (ddx , ddy );
glVertex2i (ddx + w , ddy );
glVertex2i (ddx + w , ddy - fontHeight );
glVertex2i (ddx , ddy - fontHeight );
glEnd();
glPopAttrib();
glPopMatrix();
}
void Reset() {
IsComment = false;
}
void ProcessComment(std::string& str) {
for (int i = 0; i < str.length(); i ++) {
if (IsComment) {
int loc = str.find("*/", i);
if (loc != std::string::npos) {
IsComment = false;
i = loc + 2;
} else break;
} else {
int loc = str.find("/*", i);
if (loc != std::string::npos) {
IsComment = true;
i = loc + 2;
} else break;
}
}
}
void DrawLine(const char* strbuf, float aspect, float width, int line, float upAlpha, float downAlpha, bool isError = false, float red = 1.0f, float green = 1.0f, float blue = 1.0f) {
unsigned int ddx = 0;
unsigned int ddy = -fontHeight * line;
glPushMatrix();
glScalef(0.25f * 8.0f / width,
0.25f * 8.0f / width * aspect,
0.25f * 8.0f / width);
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
///glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
//glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_COLOR_MATERIAL);
glBindTexture(GL_TEXTURE_2D, texName);
unsigned int strend = strlen(strbuf);
std::string str = strbuf;
std::vector<int> syntaxColor;
if (IsComment) {
syntaxColor.resize(strend, 2);
} else {
syntaxColor.resize(strend, 0);
}
for (int i = 0; i < str.length(); i ++) {
if (IsComment) {
int loc = str.find("*/", i);
if (loc != std::string::npos) {
IsComment = false;
for (int j = loc + 2; j < str.length(); j ++)
syntaxColor[j] = 0;
i = loc + 2;
} else break;
} else {
int loc = str.find("/*", i);
if (loc != std::string::npos) {
IsComment = true;
for (int j = loc; j < str.length(); j ++)
syntaxColor[j] = 2;
i = loc + 2;
} else break;
}
}
int loc = str.find("//", 0);
if (loc != std::string::npos) {
for (int j = loc; j < str.length(); j ++)
syntaxColor[j] = 2;
}
for (int i = 0; i < SyntaxTableNum; i ++) {
for (int j = 0; j < strend; j ++) {
int loc = str.find(syntaxTable[i], j);
if (loc != std::string::npos) {
bool ok = true;
int len = strlen(syntaxTable[i]);
if (loc - 1 >= 0 && isalpha(str[loc - 1])) {
ok = false;
}
if (loc + len < strend && isalpha(str[loc + len])) {
ok = false;
}
if (ok) {
for (int k = loc; k < loc + len; k ++) {
if (syntaxColor[k] == 0)
syntaxColor[k] = 1;
}
j = loc + len;
}
} else break;
}
}
for(unsigned int strptr = 0; strptr < strend; strptr++)
{
if (strbuf[strptr] >= 0x20 && strbuf[strptr] < 0x80)
{
unsigned int lowbit = (strbuf[strptr] - 0x20) & 0x0F;
unsigned int highbit = ((strbuf[strptr] - 0x20) >> 4) & 0x0F;
float ptx = lowbit / 16.0f;
float pty = 1.0f - highbit / 6.0f;
float dtx = 1.0f / 16.0f;
float dty = 1.0f / 6.0f;
float r = red, g = green, b = blue;
if (isError) {
r = 1.0;
g = 0.05;
b = 0.05;
} else {
switch (syntaxColor[strptr]) {
case 0:
break;
case 1:
r = 0.6;
g = 0.6;
b = 1.0;
break;
case 2:
r = 0.6;
g = 1.0;
b = 0.6;
break;
}
}
/*
if (syntaxColor[strptr] == 0) {
} else {
}*/
glBegin(GL_QUADS);
glColor4f(r, g, b, upAlpha);
glTexCoord2f(ptx , pty );
glVertex2i (ddx , ddy );
glTexCoord2f(ptx + dtx , pty );
glVertex2i (ddx + fontWidth , ddy );
glColor4f(r, g, b, downAlpha);
glTexCoord2f(ptx + dtx , pty - dty );
glVertex2i (ddx + fontWidth , ddy - fontHeight );
glTexCoord2f(ptx , pty - dty );
glVertex2i (ddx , ddy - fontHeight );
glEnd();
}
ddx += fontWidth;
if (strbuf[strptr] == 0xA || strbuf[strptr] == 0xC)
{
ddx = 0;
ddy -= fontHeight;
}
}
glPopAttrib();
glPopMatrix();
}
};
};
#endif