Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

File size: 6.4 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5343]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]17
[5343]18#include "text.h"
19#include "font.h"
[1853]20
[7193]21#include "util/loading/resource_manager.h"
[5343]22#include "debug.h"
23
[1856]24using namespace std;
[1853]25
[5343]26/**
[7355]27 * @brief creates a new Text Element
[5343]28 * @param fontFile the Font to render this text in
29 * @param type The renderType to display this font in
30 */
[7221]31Text::Text(const std::string& fontFile, unsigned int textSize)
[5343]32{
[7355]33  this->setClassID(CL_TEXT, "Text");
[1856]34
[7355]35  // initialize this Text
36  this->font = NULL;
[7726]37  this->size = textSize;
[7742]38  this->blending = TEXT_DEFAULT_BLENDING;
39  this->color = TEXT_DEFAULT_COLOR;
[7455]40
[7753]41  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
42
[7455]43  this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
[7753]44}
[7455]45
[7753]46Text::Text(const Text& text)
47{
48  this->setClassID(CL_TEXT, "Text");
49  this->font = NULL;
50
51  *this = text;
[5343]52}
53
[7753]54
[3245]55/**
[7355]56 * @brief deletes a Text out of memory
[5343]57 */
58Text::~Text()
59{
[5767]60  if (this->font != NULL && this->font != Font::getDefaultFont())
[5343]61    ResourceManager::getInstance()->unload(this->font);
[3365]62}
[1853]63
[7753]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}
[1853]77
[3245]78/**
[7753]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/**
[7355]139 * @brief sets the Font of this Text to font from fontFile
[5343]140 * @param fontFile the File to load the Font from.
141 * @param fontSize the Size of the Font
142 */
[7221]143void Text::setFont(const std::string& fontFile, unsigned int fontSize)
[5343]144{
[7455]145  Font* newFont = NULL;
[7426]146  Font* oldFont = this->font;
[5343]147
[5345]148  // load a new Font
[7221]149  if (!fontFile.empty())
[5344]150  {
[7426]151    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
152    if (newFont == NULL)
153    {
154      newFont = Font::getDefaultFont();
[7221]155      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
[7426]156    }
[5343]157  }
[7455]158  if (newFont == NULL)
[7426]159    newFont = Font::getDefaultFont();
[7455]160  assert(newFont != NULL);
[7426]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);
[7450]166
167  this->setupTextWidth();
[5343]168}
169
170/**
[7453]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/**
[7355]183 * @brief draws the Text
[5343]184 */
185void Text::draw() const
186{
[7448]187  if (unlikely(this->text.empty()))
188    return;
[5343]189  glPushMatrix();
190  // transform for alignment.
191  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
[5767]192    glTranslatef(-this->getSizeX2D(), 0, 0);
[5343]193  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
[5767]194    glTranslatef(-this->getSizeX2D()/2, 0, 0);
[5343]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
[7448]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++)
[5343]212  {
[7450]213    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
[5343]214    {
[7448]215      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
[7450]216      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
[5419]217
[7448]218      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
[7450]219      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
[5419]220
[7448]221      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
[7450]222      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
[5419]223
[7448]224      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
[7450]225      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
[5419]226
[7450]227      posX += tmpGlyph->advance * this->getSize();
[5343]228    }
229  }
[7448]230  glEnd();
[5343]231  glPopMatrix();
232}
233
[7450]234
[5343]235/**
[7450]236 * @brief setting up the Text-Width.
[5343]237 */
[7450]238void Text::setupTextWidth()
[5343]239{
[7450]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());
[5343]245}
246
247
248/**
[7450]249 * @brief prints out some nice debug information about this text
[5343]250 */
[7450]251void Text::debug() const
[5343]252{
[7450]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);
[5343]255}
256
Note: See TracBrowser for help on using the repository browser.