Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: removed GTK, and also extended the Text Class.

File size: 6.4 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->blending = TEXT_DEFAULT_BLENDING;
39  this->color = TEXT_DEFAULT_COLOR;
40
41  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
42
43  this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
44}
45
46Text::Text(const Text& text)
47{
48  this->setClassID(CL_TEXT, "Text");
49  this->font = NULL;
50
51  *this = text;
52}
53
54
55/**
56 * @brief deletes a Text out of memory
57 */
58Text::~Text()
59{
60  if (this->font != NULL && this->font != Font::getDefaultFont())
61    ResourceManager::getInstance()->unload(this->font);
62}
63
64/**
65 * @brief compare the Text with another Text.
66 * @param text the Text to compare.
67 * @returns true if all the properties Match.
68 */
69bool Text::operator==(const Text& text) const
70{
71  return (this->text == text.text &&
72          this->size == text.size &&
73          this->font == text.font &&
74          this->color == text.color &&
75          this->blending == text.blending);
76}
77
78/**
79 * @brief compare this Text's internal String with the text.
80 * @param text the Comparator Text.
81 * @returns true on a match.
82 */
83bool Text::operator==(const std::string& text) const
84{
85  return (this->text == text);
86}
87
88/**
89 * @brief Copies the properties of one text onto the other one.
90 * @param text: the Text to apply to this one.
91 * @returns This-reference.
92 */
93Text& Text::operator=(const Text& text)
94{
95  this->size = text.size;
96  this->blending = text.blending;
97  this->color = text.color;
98  this->setAlignment(text.getAlignment());
99  if (this->font != NULL)
100    ResourceManager::getInstance()->unload(this->font);
101
102  this->font = (Font*)ResourceManager::getInstance()->copy( text.font ); //!< HACK
103
104  this->text = text.text;
105  return *this;
106}
107
108/**
109 * @brief Sets a new Text to the font
110 * @param text the new text to set
111 */
112void Text::setText(const std::string& text)
113{
114  this->text = text;
115  this->setupTextWidth();
116}
117
118/**
119 * @brief append some text to the already existing Text.
120 * @param appendText The text to append to this Text.
121 */
122void Text::append(const std::string& appendText)
123{
124  this->text += appendText;
125  this->setupTextWidth();
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 sets the Font of this Text to font from fontFile
140 * @param fontFile the File to load the Font from.
141 * @param fontSize the Size of the Font
142 */
143void Text::setFont(const std::string& fontFile, unsigned int fontSize)
144{
145  Font* newFont = NULL;
146  Font* oldFont = this->font;
147
148  // load a new Font
149  if (!fontFile.empty())
150  {
151    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
152    if (newFont == NULL)
153    {
154      newFont = Font::getDefaultFont();
155      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
156    }
157  }
158  if (newFont == NULL)
159    newFont = Font::getDefaultFont();
160  assert(newFont != NULL);
161
162  // unloading the Font if we alrady have one loaded.
163  this->font = newFont;
164  if (oldFont != NULL && oldFont != Font::getDefaultFont())
165    ResourceManager::getInstance()->unload(oldFont);
166
167  this->setupTextWidth();
168}
169
170/**
171 * @brief sets the Size of the Font
172 * @param size :the size of the Text
173 */
174void Text::setSize(float size)
175{
176  this->size = size;
177  this->setSizeY2D(size);
178  this->setupTextWidth();
179}
180
181
182/**
183 * @brief draws the Text
184 */
185void Text::draw() const
186{
187  if (unlikely(this->text.empty()))
188    return;
189  glPushMatrix();
190  // transform for alignment.
191  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
192    glTranslatef(-this->getSizeX2D(), 0, 0);
193  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
194    glTranslatef(-this->getSizeX2D()/2, 0, 0);
195
196  // drawing this Text.
197  // setting the Blending effects
198  glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
199  glEnable(GL_BLEND);
200  glEnable(GL_TEXTURE_2D);
201  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
202  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
203
204  glBindTexture(GL_TEXTURE_2D, font->getTexture());
205  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
206  glRotatef(this->getAbsDir2D(), 0, 0, 1);
207
208  Glyph* tmpGlyph;
209  float posX = 0.0f;
210  glBegin(GL_QUADS);
211  for (unsigned int i = 0; i < this->text.size(); i++)
212  {
213    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
214    {
215      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
216      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
217
218      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
219      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
220
221      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
222      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
223
224      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
225      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
226
227      posX += tmpGlyph->advance * this->getSize();
228    }
229  }
230  glEnd();
231  glPopMatrix();
232}
233
234
235/**
236 * @brief setting up the Text-Width.
237 */
238void Text::setupTextWidth()
239{
240  float width = 0;
241  for (unsigned int i = 0; i < this->text.size(); i++)
242    if(this->font->getGlyphArray()[this->text[i]] != NULL)
243      width += this->font->getGlyphArray()[this->text[i]]->advance;
244  this->setSizeX2D(width *this->getSize());
245}
246
247
248/**
249 * @brief prints out some nice debug information about this text
250 */
251void Text::debug() const
252{
253  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str());
254  PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
255}
256
Note: See TracBrowser for help on using the repository browser.