[4744] | 1 | /* |
---|
[1853] | 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. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5343] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[5357] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
[1853] | 17 | |
---|
[5343] | 18 | #include "text.h" |
---|
| 19 | #include "font.h" |
---|
[1853] | 20 | |
---|
[7193] | 21 | #include "util/loading/resource_manager.h" |
---|
[5343] | 22 | #include "debug.h" |
---|
| 23 | |
---|
[1856] | 24 | using namespace std; |
---|
[1853] | 25 | |
---|
[5343] | 26 | /** |
---|
[7355] | 27 | * @brief creates a new Text Element |
---|
[5343] | 28 | * @param fontFile the Font to render this text in |
---|
| 29 | * @param type The renderType to display this font in |
---|
| 30 | */ |
---|
[7221] | 31 | Text::Text(const std::string& fontFile, unsigned int textSize) |
---|
[5343] | 32 | { |
---|
[7355] | 33 | this->setClassID(CL_TEXT, "Text"); |
---|
[1856] | 34 | |
---|
[7355] | 35 | // initialize this Text |
---|
| 36 | this->font = NULL; |
---|
[7726] | 37 | this->size = textSize; |
---|
[7919] | 38 | this->setSizeY2D(size); |
---|
[7742] | 39 | this->blending = TEXT_DEFAULT_BLENDING; |
---|
| 40 | this->color = TEXT_DEFAULT_COLOR; |
---|
[7455] | 41 | |
---|
[7753] | 42 | this->setAlignment(TEXT_DEFAULT_ALIGNMENT); |
---|
| 43 | |
---|
[7455] | 44 | this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE); |
---|
[7753] | 45 | } |
---|
[7455] | 46 | |
---|
[7753] | 47 | Text::Text(const Text& text) |
---|
| 48 | { |
---|
| 49 | this->setClassID(CL_TEXT, "Text"); |
---|
| 50 | this->font = NULL; |
---|
| 51 | |
---|
| 52 | *this = text; |
---|
[5343] | 53 | } |
---|
| 54 | |
---|
[7753] | 55 | |
---|
[3245] | 56 | /** |
---|
[7355] | 57 | * @brief deletes a Text out of memory |
---|
[5343] | 58 | */ |
---|
| 59 | Text::~Text() |
---|
| 60 | { |
---|
[5767] | 61 | if (this->font != NULL && this->font != Font::getDefaultFont()) |
---|
[5343] | 62 | ResourceManager::getInstance()->unload(this->font); |
---|
[3365] | 63 | } |
---|
[1853] | 64 | |
---|
[7753] | 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 | } |
---|
[1853] | 78 | |
---|
[3245] | 79 | /** |
---|
[7753] | 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 | /** |
---|
[7919] | 130 | * @brief appends one Character to the String. |
---|
| 131 | */ |
---|
| 132 | void Text::appendCharacter(char character) |
---|
| 133 | { |
---|
| 134 | this->text += character; |
---|
| 135 | this->setupTextWidth(); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | /** |
---|
[7753] | 140 | * @brief append some text to the already existing Text. |
---|
| 141 | * @param appendText The text to append to this Text. |
---|
| 142 | */ |
---|
| 143 | const std::string& Text::operator <<(const std::string& appendText) |
---|
| 144 | { |
---|
| 145 | this->append(appendText); |
---|
| 146 | return this->text; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /** |
---|
[7919] | 150 | * @brief removes char characters from the Text. |
---|
| 151 | * |
---|
| 152 | * @note this function checks, if the count can be removed, and if so does it. |
---|
| 153 | * Otherwise the maximum count of characters will be removed. |
---|
| 154 | */ |
---|
| 155 | void Text::removeCharacters(unsigned int chars) |
---|
| 156 | { |
---|
| 157 | if (text.size() > chars) |
---|
| 158 | this->text.resize(this->text.size()-chars); |
---|
| 159 | else if (!text.empty()) |
---|
| 160 | text.clear(); |
---|
| 161 | this->setupTextWidth(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | /** |
---|
[7355] | 166 | * @brief sets the Font of this Text to font from fontFile |
---|
[5343] | 167 | * @param fontFile the File to load the Font from. |
---|
| 168 | * @param fontSize the Size of the Font |
---|
| 169 | */ |
---|
[7221] | 170 | void Text::setFont(const std::string& fontFile, unsigned int fontSize) |
---|
[5343] | 171 | { |
---|
[7455] | 172 | Font* newFont = NULL; |
---|
[7426] | 173 | Font* oldFont = this->font; |
---|
[5343] | 174 | |
---|
[5345] | 175 | // load a new Font |
---|
[7221] | 176 | if (!fontFile.empty()) |
---|
[5344] | 177 | { |
---|
[7426] | 178 | newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize); |
---|
| 179 | if (newFont == NULL) |
---|
| 180 | { |
---|
| 181 | newFont = Font::getDefaultFont(); |
---|
[7221] | 182 | PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str()); |
---|
[7426] | 183 | } |
---|
[5343] | 184 | } |
---|
[7455] | 185 | if (newFont == NULL) |
---|
[7426] | 186 | newFont = Font::getDefaultFont(); |
---|
[7455] | 187 | assert(newFont != NULL); |
---|
[7426] | 188 | |
---|
| 189 | // unloading the Font if we alrady have one loaded. |
---|
| 190 | this->font = newFont; |
---|
| 191 | if (oldFont != NULL && oldFont != Font::getDefaultFont()) |
---|
| 192 | ResourceManager::getInstance()->unload(oldFont); |
---|
[7450] | 193 | |
---|
| 194 | this->setupTextWidth(); |
---|
[5343] | 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
[7453] | 198 | * @brief sets the Size of the Font |
---|
| 199 | * @param size :the size of the Text |
---|
| 200 | */ |
---|
| 201 | void Text::setSize(float size) |
---|
| 202 | { |
---|
| 203 | this->size = size; |
---|
| 204 | this->setSizeY2D(size); |
---|
| 205 | this->setupTextWidth(); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | /** |
---|
[7355] | 210 | * @brief draws the Text |
---|
[5343] | 211 | */ |
---|
| 212 | void Text::draw() const |
---|
| 213 | { |
---|
[7448] | 214 | if (unlikely(this->text.empty())) |
---|
| 215 | return; |
---|
[5343] | 216 | glPushMatrix(); |
---|
[7919] | 217 | glPushAttrib(GL_ENABLE_BIT); |
---|
[5343] | 218 | // transform for alignment. |
---|
| 219 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
[5767] | 220 | glTranslatef(-this->getSizeX2D(), 0, 0); |
---|
[5343] | 221 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
[5767] | 222 | glTranslatef(-this->getSizeX2D()/2, 0, 0); |
---|
[5343] | 223 | |
---|
| 224 | // drawing this Text. |
---|
| 225 | // setting the Blending effects |
---|
| 226 | glColor4f(this->color.x, this->color.y, this->color.z, this->blending); |
---|
| 227 | glEnable(GL_BLEND); |
---|
| 228 | glEnable(GL_TEXTURE_2D); |
---|
| 229 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 230 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); |
---|
| 231 | |
---|
[7448] | 232 | glBindTexture(GL_TEXTURE_2D, font->getTexture()); |
---|
| 233 | glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); |
---|
| 234 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
| 235 | |
---|
| 236 | Glyph* tmpGlyph; |
---|
| 237 | float posX = 0.0f; |
---|
| 238 | glBegin(GL_QUADS); |
---|
| 239 | for (unsigned int i = 0; i < this->text.size(); i++) |
---|
[5343] | 240 | { |
---|
[7450] | 241 | if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL)) |
---|
[5343] | 242 | { |
---|
[7448] | 243 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
[7450] | 244 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0); |
---|
[5419] | 245 | |
---|
[7448] | 246 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
[7450] | 247 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize()); |
---|
[5419] | 248 | |
---|
[7448] | 249 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
[7450] | 250 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize()); |
---|
[5419] | 251 | |
---|
[7448] | 252 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
[7450] | 253 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0); |
---|
[5419] | 254 | |
---|
[7450] | 255 | posX += tmpGlyph->advance * this->getSize(); |
---|
[5343] | 256 | } |
---|
| 257 | } |
---|
[7448] | 258 | glEnd(); |
---|
[7919] | 259 | glPopAttrib(); |
---|
[5343] | 260 | glPopMatrix(); |
---|
| 261 | } |
---|
| 262 | |
---|
[7450] | 263 | |
---|
[5343] | 264 | /** |
---|
[7450] | 265 | * @brief setting up the Text-Width. |
---|
[5343] | 266 | */ |
---|
[7450] | 267 | void Text::setupTextWidth() |
---|
[5343] | 268 | { |
---|
[7450] | 269 | float width = 0; |
---|
| 270 | for (unsigned int i = 0; i < this->text.size(); i++) |
---|
| 271 | if(this->font->getGlyphArray()[this->text[i]] != NULL) |
---|
| 272 | width += this->font->getGlyphArray()[this->text[i]]->advance; |
---|
[7919] | 273 | this->setSizeX2D(width * this->getSize()); |
---|
[5343] | 274 | } |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | /** |
---|
[7450] | 278 | * @brief prints out some nice debug information about this text |
---|
[5343] | 279 | */ |
---|
[7450] | 280 | void Text::debug() const |
---|
[5343] | 281 | { |
---|
[7450] | 282 | PRINT(0)("=== TEXT: %s (with Font:'%s') displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str()); |
---|
| 283 | PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z); |
---|
[5343] | 284 | } |
---|
| 285 | |
---|