Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged gui back to the trunk
merged with command
merge -r8377:HEAD https://svn.orxonox.net/orxonox/branches/gui .

File size: 7.0 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 sets the Font of this Text to font from fontFile
164 * @param fontFile the File to load the Font from.
165 * @param fontSize the Size of the Font
166 */
167void Text::setFont(const std::string& fontFile, unsigned int fontSize)
168{
169  Font* newFont = NULL;
170  Font* oldFont = this->font;
171
172  // load a new Font
173  if (!fontFile.empty())
174  {
175    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
176    if (newFont == NULL)
177    {
178      newFont = Font::getDefaultFont();
179      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
180    }
181  }
182  if (newFont == NULL)
183    newFont = Font::getDefaultFont();
184  assert(newFont != NULL);
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);
190
191  this->setupTextWidth();
192}
193
194/**
195 * @brief sets the Size of the Font
196 * @param size :the size of the Text
197 */
198void Text::setSize(float size)
199{
200  this->size = size;
201  this->setSizeY2D(size);
202  this->setupTextWidth();
203}
204
205
206/**
207 * @brief draws the Text
208 */
209void Text::draw() const
210{
211  if (unlikely(this->text.empty()))
212    return;
213  glPushMatrix();
214  glPushAttrib(GL_ENABLE_BIT);
215  // transform for alignment.
216  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
217    glTranslatef(-this->getSizeX2D(), 0, 0);
218  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
219    glTranslatef(-this->getSizeX2D()/2, 0, 0);
220
221  // drawing this Text.
222  // setting the Blending effects
223  glColor4fv(&this->color[0]);
224
225
226  glActiveTexture(GL_TEXTURE0);
227
228  glEnable(GL_BLEND);
229  glEnable(GL_TEXTURE_2D);
230  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
231  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
232
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++)
241  {
242    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
243    {
244      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
245      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
246
247      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
248      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
249
250      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
251      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
252
253      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
254      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
255
256      posX += tmpGlyph->advance * this->getSize();
257    }
258  }
259  glEnd();
260  glPopAttrib();
261  glPopMatrix();
262}
263
264
265/**
266 * @brief setting up the Text-Width.
267 */
268void Text::setupTextWidth()
269{
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;
274  this->setSizeX2D(width * this->getSize());
275}
276
277
278/**
279 * @brief prints out some nice debug information about this text
280 */
281void Text::debug() const
282{
283  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str());
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());
285}
286
Note: See TracBrowser for help on using the repository browser.