Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/collision_detection/src/lib/graphics/text_engine/text.cc @ 6177

Last change on this file since 6177 was 5515, checked in by bensch, 19 years ago

orxonox/trunk: unloading of fonts now without a Warning from the ResourceManager

File size: 10.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 "graphics_engine.h"
22#include "resource_manager.h"
23#include "class_list.h"
24#include "debug.h"
25#include "p_node.h"
26
27using namespace std;
28
29/**
30 *  creates a new Text Element
31 * @param fontFile the Font to render this text in
32 * @param type The renderType to display this font in
33 */
34Text::Text(const char* fontFile, unsigned int textSize, TEXT_RENDER_TYPE type)
35{
36  this->init();
37
38  if (fontFile != NULL)
39    this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
40  this->setType(type);
41  this->setSizeY2D(this->size = textSize);
42}
43
44/**
45 *  deletes a Text out of memory
46 *
47 * This also ereases the text from the textList of the TextEngine
48 */
49Text::~Text()
50{
51  if (this->font != NULL && this->font != Font::defaultFont)
52    ResourceManager::getInstance()->unload(this->font);
53
54  if (this->text)
55    delete[] this->text;
56}
57
58/**
59 * initializes Text
60 */
61void Text::init()
62{
63  this->setClassID(CL_TEXT, "Text");
64
65  // initialize this Text
66  this->font = NULL;
67  this->text = NULL;
68  this->externText = NULL;
69  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
70  this->texture = 0;
71  this->blending = TEXT_DEFAULT_BLENDING;
72  this->color = TEXT_DEFAULT_COLOR;
73  this->size = TEXT_DEFAULT_SIZE;
74  this->setType(TEXT_RENDER_DYNAMIC);
75
76  this->setText(NULL);
77}
78
79/**
80 * sets the Font of this Text to font from fontFile
81 * @param fontFile the File to load the Font from.
82 * @param fontSize the Size of the Font
83 */
84void Text::setFont(const char* fontFile, unsigned int fontSize)
85{
86  Font* tmpFont;
87  Text* newText;
88  Vector tmpVec;
89
90  // unloading the Font if we alrady have one loaded.
91  if (this->font != NULL && this->font != Font::defaultFont)
92    ResourceManager::getInstance()->unload(this->font);
93  this->font = NULL;
94
95  // load a new Font
96  if (fontFile != NULL)
97  {
98    tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, &fontSize);
99    if (tmpFont != NULL)
100      this->font = tmpFont;
101    else
102      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile);
103  }
104}
105
106/**
107 *  sets the Type of this Text
108 * @param type the type to set.
109 */
110void Text::setType(TEXT_RENDER_TYPE type)
111{
112  if (this->font != NULL && this->font->fontTTF)
113    this->type = type;
114  else
115    this->type = TEXT_RENDER_DYNAMIC;
116}
117
118/**
119 * Sets a new Text to the font
120 * @param text the new text to set
121 */
122void Text::setText(const char* text, bool isExtern)
123{
124  if (isExtern)
125  {
126    this->externText = text;
127
128    if (unlikely(this->text != NULL))
129    {
130      delete[] this->text;
131      this->text = NULL;
132    }
133  }
134  else
135  {
136    this->externText = NULL;
137    if (this->text)
138      delete[] this->text;
139    if (text != NULL)
140    {
141      this->text = new char[strlen(text)+1];
142      strcpy(this->text, text);
143    }
144    else
145      this->text = NULL;
146  }
147
148  // setting up the Text-Width if DYNAMIC
149//  if (this->type & TEXT_RENDER_DYNAMIC && this->getAlignment() != TEXT_ALIGN_LEFT && this->font != NULL)
150  const Font* calcSizeFont = this->font;
151  if (calcSizeFont != NULL || (calcSizeFont = Font::getDefaultFont()) != NULL)
152  {
153    Glyph** glyphArray = calcSizeFont->getGlyphArray();
154
155    float width = 0;
156    const char* tmpText = this->externText;
157    if (this->externText == NULL)
158      tmpText = this->text;
159    if (tmpText != NULL)
160    {
161      while (*tmpText != '\0')
162      {
163        if(glyphArray[*tmpText] != NULL)
164        {
165          width += glyphArray[*tmpText]->advance;
166        }
167        tmpText++;
168      }
169      this->setSizeX2D(this->width = width *this->getSizeY2D());
170    }
171  }
172}
173
174
175/**
176 *  creates a texture out of the given parameters !! TEXT_STATIC !! - mode
177 *
178 * this has to be called every time by the user, to if changes were made.
179 * this is only for TEXT_STATIC-mode
180 */
181void Text::createTexture()
182{
183  SDL_Surface* tmpSurf = NULL;
184  if (this->texture)
185    glDeleteTextures(1, &this->texture);
186  if (likely(this->font != NULL))
187  {
188    SDL_Color theColor = { (int)(this->color.x*255), (int)(this->color.y*255), (int)(this->color.z*255) };
189    tmpSurf = TTF_RenderText_Blended(this->font->fontTTF,
190                                     this->text,
191                                     theColor);
192  }
193  if (tmpSurf != NULL)
194  {
195    this->texture = loadTexture(tmpSurf, &this->texCoord);
196
197    this->width = tmpSurf->w;
198    this->height = tmpSurf->h;
199    SDL_FreeSurface(tmpSurf);
200  }
201}
202
203/**
204 *  draws the Text
205 */
206void Text::draw() const
207{
208  glPushMatrix();
209  // transform for alignment.
210  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
211    glTranslatef(-this->width, 0, 0);
212  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
213    glTranslatef(-this->width/2, 0, 0);
214
215  // drawing this Text.
216  // setting the Blending effects
217  glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
218  glEnable(GL_BLEND);
219  glEnable(GL_TEXTURE_2D);
220  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
221
222  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
223
224  if(likely(type & TEXT_RENDER_DYNAMIC ))
225  {
226    Glyph** glyphArray;
227    if (likely (this->font != NULL))
228    {
229      glyphArray = this->font->getGlyphArray();
230      glBindTexture(GL_TEXTURE_2D, font->getFastTextureID());
231    }
232    else
233    {
234      if (unlikely(Font::getDefaultFont() == NULL))
235        Font::initDefaultFont();
236      glyphArray = Font::getDefaultFont()->getGlyphArray();
237      glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getFastTextureID());
238    }
239    const char* tmpText = this->externText;
240    if (this->externText == NULL)
241      tmpText = this->text;
242    if (likely(tmpText != NULL))
243    {
244      glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
245      glRotatef(this->getAbsDir2D(), 0, 0, 1);
246      Glyph* tmpGlyph;
247      float posX = 0.0f;
248      while (likely(*tmpText != '\0'))
249      {
250        if(likely((tmpGlyph = glyphArray[*tmpText]) != NULL))
251        {
252          glBegin(GL_QUADS);
253
254          glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
255          glVertex2d(posX, 0);
256
257          glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
258          glVertex2d(posX, this->size);
259
260          glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
261          glVertex2d(posX+tmpGlyph->width*this->size, this->size);
262
263          glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
264          glVertex2d(posX+tmpGlyph->width*this->size, 0);
265
266          glEnd();
267          glEndList();
268          posX += tmpGlyph->advance * this->size;
269        }
270        ++tmpText;
271      }
272    }
273  }
274  else //(if type & TEXT_RENDER_STATIC)
275  {
276    glBindTexture(GL_TEXTURE_2D, this->texture);
277    glBegin(GL_QUADS);
278
279    glTexCoord2f(this->texCoord.minU, this->texCoord.minV);
280    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
281
282    glTexCoord2f(this->texCoord.maxU, this->texCoord.minV);
283    glVertex2f(this->getAbsCoor2D().x + this->width, this->getAbsCoor2D().);
284
285    glTexCoord2f(this->texCoord.maxU, this->texCoord.maxV);
286    glVertex2f(this->getAbsCoor2D().x + this->width, getAbsCoor2D().y + this->height);
287
288    glTexCoord2f(this->texCoord.minU, this->texCoord.maxV);
289    glVertex2f(getAbsCoor2D().x, getAbsCoor2D().y + this->height);
290
291    glEnd();
292
293  }
294  glPopMatrix();
295}
296
297/**
298 *  prints out some nice debug information about this text
299 */
300void Text::debug() const
301{
302  if (this->externText == NULL)
303    PRINT(0)("=== TEXT: %s ===\n", this->text);
304  else
305    PRINT(0)("=== TEXT: %s ===\n", this->externText);
306
307  if (this->getBindNode())
308    PRINT(0)("is bind to %s; ref=%p\n", this->getBindNode()->getName(), this->getBindNode());
309  PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
310}
311
312
313////////////
314/// UTIL ///
315////////////
316/**
317 * Loads a Font from an SDL_surface into a texture.
318 * @param surface The surface to make the texture of
319 * @param texCoord The texture coordinates of the 4 corners of the texture
320 * @returns the ID of the texture
321 */
322GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord)
323{
324  GLuint texture;
325  int w, h;
326  SDL_Surface *image;
327  SDL_Rect area;
328  Uint32 saved_flags;
329  Uint8  saved_alpha;
330
331  /* Use the surface width and height expanded to powers of 2 */
332  w = powerOfTwo(surface->w);
333  h = powerOfTwo(surface->h);
334  if (texCoord != NULL)
335  {
336    texCoord->minU = 0.0f;
337    texCoord->minV = 0.0f;
338    texCoord->maxU = (GLfloat)surface->w / w;
339    texCoord->maxV = (GLfloat)surface->h / h;
340  }
341  image = SDL_CreateRGBSurface(SDL_SWSURFACE,
342                               w, h,
343                               32,
344#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
345                               0x000000FF,
346                               0x0000FF00,
347                               0x00FF0000,
348                               0xFF000000
349#else
350                                   0xFF000000,
351                               0x00FF0000,
352                               0x0000FF00,
353                               0x000000FF
354#endif
355                              );
356  if ( image == NULL ) {
357    return 0;
358  }
359
360  /* Save the alpha blending attributes */
361  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
362  saved_alpha = surface->format->alpha;
363  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
364    SDL_SetAlpha(surface, 0, 0);
365  }
366
367  /* Copy the surface into the GL texture image */
368  area.x = 0;
369  area.y = 0;
370  area.w = surface->w;
371  area.h = surface->h;
372  SDL_BlitSurface(surface, &area, image, &area);
373
374  /* Restore the alpha blending attributes */
375  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
376    SDL_SetAlpha(surface, saved_flags, saved_alpha);
377  }
378
379  /* Create an OpenGL texture for the image */
380  glGenTextures(1, &texture);
381  glBindTexture(GL_TEXTURE_2D, texture);
382  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
383  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
384  glTexImage2D(GL_TEXTURE_2D,
385               0,
386               GL_RGBA,
387               w, h,
388               0,
389               GL_RGBA,
390               GL_UNSIGNED_BYTE,
391               image->pixels);
392  SDL_FreeSurface(image); /* No longer needed the data */
393
394  return texture;
395}
396
397/**
398 *  Quick utility function for texture creation
399 * @param input an integer
400 * @returns the next bigger 2^n-integer than input
401 */
402int Text::powerOfTwo(int input)
403{
404  int value = 1;
405
406  while ( value < input )
407    value <<= 1;
408  return value;
409}
Note: See TracBrowser for help on using the repository browser.