Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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