Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/text_engine/text.cc @ 9833

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

orxonox/new_class_id: almost killed off the old ResourceManager

File size: 6.7 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#include "loading/resource_manager.h"
21#include "debug.h"
22
23ObjectListDefinition(Text);
24
25/**
26 * @brief creates a new Text Element
27 * @param fontFile the Font to render this text in
28 * @param type The renderType to display this font in
29 */
30Text::Text(const std::string& fontFile, unsigned int textSize)
31    // : _font(fontFile, FONT_DEFAULT_RENDER_SIZE)
32{
33  this->registerObject(this, Text::_objectList);
34
35  // initialize this Text
36  this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
37  this->_size = textSize;
38  this->setSizeY2D(textSize);
39  this->setColor(TEXT_DEFAULT_COLOR);
40
41  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
42}
43
44Text::Text(const Text& text)
45    : _font()
46{
47  this->registerObject(this, Text::_objectList);
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}
73
74/**
75 * @brief compare this Text's internal String with the text.
76 * @param text the Comparator Text.
77 * @returns true on a match.
78 */
79bool Text::operator==(const std::string& text) const
80{
81  return (this->_text == text);
82}
83
84/**
85 * @brief Copies the properties of one text onto the other one.
86 * @param text: the Text to apply to this one.
87 * @returns This-reference.
88 */
89Text& Text::operator=(const Text& text)
90{
91  this->_size = text._size;
92  this->setAlignment(text.getAlignment());
93
94  this->_font = text._font;
95
96  this->_text = text._text;
97  return *this;
98}
99
100/**
101 * @brief Sets a new Text to the font
102 * @param text the new text to set
103 */
104void Text::setText(const std::string& text)
105{
106  this->_text = text;
107  this->setupTextWidth();
108}
109
110/**
111 * @brief append some text to the already existing Text.
112 * @param appendText The text to append to this Text.
113 */
114void Text::append(const std::string& appendText)
115{
116  this->_text += appendText;
117  this->setupTextWidth();
118}
119
120/**
121 * @brief appends one Character to the String.
122 */
123void Text::appendCharacter(char character)
124{
125  this->_text += character;
126  this->setupTextWidth();
127}
128
129
130/**
131 * @brief append some text to the already existing Text.
132 * @param appendText The text to append to this Text.
133 */
134const std::string& Text::operator <<(const std::string& appendText)
135{
136  this->append(appendText);
137  return this->_text;
138}
139
140/**
141 * @brief removes char characters from the Text.
142 *
143 * @note this function checks, if the count can be removed, and if so does it.
144 * Otherwise the maximum count of characters will be removed.
145 */
146void Text::removeCharacters(unsigned int chars)
147{
148  if (this->_text.size() > chars)
149    this->_text.resize(this->_text.size()-chars);
150  else if (!this->_text.empty())
151    this->_text.clear();
152  this->setupTextWidth();
153}
154
155
156/**
157 * @brief clears the Text Line (empies it).
158 */
159void Text::clear()
160{
161  this->_text.clear();
162  this->setupTextWidth();
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::();
182      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
183    }
184  }
185
186  if (newFont == NULL)
187    this->_font = Font();
188  else
189    this->_font = *newFont;
190
191  this->setupTextWidth();
192}
193
194/**
195 * @brief set a new Font to this Text.
196 * @param font the Font to set.
197 */
198void Text::setFont(const Font& font)
199{
200  this->_font = font;
201  this->setupTextWidth();
202}
203
204/**
205 * @brief sets the Size of the Font
206 * @param size :the size of the Text
207 */
208void Text::setSize(float size)
209{
210  this->_size = size;
211  this->setSizeY2D(size);
212  this->setupTextWidth();
213}
214
215
216/**
217 * @brief draws the Text
218 */
219void Text::draw() const
220{
221  if (unlikely(this->_text.empty()))
222    return;
223  glPushMatrix();
224  glPushAttrib(GL_ENABLE_BIT);
225  // transform for alignment.
226  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
227    glTranslatef(-this->getSizeX2D(), 0, 0);
228  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
229    glTranslatef(-this->getSizeX2D()/2, 0, 0);
230
231
232  this->font().select();
233  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
234  glRotatef(this->getAbsDir2D(), 0, 0, 1);
235
236  const Font::Glyph* tmpGlyph;
237  float posX = 0.0f;
238  glBegin(GL_QUADS);
239  for (unsigned int i = 0; i < this->_text.size(); i++)
240  {
241    if(likely((tmpGlyph = this->font().getGlyphArray()[this->_text[i]]) != NULL))
242    {
243      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
244      glVertex2d(posX+tmpGlyph->maxX*this->size(), 0);
245
246      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
247      glVertex2d(posX+tmpGlyph->maxX*this->size(), this->size());
248
249      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
250      glVertex2d(posX+tmpGlyph->minX*this->size(), this->size());
251
252      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
253      glVertex2d(posX+tmpGlyph->minX*this->size(), 0);
254
255      posX += tmpGlyph->advance * this->size();
256    }
257  }
258  glEnd();
259  glPopAttrib();
260  glPopMatrix();
261}
262
263
264/**
265 * @brief setting up the Text-Width.
266 */
267void Text::setupTextWidth()
268{
269  float width = 0;
270  for (unsigned int i = 0; i < this->_text.size(); i++)
271    if(this->_font.getGlyphArray()[this->_text[i]] != NULL)
272      width += this->_font.getGlyphArray()[this->_text[i]]->advance;
273  this->setSizeX2D(width * this->size());
274}
275
276
277/**
278 * @brief prints out some nice debug information about this text
279 */
280void Text::debug() const
281{
282  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getCName(), this->_font.getCName(), this->_text.c_str());
283  //  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());
284}
285
Note: See TracBrowser for help on using the repository browser.