Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the gui-branche back.
merged with command:
svn merge -r8520:HEAD https://svn.orxonox.net/orxonox/branches/gui
no conflicts

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