Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7422 was 7355, checked in by bensch, 19 years ago

orxonox/trunk: works on windows too

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