Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/lib/graphics/text_engine/text.cc @ 7893

Last change on this file since 7893 was 7893, checked in by bensch, 19 years ago

gui: InputLine appends

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