Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: saver font-changing in the Shell

File size: 10.4 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->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)
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)
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  {
151    Glyph** glyphArray = this->font->getGlyphArray();
152
153    float width = 0;
154    const char* tmpText = this->externText;
155    if (this->externText == NULL)
156      tmpText = this->text;
157    if (tmpText != NULL)
158    {
159      while (*tmpText != '\0')
160      {
161        if(glyphArray[*tmpText])
162        {
163          width += glyphArray[*tmpText]->width;
164        }
165        tmpText++;
166      }
167      this->width = width;
168    }
169  }
170}
171
172
173/**
174 *  creates a texture out of the given parameters !! TEXT_STATIC !! - mode
175 *
176 * this has to be called every time by the user, to if changes were made.
177 * this is only for TEXT_STATIC-mode
178 */
179void Text::createTexture()
180{
181  SDL_Surface* tmpSurf = NULL;
182  if (this->texture)
183    glDeleteTextures(1, &this->texture);
184  if (likely(this->font != NULL))
185  {
186    SDL_Color theColor = { (int)(this->color.x*255), (int)(this->color.y*255), (int)(this->color.z*255) };
187    tmpSurf = TTF_RenderText_Blended(this->font->fontTTF,
188                                     this->text,
189                                     theColor);
190  }
191  if (tmpSurf != NULL)
192  {
193    this->texture = loadTexture(tmpSurf, &this->texCoord);
194
195    this->width = tmpSurf->w;
196    this->height = tmpSurf->h;
197    SDL_FreeSurface(tmpSurf);
198  }
199}
200
201/**
202 *  draws the Text
203 */
204void Text::draw() const
205{
206  glPushMatrix();
207  // transform for alignment.
208  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
209    glTranslatef(-this->width, 0, 0);
210  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
211    glTranslatef(-this->width/2, 0, 0);
212
213  // drawing this Text.
214  // setting the Blending effects
215  glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
216  glEnable(GL_BLEND);
217  glEnable(GL_TEXTURE_2D);
218  glBlendFunc(GL_SRC_ALPHA, GL_ONE);
219
220  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
221
222  if(likely(type & TEXT_RENDER_DYNAMIC ))
223  {
224    Glyph** glyphArray;
225    if (likely (this->font != NULL))
226    {
227      glyphArray = this->font->getGlyphArray();
228      glBindTexture(GL_TEXTURE_2D, font->getFastTextureID());
229    }
230    else
231    {
232      if (unlikely(Font::getDefaultFont() == NULL))
233        Font::initDefaultFont();
234      glyphArray = Font::getDefaultFont()->getGlyphArray();
235      glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getFastTextureID());
236    }
237    glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
238//      glRotatef(this->getAbsDir2D(), 0,0,1);
239    const char* tmpText = this->externText;
240    if (this->externText == NULL)
241      tmpText = this->text;
242    if (likely(tmpText != NULL))
243    {
244      Glyph* tmpGlyph;
245      while (likely(*tmpText != '\0'))
246      {
247        if(likely((tmpGlyph = glyphArray[*tmpText]) != NULL))
248        {
249          glBegin(GL_QUADS);
250          glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
251          glVertex2d(0, - tmpGlyph->bearingY * this->size);
252          glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
253          glVertex2d(0, (tmpGlyph->height - tmpGlyph->bearingY)*this->size);
254          glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
255          glVertex2d(tmpGlyph->width*this->size, (tmpGlyph->height - tmpGlyph->bearingY)*this->size);
256          glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
257          glVertex2d(tmpGlyph->width*this->size, - tmpGlyph->bearingY*this->size);
258          glEnd();
259          glEndList();
260          glTranslatef(glyphArray[*tmpText]->width*this->size, 0, 0);
261        }
262        tmpText++;
263      }
264    }
265  }
266  else //(if type & TEXT_RENDER_STATIC)
267  {
268    glBindTexture(GL_TEXTURE_2D, this->texture);
269    glBegin(GL_QUADS);
270
271    glTexCoord2f(this->texCoord.minU, this->texCoord.minV);
272    glVertex2f(this->getAbsCoor2D().x,   this->getAbsCoor2D().);
273
274    glTexCoord2f(this->texCoord.maxU, this->texCoord.minV);
275    glVertex2f(this->getAbsCoor2D().x + this->width, this->getAbsCoor2D().);
276
277    glTexCoord2f(this->texCoord.maxU, this->texCoord.maxV);
278    glVertex2f(this->getAbsCoor2D().x + this->width, getAbsCoor2D().y + this->height);
279
280    glTexCoord2f(this->texCoord.minU, this->texCoord.maxV);
281    glVertex2f(getAbsCoor2D().x, getAbsCoor2D().y + this->height);
282
283    glEnd();
284
285  }
286  glPopMatrix();
287}
288
289/**
290 *  prints out some nice debug information about this text
291 */
292void Text::debug() const
293{
294  if (this->externText == NULL)
295    PRINT(0)("=== TEXT: %s ===\n", this->text);
296  else
297    PRINT(0)("=== TEXT: %s ===\n", this->externText);
298
299  if (this->getBindNode())
300    PRINT(0)("is bind to %s; ref=%p\n", this->getBindNode()->getName(), this->getBindNode());
301  PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
302}
303
304
305////////////
306/// UTIL ///
307////////////
308/**
309 * Loads a Font from an SDL_surface into a texture.
310 * @param surface The surface to make the texture of
311 * @param texCoord The texture coordinates of the 4 corners of the texture
312 * @returns the ID of the texture
313 */
314GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord)
315{
316  GLuint texture;
317  int w, h;
318  SDL_Surface *image;
319  SDL_Rect area;
320  Uint32 saved_flags;
321  Uint8  saved_alpha;
322
323  /* Use the surface width and height expanded to powers of 2 */
324  w = powerOfTwo(surface->w);
325  h = powerOfTwo(surface->h);
326  if (texCoord != NULL)
327  {
328    texCoord->minU = 0.0f;
329    texCoord->minV = 0.0f;
330    texCoord->maxU = (GLfloat)surface->w / w;
331    texCoord->maxV = (GLfloat)surface->h / h;
332  }
333  image = SDL_CreateRGBSurface(SDL_SWSURFACE,
334                               w, h,
335                               32,
336#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
337                               0x000000FF,
338                               0x0000FF00,
339                               0x00FF0000,
340                               0xFF000000
341#else
342                                   0xFF000000,
343                               0x00FF0000,
344                               0x0000FF00,
345                               0x000000FF
346#endif
347                              );
348  if ( image == NULL ) {
349    return 0;
350  }
351
352  /* Save the alpha blending attributes */
353  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
354  saved_alpha = surface->format->alpha;
355  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
356    SDL_SetAlpha(surface, 0, 0);
357  }
358
359  /* Copy the surface into the GL texture image */
360  area.x = 0;
361  area.y = 0;
362  area.w = surface->w;
363  area.h = surface->h;
364  SDL_BlitSurface(surface, &area, image, &area);
365
366  /* Restore the alpha blending attributes */
367  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
368    SDL_SetAlpha(surface, saved_flags, saved_alpha);
369  }
370
371  /* Create an OpenGL texture for the image */
372  glGenTextures(1, &texture);
373  glBindTexture(GL_TEXTURE_2D, texture);
374  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
375  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
376  glTexImage2D(GL_TEXTURE_2D,
377               0,
378               GL_RGBA,
379               w, h,
380               0,
381               GL_RGBA,
382               GL_UNSIGNED_BYTE,
383               image->pixels);
384  SDL_FreeSurface(image); /* No longer needed the data */
385
386  return texture;
387}
388
389/**
390 *  Quick utility function for texture creation
391 * @param input an integer
392 * @returns the next bigger 2^n-integer than input
393 */
394int Text::powerOfTwo(int input)
395{
396  int value = 1;
397
398  while ( value < input )
399    value <<= 1;
400  return value;
401}
Note: See TracBrowser for help on using the repository browser.