-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFont.cpp
197 lines (148 loc) · 4.42 KB
/
Font.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
#include "Font.h"
#include "Draw.h"
#include "System.h"
#include <SDL/SDL_opengl.h>
#include <fstream>
TrueTypeFont::TrueTypeFont()
{
TTF_Init();
ttf = TTF_OpenFont( LOCAL_FILE("art/font/xlmonoalt.ttf"), 20 );
if( ! ttf )
throw "Could not load font.";
}
TrueTypeFont::~TrueTypeFont()
{
}
void TrueTypeFont::draw( const std::string& text, float x, float y )
{
SDL_Color c = { 1, 1, 1, 1 };
SDL_Color v = { 0, 0, 0, 0 };
SDL_Surface *Message = TTF_RenderText_Shaded(ttf, text.c_str(), c, v);
if( ! Message )
return;
unsigned int tex = 0;
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, Message->w, Message->h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, Message->pixels);
int texCoords[] = {
0, 0,
1, 0,
1, 1,
0, 1
};
int verts[] = {
0, 0,
Message->w, 0,
Message->w, Message->h,
0, Message->h
};
glTranslatef( x, y, 0 );
draw::draw( verts, 4, tex, texCoords );
glLoadIdentity();
glDeleteTextures( 1, &tex );
SDL_FreeSurface( Message );
}
TextBox::TextBox( TrueTypeFont& font, float x, float y )
: font(font), x(x), y(y), line(0)
{
}
void TextBox::write( const std::string& text )
{
// This algorithm REQUIRES that end initialize to 0 - 1.
std::string::size_type i, end = 0 - 1;
do {
// This prevents i from referencing the \n char.
i = end + 1;
end = text.find( '\n', i );
if( end == std::string::npos )
end = text.size();
writeln( text.substr(i,end) );
} while( end < text.size() );
}
void TextBox::writeln( const std::string& text )
{
font.draw( text, x, y );
y += TrueTypeFont::LINE_HEIGHT;
}
TextLine::TextLine( TrueTypeFont* f, const std::string& t,
const Vector<float,2>& p )
: ttf(f), text(t), pos(p), color(1,1,1,1)
{
SDL_Color c = { 1, 1, 1, 1 };
SDL_Color v = { 0, 0, 0, 0 };
SDL_Surface* sdlTexture = TTF_RenderText_Shaded( ttf->ttf, text.c_str(), c, v );
dims.x( sdlTexture->w );
dims.y( sdlTexture->h );
if( !sdlTexture )
throw;
glGenTextures( 1, &tex );
glBindTexture( GL_TEXTURE_2D, tex );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, dims.x(), dims.y(), 0,
GL_ALPHA, GL_UNSIGNED_BYTE, sdlTexture->pixels );
SDL_FreeSurface( sdlTexture );
}
void TextLine::str( const std::string& s )
{
text = s;
glDeleteTextures( 1, &tex );
SDL_Color c = { 1, 1, 1, 1 };
SDL_Color v = { 0, 0, 0, 0 };
SDL_Surface* sdlTexture = TTF_RenderText_Shaded( ttf->ttf, text.c_str(), c, v );
dims.x( sdlTexture->w );
dims.y( sdlTexture->h );
if( !sdlTexture )
throw;
glBindTexture( GL_TEXTURE_2D, tex );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_ALPHA, dims.x(), dims.y(), 0,
GL_ALPHA, GL_UNSIGNED_BYTE, sdlTexture->pixels );
SDL_FreeSurface( sdlTexture );
}
void TextLine::draw()
{
static int texCoords[] = {
0, 0,
1, 0,
1, 1,
0, 1
};
float verts[] = {
0, 0,
dims.x(), 0,
dims.x(), dims.y(),
0, dims.y()
};
glTranslatef( pos.x(), pos.y(), 0 );
glColor4f( color.r(), color.g(), color.b(), color.a() );
draw::draw( verts, 4, tex, texCoords );
glLoadIdentity();
}
TextLine::~TextLine()
{
glDeleteTextures( 1, &tex );
}
LinePrinter::LinePrinter( TrueTypeFont* f, const Vector<float,2>& p )
: ttf(f), pos( p ), color(1,1,1,1), space( 0.0f )
{
}
void LinePrinter::add_line( const std::string& str )
{
Vector<float, 2> p = pos;
p.y( p.y() + space );
lines.push_back(
std::tr1::shared_ptr<TextLine>( new TextLine(ttf, str, p) )
);
lines.back()->color = color;
space += lines.back()->dims.y();
}
void LinePrinter::add_line()
{
// Assume the user wants the space of the previous line added.
if( lines.size() > 0 )
space += lines.back()->dims.y();
}