Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the gui back to the trunk

File size: 7.1 KB
Line 
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
24using 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 */
31Text::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->color = TEXT_DEFAULT_COLOR;
40
41  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
42
43  this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
44}
45
46Text::Text(const Text& text)
47{
48  this->setClassID(CL_TEXT, "Text");
49  this->font = NULL;
50
51  *this = text;
52}
53
54
55/**
56 * @brief deletes a Text out of memory
57 */
58Text::~Text()
59{
60  if (this->font != NULL && this->font != Font::getDefaultFont())
61    ResourceManager::getInstance()->unload(this->font);
62}
63
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 &&
74          this->color == text.color);
75}
76
77/**
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/**
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/**
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/**
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/**
163 * @brief clears the Text Line (empies it).
164 */
165void Text::clear()
166{
167  text.clear();
168  this->setupTextWidth();
169}
170
171/**
172 * @brief sets the Font of this Text to font from fontFile
173 * @param fontFile the File to load the Font from.
174 * @param fontSize the Size of the Font
175 */
176void Text::setFont(const std::string& fontFile, unsigned int fontSize)
177{
178  Font* newFont = NULL;
179  Font* oldFont = this->font;
180
181  // load a new Font
182  if (!fontFile.empty())
183  {
184    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
185    if (newFont == NULL)
186    {
187      newFont = Font::getDefaultFont();
188      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
189    }
190  }
191  if (newFont == NULL)
192    newFont = Font::getDefaultFont();
193  assert(newFont != NULL);
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);
199
200  this->setupTextWidth();
201}
202
203/**
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/**
216 * @brief draws the Text
217 */
218void Text::draw() const
219{
220  if (unlikely(this->text.empty()))
221    return;
222  glPushMatrix();
223  glPushAttrib(GL_ENABLE_BIT);
224  // transform for alignment.
225  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
226    glTranslatef(-this->getSizeX2D(), 0, 0);
227  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
228    glTranslatef(-this->getSizeX2D()/2, 0, 0);
229
230  // drawing this Text.
231  // setting the Blending effects
232  glColor4fv(&this->color[0]);
233
234
235  glActiveTexture(GL_TEXTURE0);
236
237  glEnable(GL_BLEND);
238  glEnable(GL_TEXTURE_2D);
239  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
240  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
241
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++)
250  {
251    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
252    {
253      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
254      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
255
256      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
257      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
258
259      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
260      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
261
262      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
263      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
264
265      posX += tmpGlyph->advance * this->getSize();
266    }
267  }
268  glEnd();
269  glPopAttrib();
270  glPopMatrix();
271}
272
273
274/**
275 * @brief setting up the Text-Width.
276 */
277void Text::setupTextWidth()
278{
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;
283  this->setSizeX2D(width * this->getSize());
284}
285
286
287/**
288 * @brief prints out some nice debug information about this text
289 */
290void Text::debug() const
291{
292  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str());
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());
294}
295
Note: See TracBrowser for help on using the repository browser.