Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8256 was 8037, checked in by bensch, 19 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
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->blending = TEXT_DEFAULT_BLENDING;
40  this->color = TEXT_DEFAULT_COLOR;
41
42  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
43
44  this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
45}
46
47Text::Text(const Text& text)
48{
49  this->setClassID(CL_TEXT, "Text");
50  this->font = NULL;
51
52  *this = text;
53}
54
55
56/**
57 * @brief deletes a Text out of memory
58 */
59Text::~Text()
60{
61  if (this->font != NULL && this->font != Font::getDefaultFont())
62    ResourceManager::getInstance()->unload(this->font);
63}
64
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}
78
79/**
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/**
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/**
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/**
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/**
166 * @brief sets the Font of this Text to font from fontFile
167 * @param fontFile the File to load the Font from.
168 * @param fontSize the Size of the Font
169 */
170void Text::setFont(const std::string& fontFile, unsigned int fontSize)
171{
172  Font* newFont = NULL;
173  Font* oldFont = this->font;
174
175  // load a new Font
176  if (!fontFile.empty())
177  {
178    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
179    if (newFont == NULL)
180    {
181      newFont = Font::getDefaultFont();
182      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
183    }
184  }
185  if (newFont == NULL)
186    newFont = Font::getDefaultFont();
187  assert(newFont != NULL);
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);
193
194  this->setupTextWidth();
195}
196
197/**
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/**
210 * @brief draws the Text
211 */
212void Text::draw() const
213{
214  if (unlikely(this->text.empty()))
215    return;
216  glPushMatrix();
217  glPushAttrib(GL_ENABLE_BIT);
218  // transform for alignment.
219  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
220    glTranslatef(-this->getSizeX2D(), 0, 0);
221  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
222    glTranslatef(-this->getSizeX2D()/2, 0, 0);
223
224  // drawing this Text.
225  // setting the Blending effects
226  glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
227
228
229  glActiveTexture(GL_TEXTURE0);
230
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
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++)
244  {
245    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
246    {
247      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
248      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
249
250      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
251      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
252
253      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
254      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
255
256      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
257      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
258
259      posX += tmpGlyph->advance * this->getSize();
260    }
261  }
262  glEnd();
263  glPopAttrib();
264  glPopMatrix();
265}
266
267
268/**
269 * @brief setting up the Text-Width.
270 */
271void Text::setupTextWidth()
272{
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;
277  this->setSizeX2D(width * this->getSize());
278}
279
280
281/**
282 * @brief prints out some nice debug information about this text
283 */
284void Text::debug() const
285{
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);
288}
289
Note: See TracBrowser for help on using the repository browser.