Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7740 was 7726, checked in by bensch, 19 years ago

trunk: minor valgrind bug fixed

File size: 4.8 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
39  this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
40
41  this->blending = TEXT_DEFAULT_BLENDING;
42  this->color = TEXT_DEFAULT_COLOR;
43  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
44  this->setSize(TEXT_DEFAULT_SIZE);
45}
46
47/**
48 * @brief deletes a Text out of memory
49 */
50Text::~Text()
51{
52  if (this->font != NULL && this->font != Font::getDefaultFont())
53    ResourceManager::getInstance()->unload(this->font);
54}
55
56
57/**
58 * @brief sets the Font of this Text to font from fontFile
59 * @param fontFile the File to load the Font from.
60 * @param fontSize the Size of the Font
61 */
62void Text::setFont(const std::string& fontFile, unsigned int fontSize)
63{
64  Font* newFont = NULL;
65  Font* oldFont = this->font;
66
67  // load a new Font
68  if (!fontFile.empty())
69  {
70    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
71    if (newFont == NULL)
72    {
73      newFont = Font::getDefaultFont();
74      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
75    }
76  }
77  if (newFont == NULL)
78    newFont = Font::getDefaultFont();
79  assert(newFont != NULL);
80
81  // unloading the Font if we alrady have one loaded.
82  this->font = newFont;
83  if (oldFont != NULL && oldFont != Font::getDefaultFont())
84    ResourceManager::getInstance()->unload(oldFont);
85
86  this->setupTextWidth();
87}
88
89/**
90 * @brief Sets a new Text to the font
91 * @param text the new text to set
92 */
93void Text::setText(const std::string& text)
94{
95  this->text = text;
96
97  this->setupTextWidth();
98}
99
100
101/**
102 * @brief sets the Size of the Font
103 * @param size :the size of the Text
104 */
105void Text::setSize(float size)
106{
107  this->size = size;
108  this->setSizeY2D(size);
109  this->setupTextWidth();
110}
111
112
113/**
114 * @brief draws the Text
115 */
116void Text::draw() const
117{
118  if (unlikely(this->text.empty()))
119    return;
120  glPushMatrix();
121  // transform for alignment.
122  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
123    glTranslatef(-this->getSizeX2D(), 0, 0);
124  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
125    glTranslatef(-this->getSizeX2D()/2, 0, 0);
126
127  // drawing this Text.
128  // setting the Blending effects
129  glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
130  glEnable(GL_BLEND);
131  glEnable(GL_TEXTURE_2D);
132  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
133  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
134
135  glBindTexture(GL_TEXTURE_2D, font->getTexture());
136  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
137  glRotatef(this->getAbsDir2D(), 0, 0, 1);
138
139  Glyph* tmpGlyph;
140  float posX = 0.0f;
141  glBegin(GL_QUADS);
142  for (unsigned int i = 0; i < this->text.size(); i++)
143  {
144    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
145    {
146      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
147      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
148
149      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
150      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
151
152      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
153      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
154
155      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
156      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
157
158      posX += tmpGlyph->advance * this->getSize();
159    }
160  }
161  glEnd();
162  glPopMatrix();
163}
164
165
166/**
167 * @brief setting up the Text-Width.
168 */
169void Text::setupTextWidth()
170{
171  float width = 0;
172  for (unsigned int i = 0; i < this->text.size(); i++)
173    if(this->font->getGlyphArray()[this->text[i]] != NULL)
174      width += this->font->getGlyphArray()[this->text[i]]->advance;
175  this->setSizeX2D(width *this->getSize());
176}
177
178
179/**
180 * @brief prints out some nice debug information about this text
181 */
182void Text::debug() const
183{
184  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str());
185  PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
186}
187
Note: See TracBrowser for help on using the repository browser.