1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
17 | |
---|
18 | #include "text.h" |
---|
19 | #include "font.h" |
---|
20 | |
---|
21 | #include "util/loading/resource_manager.h" |
---|
22 | #include "debug.h" |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | /** |
---|
27 | * @brief creates a new Text Element |
---|
28 | * @param fontFile the Font to render this text in |
---|
29 | * @param type The renderType to display this font in |
---|
30 | */ |
---|
31 | Text::Text(const std::string& fontFile, unsigned int textSize) |
---|
32 | { |
---|
33 | this->setClassID(CL_TEXT, "Text"); |
---|
34 | |
---|
35 | // initialize this Text |
---|
36 | this->font = NULL; |
---|
37 | this->size = textSize; |
---|
38 | this->setSizeY2D(size); |
---|
39 | this->blending = TEXT_DEFAULT_BLENDING; |
---|
40 | this->color = TEXT_DEFAULT_COLOR; |
---|
41 | |
---|
42 | this->setAlignment(TEXT_DEFAULT_ALIGNMENT); |
---|
43 | |
---|
44 | this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE); |
---|
45 | } |
---|
46 | |
---|
47 | Text::Text(const Text& text) |
---|
48 | { |
---|
49 | this->setClassID(CL_TEXT, "Text"); |
---|
50 | this->font = NULL; |
---|
51 | |
---|
52 | *this = text; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * @brief deletes a Text out of memory |
---|
58 | */ |
---|
59 | Text::~Text() |
---|
60 | { |
---|
61 | if (this->font != NULL && this->font != Font::getDefaultFont()) |
---|
62 | ResourceManager::getInstance()->unload(this->font); |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * @brief compare the Text with another Text. |
---|
67 | * @param text the Text to compare. |
---|
68 | * @returns true if all the properties Match. |
---|
69 | */ |
---|
70 | bool Text::operator==(const Text& text) const |
---|
71 | { |
---|
72 | return (this->text == text.text && |
---|
73 | this->size == text.size && |
---|
74 | this->font == text.font && |
---|
75 | this->color == text.color && |
---|
76 | this->blending == text.blending); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * @brief compare this Text's internal String with the text. |
---|
81 | * @param text the Comparator Text. |
---|
82 | * @returns true on a match. |
---|
83 | */ |
---|
84 | bool Text::operator==(const std::string& text) const |
---|
85 | { |
---|
86 | return (this->text == text); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * @brief Copies the properties of one text onto the other one. |
---|
91 | * @param text: the Text to apply to this one. |
---|
92 | * @returns This-reference. |
---|
93 | */ |
---|
94 | Text& Text::operator=(const Text& text) |
---|
95 | { |
---|
96 | this->size = text.size; |
---|
97 | this->blending = text.blending; |
---|
98 | this->color = text.color; |
---|
99 | this->setAlignment(text.getAlignment()); |
---|
100 | if (this->font != NULL) |
---|
101 | ResourceManager::getInstance()->unload(this->font); |
---|
102 | |
---|
103 | this->font = (Font*)ResourceManager::getInstance()->copy( text.font ); //!< HACK |
---|
104 | |
---|
105 | this->text = text.text; |
---|
106 | return *this; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * @brief Sets a new Text to the font |
---|
111 | * @param text the new text to set |
---|
112 | */ |
---|
113 | void Text::setText(const std::string& text) |
---|
114 | { |
---|
115 | this->text = text; |
---|
116 | this->setupTextWidth(); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * @brief append some text to the already existing Text. |
---|
121 | * @param appendText The text to append to this Text. |
---|
122 | */ |
---|
123 | void Text::append(const std::string& appendText) |
---|
124 | { |
---|
125 | this->text += appendText; |
---|
126 | this->setupTextWidth(); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * @brief append some text to the already existing Text. |
---|
131 | * @param appendText The text to append to this Text. |
---|
132 | */ |
---|
133 | const std::string& Text::operator <<(const std::string& appendText) |
---|
134 | { |
---|
135 | this->append(appendText); |
---|
136 | return this->text; |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * @brief sets the Font of this Text to font from fontFile |
---|
141 | * @param fontFile the File to load the Font from. |
---|
142 | * @param fontSize the Size of the Font |
---|
143 | */ |
---|
144 | void Text::setFont(const std::string& fontFile, unsigned int fontSize) |
---|
145 | { |
---|
146 | Font* newFont = NULL; |
---|
147 | Font* oldFont = this->font; |
---|
148 | |
---|
149 | // load a new Font |
---|
150 | if (!fontFile.empty()) |
---|
151 | { |
---|
152 | newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize); |
---|
153 | if (newFont == NULL) |
---|
154 | { |
---|
155 | newFont = Font::getDefaultFont(); |
---|
156 | PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str()); |
---|
157 | } |
---|
158 | } |
---|
159 | if (newFont == NULL) |
---|
160 | newFont = Font::getDefaultFont(); |
---|
161 | assert(newFont != NULL); |
---|
162 | |
---|
163 | // unloading the Font if we alrady have one loaded. |
---|
164 | this->font = newFont; |
---|
165 | if (oldFont != NULL && oldFont != Font::getDefaultFont()) |
---|
166 | ResourceManager::getInstance()->unload(oldFont); |
---|
167 | |
---|
168 | this->setupTextWidth(); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * @brief sets the Size of the Font |
---|
173 | * @param size :the size of the Text |
---|
174 | */ |
---|
175 | void Text::setSize(float size) |
---|
176 | { |
---|
177 | this->size = size; |
---|
178 | this->setSizeY2D(size); |
---|
179 | this->setupTextWidth(); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | * @brief draws the Text |
---|
185 | */ |
---|
186 | void Text::draw() const |
---|
187 | { |
---|
188 | if (unlikely(this->text.empty())) |
---|
189 | return; |
---|
190 | glPushMatrix(); |
---|
191 | // transform for alignment. |
---|
192 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
193 | glTranslatef(-this->getSizeX2D(), 0, 0); |
---|
194 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
195 | glTranslatef(-this->getSizeX2D()/2, 0, 0); |
---|
196 | |
---|
197 | // drawing this Text. |
---|
198 | // setting the Blending effects |
---|
199 | glColor4f(this->color.x, this->color.y, this->color.z, this->blending); |
---|
200 | glEnable(GL_BLEND); |
---|
201 | glEnable(GL_TEXTURE_2D); |
---|
202 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
203 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); |
---|
204 | |
---|
205 | glBindTexture(GL_TEXTURE_2D, font->getTexture()); |
---|
206 | glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); |
---|
207 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
208 | |
---|
209 | Glyph* tmpGlyph; |
---|
210 | float posX = 0.0f; |
---|
211 | glBegin(GL_QUADS); |
---|
212 | for (unsigned int i = 0; i < this->text.size(); i++) |
---|
213 | { |
---|
214 | if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL)) |
---|
215 | { |
---|
216 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
217 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0); |
---|
218 | |
---|
219 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
220 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize()); |
---|
221 | |
---|
222 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
223 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize()); |
---|
224 | |
---|
225 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
226 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0); |
---|
227 | |
---|
228 | posX += tmpGlyph->advance * this->getSize(); |
---|
229 | } |
---|
230 | } |
---|
231 | glEnd(); |
---|
232 | glPopMatrix(); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | /** |
---|
237 | * @brief setting up the Text-Width. |
---|
238 | */ |
---|
239 | void Text::setupTextWidth() |
---|
240 | { |
---|
241 | float width = 0; |
---|
242 | for (unsigned int i = 0; i < this->text.size(); i++) |
---|
243 | if(this->font->getGlyphArray()[this->text[i]] != NULL) |
---|
244 | width += this->font->getGlyphArray()[this->text[i]]->advance; |
---|
245 | this->setSizeX2D(width * this->getSize()); |
---|
246 | printf("%s:: %f\n", this->text.c_str(), width * this->getSize()); |
---|
247 | } |
---|
248 | |
---|
249 | |
---|
250 | /** |
---|
251 | * @brief prints out some nice debug information about this text |
---|
252 | */ |
---|
253 | void Text::debug() const |
---|
254 | { |
---|
255 | PRINT(0)("=== TEXT: %s (with Font:'%s') displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str()); |
---|
256 | PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z); |
---|
257 | } |
---|
258 | |
---|