-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawtext.cpp
259 lines (227 loc) · 5.56 KB
/
drawtext.cpp
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
#include "drawtext.h"
#include "GL/glew.h"
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h> // vsnprintf
#include <vector>
#include <utility>
using namespace std;
struct TextColor
{
float r, g, b, a;
};
static vector< pair<string, TextColor> > g_strings, g_staticStrings;
static float g_textColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
static float g_fpsColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
static bool g_reverseText = false, g_alphaEffect = false;
static bool g_wordWrap = true, g_drawFps = false;
void setTextColor3f(float r, float g, float b)
{
g_textColor[0] = r;
g_textColor[1] = g;
g_textColor[2] = b;
g_textColor[3] = 1.0f;
}
void setTextColor4f(float r, float g, float b, float a)
{
g_textColor[0] = r;
g_textColor[1] = g;
g_textColor[2] = b;
g_textColor[3] = a;
}
void setTextColor3fv(float* vals)
{
g_textColor[0] = vals[0];
g_textColor[1] = vals[1];
g_textColor[2] = vals[2];
g_textColor[3] = 1.0f;
}
void setTextColor4fv(float * vals)
{
g_textColor[0] = vals[0];
g_textColor[1] = vals[1];
g_textColor[2] = vals[2];
g_textColor[3] = vals[3];
}
void setTextParameterb(TEXT_PARAMETER what, bool b)
{
switch(what)
{
case TEXT_REVERSE_ORDER:
g_reverseText = b;
break;
case TEXT_ALPHA_EFFECT:
g_alphaEffect = b;
break;
case TEXT_WORD_WRAP:
g_wordWrap = b;
break;
}
}
void setFpsColor3f(float r, float g, float b)
{
g_fpsColor[0] = r;
g_fpsColor[1] = g;
g_fpsColor[2] = b;
g_fpsColor[3] = 1.0f;
}
void setFpsColor4f(float r, float g, float b, float a)
{
g_fpsColor[0] = r;
g_fpsColor[1] = g;
g_fpsColor[2] = b;
g_fpsColor[3] = a;
}
void setFpsColor3fv(float* vals)
{
g_fpsColor[0] = vals[0];
g_fpsColor[1] = vals[1];
g_fpsColor[2] = vals[2];
g_fpsColor[3] = 1.0f;
}
void setFpsColor4fv(float * vals)
{
g_fpsColor[0] = vals[0];
g_fpsColor[1] = vals[1];
g_fpsColor[2] = vals[2];
g_fpsColor[3] = vals[3];
}
void showFps(bool b)
{
g_drawFps = b;
}
string format(const char* s, va_list argList)
{
#ifdef _MSC_VER
#define vsnprintf _vsnprintf
#endif
string str;
const int startSize = 161;
int currSize, maxSize = 1000000;
char buff1[startSize];
currSize = startSize - 1;
int ret = vsnprintf(buff1, currSize, s, argList);
if(ret >= 0 && ret <= startSize)
str = string(buff1);
else
{
char* buff2 = NULL;
while((ret < 0 || ret > currSize + 1)&& currSize < maxSize)
{
currSize *= 2;
buff2 = (char*)realloc(buff2, currSize + 1);
ret = vsnprintf(buff2, currSize, s, argList);
}
str = string(buff2);
}
return str;
}
void drawText(const char* s, ...)
{
//cf. flipcode, morgan mcguires format() cotd
va_list argList;
va_start(argList, s);
string str = format(s, argList);
va_end(argList);
TextColor col = { g_textColor[0], g_textColor[1], g_textColor[2], g_textColor[3] };
g_strings.push_back(make_pair(str, col));
}
void addText(const char* s, ...)
{
//cf. flipcode, morgan mcguires format() cotd
va_list argList;
va_start(argList, s);
string str = format(s, argList);
va_end(argList);
TextColor col = { g_textColor[0], g_textColor[1], g_textColor[2], g_textColor[3] };
g_staticStrings.push_back(make_pair(str, col));
}
void renderTexts(int w, int h, int fontWidth, int fontHeight, GLuint fontList,
float fps)
{
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, w - 1, h - 1, 0.0f, 1.0f, -1.0f);
if(g_alphaEffect)
{
glPushAttrib(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT);
glDisable(GL_TEXTURE_1D);
glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_3D);
glDisable(GL_TEXTURE_CUBE_MAP);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
vector< pair<string, TextColor> > strings;
strings.resize(g_strings.size() + g_staticStrings.size());
copy(g_staticStrings.begin(), g_staticStrings.end(), strings.begin());
copy(g_strings.begin(), g_strings.end(), strings.begin() + g_staticStrings.size());
g_strings.clear();
int s = strings.size();
int line = 1, col = 0;
float currAlpha = 1.0f, stepAlpha = 1.0f/s;
if(!g_reverseText)
{
currAlpha = stepAlpha;
stepAlpha = -stepAlpha;
}
for(int i = 0; i < s; ++i)
{
int index = i;
if(g_reverseText)
index = s - 1 - i;
if(g_alphaEffect)
glColor4f(strings[index].second.r, strings[index].second.g, strings[index].second.b, currAlpha);
else
glColor4f(strings[index].second.r, strings[index].second.g, strings[index].second.b, strings[index].second.a);
glRasterPos2f(10.0f, line*fontHeight);
string& curr = strings[index].first;
col = 0;
int l = curr.length();
for(int j = 0; j < l; ++j)
{
unsigned char currChar = (unsigned char)curr[j];
if(currChar == '\n')
{
++line;
col = 0;
glRasterPos2f(0.0f, line*fontHeight);
}
else
{
col += fontWidth;
if(col > w && g_wordWrap)
{
++line;
col = 0;
glRasterPos2f(0.0f, line*fontHeight);
}
glCallList(currChar + fontList);
}
}
++line;
currAlpha -= stepAlpha;
}
if(g_drawFps)
{
char buff[20];
sprintf(buff, "FPS: %f", fps);
glColor4fv(g_fpsColor);
glRasterPos2f(5, h - 1 - 5);
glListBase(fontList);
glCallLists(strlen(buff), GL_UNSIGNED_BYTE, buff);
}
glPopAttrib();
if(g_alphaEffect)
glPopAttrib();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}