Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/text_engine/text.cc @ 7564

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

orxonox/trunk: works on windows too

File size: 7.7 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 "util/loading/resource_manager.h"
22#include "debug.h"
23
24using namespace std;
25
26/**
27 * @brief creates a new Text Element
28 * @param fontFile the Font to render this text in
29 * @param type The renderType to display this font in
30 */
31Text::Text(const std::string& fontFile, unsigned int textSize)
32{
33  this->setClassID(CL_TEXT, "Text");
34
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
45  if (!fontFile.empty())
46    this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
47  this->setSizeY2D(textSize);
48}
49
50/**
51 * @brief deletes a Text out of memory
52 */
53Text::~Text()
54{
55  if (this->font != NULL && this->font != Font::getDefaultFont())
56    ResourceManager::getInstance()->unload(this->font);
57}
58
59
60/**
61 * @brief sets the Font of this Text to font from fontFile
62 * @param fontFile the File to load the Font from.
63 * @param fontSize the Size of the Font
64 */
65void Text::setFont(const std::string& fontFile, unsigned int fontSize)
66{
67  Font* tmpFont;
68  Text* newText;
69  Vector tmpVec;
70
71  // unloading the Font if we alrady have one loaded.
72  if (this->font != NULL && this->font != Font::getDefaultFont())
73    ResourceManager::getInstance()->unload(this->font);
74  this->font = NULL;
75
76  // load a new Font
77  if (!fontFile.empty())
78  {
79    tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
80    if (tmpFont != NULL)
81      this->font = tmpFont;
82    else
83      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
84  }
85}
86
87/**
88 * @brief Sets a new Text to the font
89 * @param text the new text to set
90 */
91void Text::setText(const std::string& text)
92{
93  this->text = text;
94
95  // setting up the Text-Width if DYNAMIC
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)
99  {
100    Glyph** glyphArray = calcSizeFont->getGlyphArray();
101
102    float width = 0;
103    if (!this->text.empty())
104    {
105      for (unsigned int i = 0; i < this->text.size(); i++)
106      {
107        if(glyphArray[this->text[i]] != NULL)
108        {
109          width += glyphArray[this->text[i]]->advance;
110        }
111      }
112      this->setSizeX2D(width *this->getSizeY2D());
113    }
114  }
115}
116
117/**
118 * @brief draws the Text
119 */
120void Text::draw() const
121{
122  glPushMatrix();
123  // transform for alignment.
124  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
125    glTranslatef(-this->getSizeX2D(), 0, 0);
126  else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER)
127    glTranslatef(-this->getSizeX2D()/2, 0, 0);
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
138  Glyph** glyphArray;
139  if (likely (this->font != NULL))
140  {
141    glyphArray = this->font->getGlyphArray();
142    glBindTexture(GL_TEXTURE_2D, font->getTexture());
143  }
144  else
145  {
146    if (unlikely(Font::getDefaultFont() == NULL))
147      Font::initDefaultFont();
148    glyphArray = Font::getDefaultFont()->getGlyphArray();
149    glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getTexture());
150  }
151  if (likely(!this->text.empty()))
152  {
153    glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
154    glRotatef(this->getAbsDir2D(), 0, 0, 1);
155    Glyph* tmpGlyph;
156    float posX = 0.0f;
157
158    glBegin(GL_QUADS);
159    for (unsigned int i = 0; i < this->text.size(); i++)
160    {
161      if(likely((tmpGlyph = glyphArray[this->text[i]]) != NULL))
162      {
163        glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
164        glVertex2d(posX+tmpGlyph->width*this->getSizeY2D(), 0);
165
166        glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
167        glVertex2d(posX+tmpGlyph->width*this->getSizeY2D(), this->getSizeY2D());
168
169        glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
170        glVertex2d(posX, this->getSizeY2D());
171
172        glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
173        glVertex2d(posX, 0);
174
175        posX += tmpGlyph->advance * this->getSizeY2D();
176      }
177    }
178    glEnd();
179  }
180
181  glPopMatrix();
182}
183
184/**
185 * @brief prints out some nice debug information about this text
186 */
187void Text::debug() const
188{
189  PRINT(0)("=== TEXT: %s ===\n", this->text.c_str());
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/**
198 * @brief Loads a Font from an SDL_surface into a texture.
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);
263  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
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/**
279 * @brief Quick utility function for texture creation
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.