Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7449 was 7448, checked in by bensch, 19 years ago

orxonox/trunk: text-draw optimized

File size: 7.5 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;
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
[7221]45  if (!fontFile.empty())
[5369]46    this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
[5768]47  this->setSizeY2D(textSize);
[5343]48}
49
[3245]50/**
[7355]51 * @brief deletes a Text out of memory
[5343]52 */
53Text::~Text()
54{
[5767]55  if (this->font != NULL && this->font != Font::getDefaultFont())
[5343]56    ResourceManager::getInstance()->unload(this->font);
[3365]57}
[1853]58
59
[3245]60/**
[7355]61 * @brief sets the Font of this Text to font from fontFile
[5343]62 * @param fontFile the File to load the Font from.
63 * @param fontSize the Size of the Font
64 */
[7221]65void Text::setFont(const std::string& fontFile, unsigned int fontSize)
[5343]66{
[7426]67  Font* newFont;
68  Font* oldFont = this->font;
[5343]69
[5345]70  // load a new Font
[7221]71  if (!fontFile.empty())
[5344]72  {
[7426]73    newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
74    if (newFont == NULL)
75    {
76      newFont = Font::getDefaultFont();
[7221]77      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
[7426]78    }
[5343]79  }
[7426]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);
[5343]87}
88
89/**
[7355]90 * @brief Sets a new Text to the font
[5343]91 * @param text the new text to set
92 */
[7221]93void Text::setText(const std::string& text)
[5343]94{
[7221]95  this->text = text;
[5343]96
97  // setting up the Text-Width if DYNAMIC
[7448]98  //  if (this->type & TEXT_RENDER_DYNAMIC && this->getAlignment() != TEXT_ALIGN_LEFT && this->font != NULL)
[5418]99  const Font* calcSizeFont = this->font;
100  if (calcSizeFont != NULL || (calcSizeFont = Font::getDefaultFont()) != NULL)
[5343]101  {
[5418]102    Glyph** glyphArray = calcSizeFont->getGlyphArray();
[5343]103
[5368]104    float width = 0;
[7221]105    if (!this->text.empty())
[5343]106    {
[7221]107      for (unsigned int i = 0; i < this->text.size(); i++)
[5343]108      {
[7221]109        if(glyphArray[this->text[i]] != NULL)
[5343]110        {
[7221]111          width += glyphArray[this->text[i]]->advance;
[5343]112        }
113      }
[5767]114      this->setSizeX2D(width *this->getSizeY2D());
[5343]115    }
116  }
117}
118
119/**
[7355]120 * @brief draws the Text
[5343]121 */
122void Text::draw() const
123{
[7448]124  if (unlikely(this->text.empty()))
125    return;
[5343]126  glPushMatrix();
127  // transform for alignment.
128  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
[5767]129    glTranslatef(-this->getSizeX2D(), 0, 0);
[5343]130  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
[5767]131    glTranslatef(-this->getSizeX2D()/2, 0, 0);
[5343]132
133  // drawing this Text.
134  // setting the Blending effects
135  glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
136  glEnable(GL_BLEND);
137  glEnable(GL_TEXTURE_2D);
138  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
139  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
140
[7448]141  Glyph** glyphArray = this->font->getGlyphArray();
142  glBindTexture(GL_TEXTURE_2D, font->getTexture());
143  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
144  glRotatef(this->getAbsDir2D(), 0, 0, 1);
145
146  Glyph* tmpGlyph;
147  float posX = 0.0f;
148  glBegin(GL_QUADS);
149  for (unsigned int i = 0; i < this->text.size(); i++)
[5343]150  {
[7448]151    if(likely((tmpGlyph = glyphArray[this->text[i]]) != NULL))
[5343]152    {
[7448]153      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
154      glVertex2d(posX+tmpGlyph->maxX*this->getSizeY2D(), 0);
[5419]155
[7448]156      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
157      glVertex2d(posX+tmpGlyph->maxX*this->getSizeY2D(), this->getSizeY2D());
[5419]158
[7448]159      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
160      glVertex2d(posX+tmpGlyph->minX*this->getSizeY2D(), this->getSizeY2D());
[5419]161
[7448]162      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
163      glVertex2d(posX+tmpGlyph->minX*this->getSizeY2D(), 0);
[5419]164
[7448]165      posX += tmpGlyph->advance * this->getSizeY2D();
[5343]166    }
167  }
[7448]168  glEnd();
[5343]169  glPopMatrix();
170}
171
172/**
[7355]173 * @brief prints out some nice debug information about this text
[5343]174 */
175void Text::debug() const
176{
[7221]177  PRINT(0)("=== TEXT: %s ===\n", this->text.c_str());
[5343]178  PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
179}
180
181
182////////////
183/// UTIL ///
184////////////
185/**
[7355]186 * @brief Loads a Font from an SDL_surface into a texture.
[5343]187 * @param surface The surface to make the texture of
188 * @param texCoord The texture coordinates of the 4 corners of the texture
189 * @returns the ID of the texture
190 */
191GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord)
192{
193  GLuint texture;
194  int w, h;
195  SDL_Surface *image;
196  SDL_Rect area;
197  Uint32 saved_flags;
198  Uint8  saved_alpha;
199
200  /* Use the surface width and height expanded to powers of 2 */
201  w = powerOfTwo(surface->w);
202  h = powerOfTwo(surface->h);
203  if (texCoord != NULL)
204  {
205    texCoord->minU = 0.0f;
206    texCoord->minV = 0.0f;
207    texCoord->maxU = (GLfloat)surface->w / w;
208    texCoord->maxV = (GLfloat)surface->h / h;
209  }
210  image = SDL_CreateRGBSurface(SDL_SWSURFACE,
211                               w, h,
212                               32,
213#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
214                               0x000000FF,
215                               0x0000FF00,
216                               0x00FF0000,
217                               0xFF000000
218#else
[7448]219                               0xFF000000,
[5343]220                               0x00FF0000,
221                               0x0000FF00,
222                               0x000000FF
223#endif
224                              );
[7448]225  if ( image == NULL )
226  {
[5343]227    return 0;
228  }
229
230  /* Save the alpha blending attributes */
231  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
232  saved_alpha = surface->format->alpha;
[7448]233  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
234  {
[5343]235    SDL_SetAlpha(surface, 0, 0);
236  }
237
238  /* Copy the surface into the GL texture image */
239  area.x = 0;
240  area.y = 0;
241  area.w = surface->w;
242  area.h = surface->h;
243  SDL_BlitSurface(surface, &area, image, &area);
244
245  /* Restore the alpha blending attributes */
[7448]246  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
247  {
[5343]248    SDL_SetAlpha(surface, saved_flags, saved_alpha);
249  }
250
251  /* Create an OpenGL texture for the image */
252  glGenTextures(1, &texture);
253  glBindTexture(GL_TEXTURE_2D, texture);
[5368]254  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
[5343]255  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
256  glTexImage2D(GL_TEXTURE_2D,
257               0,
258               GL_RGBA,
259               w, h,
260               0,
261               GL_RGBA,
262               GL_UNSIGNED_BYTE,
263               image->pixels);
264  SDL_FreeSurface(image); /* No longer needed the data */
265
266  return texture;
267}
268
269/**
[7355]270 * @brief Quick utility function for texture creation
[5343]271 * @param input an integer
272 * @returns the next bigger 2^n-integer than input
273 */
274int Text::powerOfTwo(int input)
275{
276  int value = 1;
277
278  while ( value < input )
279    value <<= 1;
280  return value;
281}
Note: See TracBrowser for help on using the repository browser.