Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more thoughts on Resources, and soon going to adapt… i think i've got a clue :)

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