Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/text_engine/text.cc @ 8082

Last change on this file since 8082 was 8037, checked in by bensch, 18 years ago

trunk: merged the water back
merged with command
svn merge -r7798:HEAD https://svn.orxonox.net/orxonox/branches/water .

conflicts are all resolved

File size: 7.1 KB
RevLine 
[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]24using 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]31Text::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]47Text::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 */
59Text::~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 */
70bool 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 */
84bool 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 */
94Text& 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 */
113void 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 */
123void 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 */
132void 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 */
143const 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 */
155void 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]170void 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 */
201void 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 */
212void 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);
[8037]227
228
229  glActiveTexture(GL_TEXTURE0);
230
[5343]231  glEnable(GL_BLEND);
232  glEnable(GL_TEXTURE_2D);
233  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
234  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
235
[7448]236  glBindTexture(GL_TEXTURE_2D, font->getTexture());
237  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
238  glRotatef(this->getAbsDir2D(), 0, 0, 1);
239
240  Glyph* tmpGlyph;
241  float posX = 0.0f;
242  glBegin(GL_QUADS);
243  for (unsigned int i = 0; i < this->text.size(); i++)
[5343]244  {
[7450]245    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
[5343]246    {
[7448]247      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
[7450]248      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
[5419]249
[7448]250      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
[7450]251      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
[5419]252
[7448]253      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
[7450]254      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
[5419]255
[7448]256      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
[7450]257      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
[5419]258
[7450]259      posX += tmpGlyph->advance * this->getSize();
[5343]260    }
261  }
[7448]262  glEnd();
[7919]263  glPopAttrib();
[5343]264  glPopMatrix();
265}
266
[7450]267
[5343]268/**
[7450]269 * @brief setting up the Text-Width.
[5343]270 */
[7450]271void Text::setupTextWidth()
[5343]272{
[7450]273  float width = 0;
274  for (unsigned int i = 0; i < this->text.size(); i++)
275    if(this->font->getGlyphArray()[this->text[i]] != NULL)
276      width += this->font->getGlyphArray()[this->text[i]]->advance;
[7919]277  this->setSizeX2D(width * this->getSize());
[5343]278}
279
280
281/**
[7450]282 * @brief prints out some nice debug information about this text
[5343]283 */
[7450]284void Text::debug() const
[5343]285{
[7450]286  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str());
287  PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
[5343]288}
289
Note: See TracBrowser for help on using the repository browser.