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
|
/*
render/text.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "auxiliary/functions.h"
#include "render/text.h"
#include "render/textures.h"
#include "sys/sys.h"
namespace render
{
float Text::text_fontwidth = 16.0f;
float Text::text_fontheight = 24.0f;
math::Color * Text::base_color[10];
math::Color * Text::core_color[26];
void Text::init()
{
// base colors
// Black=0, Red=1, Green=2, Yellow=3, Blue=4, Cyan=5, Magenta=6, White=7
base_color[0] = new math::Color(0, 0, 0);
base_color[1] = new math::Color(1, 0, 0);
base_color[2] = new math::Color(0, 1, 0);
base_color[3] = new math::Color(1, 1, 0);
base_color[4] = new math::Color(0, 0, 1);
base_color[5] = new math::Color(0, 1, 1);
base_color[6] = new math::Color(1, 0, 1);
base_color[7] = new math::Color(1, 1, 1);
for (size_t i=0; i< 26; i++) {
core_color[i] = new math::Color(.7, .7, .7);
}
// N - normal color
core_color[(size_t)('N'-'A')]->assign(.7, .7, .7);
// D - Debug color
core_color[(size_t)('D'-'A')]->assign(.6, .6, .6);
// B - bold color
core_color[(size_t)('B'-'A')]->assign(1, 1, 1);
// W - warning color
core_color[(size_t)('W'-'A')]->assign(1, 1, 0);
// R - Error color
core_color[(size_t)('R'-'A')]->assign(1, 0, 0);
// F - Fancy color
core_color[(size_t)('F'-'A')]->assign(0, 1, 0);
}
void Text::shutdown()
{
for (size_t i=0; i< 7; i++) {
delete base_color[i];
base_color[i] = 0;
}
for (size_t i=0; i< 26; i++) {
delete core_color[i];
core_color[i] = 0;
}
}
void Text::setcolor(const char color)
{
if (('A' <= color) && (color <= 'Z')) {
gl::color(*core_color[(size_t) (color - 'A')]);
}
else if (('0' <= color) && (color <= '9')) {
gl::color(*base_color[(size_t) (color - '0')]);
}
else {
gl::color(1, 1, 1);
}
}
void Text::setfont(const char *texture, float width, float height)
{
Textures::bind(texture, false);
text_fontwidth = width;
text_fontheight = height;
}
void Text::draw(float x, float y, const char ascii)
{
if (ascii != ' ') {
int row = (int) ascii >> 4;
int col = (int) ascii & 15;
float frow = row * 0.0625f;
float fcol = col * 0.0625f;
gl::begin(gl::Quads);
glTexCoord2f(fcol, frow);
gl::vertex(x,y,1);
glTexCoord2f(fcol + 0.0625f, frow);
gl::vertex(x+text_fontwidth,y,1);
glTexCoord2f(fcol +0.0625f, frow + 0.0625f);
gl::vertex(x+text_fontwidth,y+text_fontheight,1);
glTexCoord2f(fcol, frow+0.0625f);
gl::vertex(x,y+text_fontheight,1);
gl::end();
}
}
void Text::draw(float x, float y, const char *text)
{
const char *c = text;
while (*c) {
if (aux::is_base_color_code(c)) {
c++;
gl::color(*base_color[ (size_t)(*c - '0')]);
} else if (aux::is_core_color_code(c)) {
c++;
gl::color(*core_color[ (size_t)(*c - 'A')]);
} else {
draw(x, y, *c);
x += text_fontwidth;
}
c++;
}
}
void Text::draw(float x, float y, std::stringstream & textstream)
{
char line[MAXCMDSIZE];
while (textstream.getline(line, MAXCMDSIZE-1)) {
draw(x, y, line);
y += text_fontheight;
}
textstream.clear();
}
void Text::draw(float x, float y, const std::string & text)
{
draw(x, y, text.c_str());
}
}
|