Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

load-implementation of font in font-data

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