Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7615 was 7455, checked in by bensch, 19 years ago

orxonox/trunk: fixed a SegFault (that this worked before is some wonder :)

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