Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8520 was 8518, checked in by bensch, 18 years ago

merged the gui back to the trunk

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->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]46Text::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 */
58Text::~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 */
69bool 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 */
82bool 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 */
92Text& 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 */
110void 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 */
120void 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 */
129void 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 */
140const 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 */
152void 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/**
[8518]163 * @brief clears the Text Line (empies it).
164 */
165void Text::clear()
166{
167  text.clear();
168  this->setupTextWidth();
169}
170
171/**
[7355]172 * @brief sets the Font of this Text to font from fontFile
[5343]173 * @param fontFile the File to load the Font from.
174 * @param fontSize the Size of the Font
175 */
[7221]176void Text::setFont(const std::string& fontFile, unsigned int fontSize)
[5343]177{
[7455]178  Font* newFont = NULL;
[7426]179  Font* oldFont = this->font;
[5343]180
[5345]181  // load a new Font
[7221]182  if (!fontFile.empty())
[5344]183  {
[7426]184    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
185    if (newFont == NULL)
186    {
187      newFont = Font::getDefaultFont();
[7221]188      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
[7426]189    }
[5343]190  }
[7455]191  if (newFont == NULL)
[7426]192    newFont = Font::getDefaultFont();
[7455]193  assert(newFont != NULL);
[7426]194
195  // unloading the Font if we alrady have one loaded.
196  this->font = newFont;
197  if (oldFont != NULL && oldFont != Font::getDefaultFont())
198    ResourceManager::getInstance()->unload(oldFont);
[7450]199
200  this->setupTextWidth();
[5343]201}
202
203/**
[7453]204 * @brief sets the Size of the Font
205 * @param size :the size of the Text
206 */
207void Text::setSize(float size)
208{
209  this->size = size;
210  this->setSizeY2D(size);
211  this->setupTextWidth();
212}
213
214
215/**
[7355]216 * @brief draws the Text
[5343]217 */
218void Text::draw() const
219{
[7448]220  if (unlikely(this->text.empty()))
221    return;
[5343]222  glPushMatrix();
[7919]223  glPushAttrib(GL_ENABLE_BIT);
[5343]224  // transform for alignment.
225  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
[5767]226    glTranslatef(-this->getSizeX2D(), 0, 0);
[5343]227  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
[5767]228    glTranslatef(-this->getSizeX2D()/2, 0, 0);
[5343]229
230  // drawing this Text.
231  // setting the Blending effects
[8448]232  glColor4fv(&this->color[0]);
[8037]233
234
235  glActiveTexture(GL_TEXTURE0);
236
[5343]237  glEnable(GL_BLEND);
238  glEnable(GL_TEXTURE_2D);
[8448]239  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
[5343]240  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
241
[7448]242  glBindTexture(GL_TEXTURE_2D, font->getTexture());
243  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
244  glRotatef(this->getAbsDir2D(), 0, 0, 1);
245
246  Glyph* tmpGlyph;
247  float posX = 0.0f;
248  glBegin(GL_QUADS);
249  for (unsigned int i = 0; i < this->text.size(); i++)
[5343]250  {
[7450]251    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
[5343]252    {
[7448]253      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
[7450]254      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
[5419]255
[7448]256      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
[7450]257      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
[5419]258
[7448]259      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
[7450]260      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
[5419]261
[7448]262      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
[7450]263      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
[5419]264
[7450]265      posX += tmpGlyph->advance * this->getSize();
[5343]266    }
267  }
[7448]268  glEnd();
[7919]269  glPopAttrib();
[5343]270  glPopMatrix();
271}
272
[7450]273
[5343]274/**
[7450]275 * @brief setting up the Text-Width.
[5343]276 */
[7450]277void Text::setupTextWidth()
[5343]278{
[7450]279  float width = 0;
280  for (unsigned int i = 0; i < this->text.size(); i++)
281    if(this->font->getGlyphArray()[this->text[i]] != NULL)
282      width += this->font->getGlyphArray()[this->text[i]]->advance;
[7919]283  this->setSizeX2D(width * this->getSize());
[5343]284}
285
286
287/**
[7450]288 * @brief prints out some nice debug information about this text
[5343]289 */
[7450]290void Text::debug() const
[5343]291{
[7450]292  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str());
[8448]293  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]294}
295
Note: See TracBrowser for help on using the repository browser.