[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->color = TEXT_DEFAULT_COLOR; |
---|
[7455] | 40 | |
---|
[7753] | 41 | this->setAlignment(TEXT_DEFAULT_ALIGNMENT); |
---|
| 42 | |
---|
[7455] | 43 | this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE); |
---|
[7753] | 44 | } |
---|
[7455] | 45 | |
---|
[7753] | 46 | Text::Text(const Text& text) |
---|
| 47 | { |
---|
| 48 | this->setClassID(CL_TEXT, "Text"); |
---|
| 49 | this->font = NULL; |
---|
| 50 | |
---|
| 51 | *this = text; |
---|
[5343] | 52 | } |
---|
| 53 | |
---|
[7753] | 54 | |
---|
[3245] | 55 | /** |
---|
[7355] | 56 | * @brief deletes a Text out of memory |
---|
[5343] | 57 | */ |
---|
| 58 | Text::~Text() |
---|
| 59 | { |
---|
[5767] | 60 | if (this->font != NULL && this->font != Font::getDefaultFont()) |
---|
[5343] | 61 | ResourceManager::getInstance()->unload(this->font); |
---|
[3365] | 62 | } |
---|
[1853] | 63 | |
---|
[7753] | 64 | /** |
---|
| 65 | * @brief compare the Text with another Text. |
---|
| 66 | * @param text the Text to compare. |
---|
| 67 | * @returns true if all the properties Match. |
---|
| 68 | */ |
---|
| 69 | bool Text::operator==(const Text& text) const |
---|
| 70 | { |
---|
| 71 | return (this->text == text.text && |
---|
| 72 | this->size == text.size && |
---|
| 73 | this->font == text.font && |
---|
[8448] | 74 | this->color == text.color); |
---|
[7753] | 75 | } |
---|
[1853] | 76 | |
---|
[3245] | 77 | /** |
---|
[7753] | 78 | * @brief compare this Text's internal String with the text. |
---|
| 79 | * @param text the Comparator Text. |
---|
| 80 | * @returns true on a match. |
---|
| 81 | */ |
---|
| 82 | bool Text::operator==(const std::string& text) const |
---|
| 83 | { |
---|
| 84 | return (this->text == text); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * @brief Copies the properties of one text onto the other one. |
---|
| 89 | * @param text: the Text to apply to this one. |
---|
| 90 | * @returns This-reference. |
---|
| 91 | */ |
---|
| 92 | Text& Text::operator=(const Text& text) |
---|
| 93 | { |
---|
| 94 | this->size = text.size; |
---|
| 95 | this->color = text.color; |
---|
| 96 | this->setAlignment(text.getAlignment()); |
---|
| 97 | if (this->font != NULL) |
---|
| 98 | ResourceManager::getInstance()->unload(this->font); |
---|
| 99 | |
---|
| 100 | this->font = (Font*)ResourceManager::getInstance()->copy( text.font ); //!< HACK |
---|
| 101 | |
---|
| 102 | this->text = text.text; |
---|
| 103 | return *this; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * @brief Sets a new Text to the font |
---|
| 108 | * @param text the new text to set |
---|
| 109 | */ |
---|
| 110 | void Text::setText(const std::string& text) |
---|
| 111 | { |
---|
| 112 | this->text = text; |
---|
| 113 | this->setupTextWidth(); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * @brief append some text to the already existing Text. |
---|
| 118 | * @param appendText The text to append to this Text. |
---|
| 119 | */ |
---|
| 120 | void Text::append(const std::string& appendText) |
---|
| 121 | { |
---|
| 122 | this->text += appendText; |
---|
| 123 | this->setupTextWidth(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
[7919] | 127 | * @brief appends one Character to the String. |
---|
| 128 | */ |
---|
| 129 | void Text::appendCharacter(char character) |
---|
| 130 | { |
---|
| 131 | this->text += character; |
---|
| 132 | this->setupTextWidth(); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | /** |
---|
[7753] | 137 | * @brief append some text to the already existing Text. |
---|
| 138 | * @param appendText The text to append to this Text. |
---|
| 139 | */ |
---|
| 140 | const std::string& Text::operator <<(const std::string& appendText) |
---|
| 141 | { |
---|
| 142 | this->append(appendText); |
---|
| 143 | return this->text; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
[7919] | 147 | * @brief removes char characters from the Text. |
---|
| 148 | * |
---|
| 149 | * @note this function checks, if the count can be removed, and if so does it. |
---|
| 150 | * Otherwise the maximum count of characters will be removed. |
---|
| 151 | */ |
---|
| 152 | void Text::removeCharacters(unsigned int chars) |
---|
| 153 | { |
---|
| 154 | if (text.size() > chars) |
---|
| 155 | this->text.resize(this->text.size()-chars); |
---|
| 156 | else if (!text.empty()) |
---|
| 157 | text.clear(); |
---|
| 158 | this->setupTextWidth(); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | /** |
---|
[7355] | 163 | * @brief sets the Font of this Text to font from fontFile |
---|
[5343] | 164 | * @param fontFile the File to load the Font from. |
---|
| 165 | * @param fontSize the Size of the Font |
---|
| 166 | */ |
---|
[7221] | 167 | void Text::setFont(const std::string& fontFile, unsigned int fontSize) |
---|
[5343] | 168 | { |
---|
[7455] | 169 | Font* newFont = NULL; |
---|
[7426] | 170 | Font* oldFont = this->font; |
---|
[5343] | 171 | |
---|
[5345] | 172 | // load a new Font |
---|
[7221] | 173 | if (!fontFile.empty()) |
---|
[5344] | 174 | { |
---|
[7426] | 175 | newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize); |
---|
| 176 | if (newFont == NULL) |
---|
| 177 | { |
---|
| 178 | newFont = Font::getDefaultFont(); |
---|
[7221] | 179 | PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str()); |
---|
[7426] | 180 | } |
---|
[5343] | 181 | } |
---|
[7455] | 182 | if (newFont == NULL) |
---|
[7426] | 183 | newFont = Font::getDefaultFont(); |
---|
[7455] | 184 | assert(newFont != NULL); |
---|
[7426] | 185 | |
---|
| 186 | // unloading the Font if we alrady have one loaded. |
---|
| 187 | this->font = newFont; |
---|
| 188 | if (oldFont != NULL && oldFont != Font::getDefaultFont()) |
---|
| 189 | ResourceManager::getInstance()->unload(oldFont); |
---|
[7450] | 190 | |
---|
| 191 | this->setupTextWidth(); |
---|
[5343] | 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
[7453] | 195 | * @brief sets the Size of the Font |
---|
| 196 | * @param size :the size of the Text |
---|
| 197 | */ |
---|
| 198 | void Text::setSize(float size) |
---|
| 199 | { |
---|
| 200 | this->size = size; |
---|
| 201 | this->setSizeY2D(size); |
---|
| 202 | this->setupTextWidth(); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | /** |
---|
[7355] | 207 | * @brief draws the Text |
---|
[5343] | 208 | */ |
---|
| 209 | void Text::draw() const |
---|
| 210 | { |
---|
[7448] | 211 | if (unlikely(this->text.empty())) |
---|
| 212 | return; |
---|
[5343] | 213 | glPushMatrix(); |
---|
[7919] | 214 | glPushAttrib(GL_ENABLE_BIT); |
---|
[5343] | 215 | // transform for alignment. |
---|
| 216 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
[5767] | 217 | glTranslatef(-this->getSizeX2D(), 0, 0); |
---|
[5343] | 218 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
[5767] | 219 | glTranslatef(-this->getSizeX2D()/2, 0, 0); |
---|
[5343] | 220 | |
---|
| 221 | // drawing this Text. |
---|
| 222 | // setting the Blending effects |
---|
[8448] | 223 | glColor4fv(&this->color[0]); |
---|
[8037] | 224 | |
---|
| 225 | |
---|
| 226 | glActiveTexture(GL_TEXTURE0); |
---|
| 227 | |
---|
[5343] | 228 | glEnable(GL_BLEND); |
---|
| 229 | glEnable(GL_TEXTURE_2D); |
---|
[8448] | 230 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
[5343] | 231 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); |
---|
| 232 | |
---|
[7448] | 233 | glBindTexture(GL_TEXTURE_2D, font->getTexture()); |
---|
| 234 | glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); |
---|
| 235 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
| 236 | |
---|
| 237 | Glyph* tmpGlyph; |
---|
| 238 | float posX = 0.0f; |
---|
| 239 | glBegin(GL_QUADS); |
---|
| 240 | for (unsigned int i = 0; i < this->text.size(); i++) |
---|
[5343] | 241 | { |
---|
[7450] | 242 | if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL)) |
---|
[5343] | 243 | { |
---|
[7448] | 244 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
[7450] | 245 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0); |
---|
[5419] | 246 | |
---|
[7448] | 247 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
[7450] | 248 | glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize()); |
---|
[5419] | 249 | |
---|
[7448] | 250 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
[7450] | 251 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize()); |
---|
[5419] | 252 | |
---|
[7448] | 253 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
[7450] | 254 | glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0); |
---|
[5419] | 255 | |
---|
[7450] | 256 | posX += tmpGlyph->advance * this->getSize(); |
---|
[5343] | 257 | } |
---|
| 258 | } |
---|
[7448] | 259 | glEnd(); |
---|
[7919] | 260 | glPopAttrib(); |
---|
[5343] | 261 | glPopMatrix(); |
---|
| 262 | } |
---|
| 263 | |
---|
[7450] | 264 | |
---|
[5343] | 265 | /** |
---|
[7450] | 266 | * @brief setting up the Text-Width. |
---|
[5343] | 267 | */ |
---|
[7450] | 268 | void Text::setupTextWidth() |
---|
[5343] | 269 | { |
---|
[7450] | 270 | float width = 0; |
---|
| 271 | for (unsigned int i = 0; i < this->text.size(); i++) |
---|
| 272 | if(this->font->getGlyphArray()[this->text[i]] != NULL) |
---|
| 273 | width += this->font->getGlyphArray()[this->text[i]]->advance; |
---|
[7919] | 274 | this->setSizeX2D(width * this->getSize()); |
---|
[5343] | 275 | } |
---|
| 276 | |
---|
| 277 | |
---|
| 278 | /** |
---|
[7450] | 279 | * @brief prints out some nice debug information about this text |
---|
[5343] | 280 | */ |
---|
[7450] | 281 | void Text::debug() const |
---|
[5343] | 282 | { |
---|
[7450] | 283 | PRINT(0)("=== TEXT: %s (with Font:'%s') displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str()); |
---|
[8448] | 284 | PRINT(0)("Color: r=%0.2f g=%0.2f b=%0.2f a=%0.2f\n", this->color.r(), this->color.g(), this->color.b(), this->color.a()); |
---|
[5343] | 285 | } |
---|
| 286 | |
---|