Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: added MultiLineText a Text for multiple line-input, that should automatically shilft position

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