Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/textEngine/src/lib/graphics/font/text_engine.cc @ 3770

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

orxonox/branches/textEngine: textEngine rendering again

File size: 20.0 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   for some fonts and licenses visit: =http://www.dafont.com/en/font.php=
16
17   !! IMPORTANT !! When using ttf fonts clear the license issues prior to
18   adding them to orxonox. This is really important, because we do not
19   want to offend anyone.
20*/
21
22#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_FONT
23
24#include "text_engine.h"
25
26using namespace std;
27
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31
32#include "graphics_engine.h"
33#include "resource_manager.h"
34
35#include "p_node.h"
36#include "vector.h"
37#include "debug.h"
38
39
40////////////
41/// TEXT ///
42////////////
43Text::Text(Font* font, int type)
44{
45  // initialize this Text
46  this->bindNode = NULL;
47  this->font = font;
48  this->text = NULL;
49  this->texture = 0;
50  this->setType(type);
51  this->setPosition(0, 0);
52
53  this->setText(FONT_DEFAULT_TEXT);
54}
55
56
57Text::~Text(void)
58{
59
60}
61
62
63void Text::setBindNode(PNode* bindNode)
64{
65  this->bindNode = bindNode;
66}
67
68/**
69   \brief sets the Type of this Text
70   \param type the type to set.
71*/
72void Text::setType(int type)
73{
74  this->type = type;
75}
76
77
78/**
79   \brief Sets a new Text to the font
80   \param text the new text to set
81*/
82void Text::setText(const char* text)
83{
84  if (this->text)
85    delete []this->text;
86  this->text = new char[strlen(text)+1];
87  strcpy(this->text, text);
88}
89
90/**
91   \brief sets a Position.
92   \param x the x-position in pixels from the left border
93   \param y the y-position in pixels from the top border
94*/
95void Text::setPosition(int x, int y)
96{
97  this->posSize.x = x;
98  this->posSize.y = y;
99}
100
101/**
102   \brief sets a new color to the font
103   \param r Red
104   \param g Green
105   \param b Blue
106*/
107void Text::setColor(Uint8 r, Uint8 g, Uint8 b)
108{
109  this->color.r = r;
110  this->color.g = g;
111  this->color.b = b;
112}
113
114/**
115   \brief creates a texture out of the given parameters
116
117   this has to be called every time by the user, to if changes were made.
118*/
119void Text::createTexture(void)
120{
121  SDL_Surface* tmpSurf;
122  if (this->texture)
123    glDeleteTextures(1, &this->texture);
124  tmpSurf = TTF_RenderText_Blended(this->font->font,
125                                   this->text,
126                                   this->color);
127  if (tmpSurf)
128    this->texture = loadTexture(tmpSurf, &this->texCoord);
129
130  this->posSize.w = tmpSurf->w;
131  this->posSize.h = tmpSurf->h;
132  SDL_FreeSurface(tmpSurf);
133}
134
135/**
136   \brief draws the Font
137   \todo FIX this is to slow/static
138*/
139void Text::draw(void)
140{
141  // storing all the Transformation Matrices.
142  GLdouble modMat[16];
143  GLdouble projMat[16];
144  GLint viewPort[4];
145  glGetDoublev(GL_PROJECTION_MATRIX, projMat);
146  glGetDoublev(GL_MODELVIEW_MATRIX, modMat);
147  glGetIntegerv(GL_VIEWPORT, viewPort);
148
149  GraphicsEngine::enter2DMode();
150
151
152  // setting the Position of this Text.
153  Vector pos;
154  if (this->bindNode)
155    {
156      GLdouble x = this->bindNode->getAbsCoor().x;
157      GLdouble y = this->bindNode->getAbsCoor().y;
158      GLdouble z = this->bindNode->getAbsCoor().z;
159      GLdouble tmp[3];
160      gluProject(x, y, z, modMat, projMat, viewPort, tmp, tmp+1, tmp+2);
161      printf("test %f %f %f,\n", tmp[0], tmp[1], tmp[2]);
162      pos.x = tmp[0] + this->posSize.x;
163      pos.y = GraphicsEngine::getInstance()->getResolutionY() - tmp[1] + this->posSize.y;
164      pos.z = tmp[2];
165    }
166  else 
167    {
168      pos.x = this->posSize.x;
169      pos.y = this->posSize.y;
170      pos.z = 0;
171    }
172
173  // drawing this Text.
174  if(type == TEXT_STATIC)
175    {
176      glBindTexture(GL_TEXTURE_2D, this->texture);
177      glEnable(GL_TEXTURE_2D);
178      glBegin(GL_QUADS);
179     
180      glTexCoord2f(this->texCoord.minU, this->texCoord.minV);
181      glVertex2i(pos.x,   pos.);
182     
183      glTexCoord2f(this->texCoord.maxU, this->texCoord.minV);
184      glVertex2i(pos.x + this->posSize.w, pos.);
185     
186      glTexCoord2f(this->texCoord.maxU, this->texCoord.maxV);
187      glVertex2i(pos.x + this->posSize.w, pos.y + this->posSize.h);
188     
189      glTexCoord2f(this->texCoord.minU, this->texCoord.maxV);
190      glVertex2i(pos.x, pos.y + this->posSize.h);
191     
192      glEnd();
193    }
194  else //(if type == TEXT_DYNAMIC)
195    {
196      Glyph** glyphArray = this->font->getGlyphArray();
197      glBindTexture(GL_TEXTURE_2D, this->font->getFastTextureID());
198      //      glEnable(GL_TEXTURE_2D);
199      glTranslatef(pos.x, pos.y, 0);
200
201      //      printf("%d, %d\n", this->font->getFastTextureID(), glyphArray[65]->displayList);
202      char* tmpText = this->text;
203      while (*tmpText != '\0')
204        {
205          if(glyphArray[*tmpText])
206            {
207              glCallList(glyphArray[*tmpText]->displayList);
208              glTranslatef(glyphArray[*tmpText]->width, 0, 0);
209            }
210          tmpText++;
211        }
212    }
213  GraphicsEngine::leave2DMode();
214}
215
216
217
218////////////
219/// FONT ///
220////////////
221/**
222   \brief constructs a Font
223   \param fontFile the File to load the font from
224   \param fontSize the Size of the Font in Pixels
225   \param r Red value of the Font.
226   \param g Green value of the Font.
227   \param b Blue value of the Font.
228*/
229Font::Font(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b)
230{
231  // setting default values.
232  this->font = NULL;
233  this->fontFile = NULL;
234  this->glyphArray = NULL;
235  this->fastTextureID = 0; 
236 
237  this->setSize(fontSize);
238  this->setStyle("c");//TTF_STYLE_NORMAL);
239
240  this->setFont(fontFile);
241
242
243  this->setFastColor(r, g, b);
244
245  this->fastTextureID = this->createFastTexture();
246}
247
248/**
249   \brief destructs a font
250*/
251Font::~Font(void)
252{
253  // deleting the List of all Texts
254
255  // deleting all Glyphs
256  if (this->glyphArray)
257    {
258      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
259        delete this->glyphArray[i];
260      delete []this->glyphArray;
261    }
262
263  // erease this font out of the memory.
264  if (this->font)
265    TTF_CloseFont(this->font);
266}
267
268/**
269   \brief sets The Font.
270   \param fontFile The file containing the font.
271   \returns true if loaded, false if something went wrong, or if a font was loaded before.
272*/
273bool Font::setFont(const char* fontFile)
274{
275  if (!this->fontFile)
276    {
277      this->fontFile = new char[strlen(fontFile)+1];
278      strcpy(this->fontFile, fontFile);
279     
280      this->font = TTF_OpenFont(this->fontFile, this->fontSize);
281      if(!this->font) 
282        {
283          PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
284          return false;
285      }
286      return true;
287    }
288  else
289    {
290      PRINTF(2)("Font already initialized, unable to change it now.\n");
291      return false;
292    }
293}
294
295/**
296   \brief sets a specific renderStyle
297   \param renderStyle the Style to render: a char-array containing:
298   i: italic, b: bold, u, underline
299*/
300void Font::setStyle(char* renderStyle)
301{
302  this->renderStyle = TTF_STYLE_NORMAL;
303 
304  for (int i = 0; i < strlen(renderStyle); i++)
305    if (strncmp(renderStyle+i, "b", 1) == 0) 
306      this->renderStyle |= TTF_STYLE_BOLD;
307    else if (strncmp(renderStyle+i, "i", 1) == 0)
308      this->renderStyle |= TTF_STYLE_ITALIC;
309    else if (strncmp(renderStyle+i, "u", 1) == 0) 
310      this->renderStyle |= TTF_STYLE_UNDERLINE;
311
312  if (this->font)
313    TTF_SetFontStyle(this->font, this->renderStyle);
314  else
315    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
316}
317
318
319
320/**
321   \brief Sets a new Size to the font
322   \param fontSize The new Size in pixels.
323*/
324void Font::setSize(unsigned int fontSize)
325{
326  this->fontSize = fontSize;
327}
328
329/**
330   \brief sets a new color to the font
331   \param r Red
332   \param g Green
333   \param b Blue
334*/
335void Font::setFastColor(Uint8 r, Uint8 g, Uint8 b)
336{
337  this->fastColor.r = r;
338  this->fastColor.g = g;
339  this->fastColor.b = b;
340}
341
342/**
343   \returns the maximum height of the Font, if the font was initialized, 0 otherwise
344*/
345int Font::getMaxHeight(void)
346{
347  if (this->font)
348    return TTF_FontHeight(this->font);
349  else
350    return 0;
351}
352
353/**
354   \returns the maximum ascent of the Font, if the font was initialized, 0 otherwise
355
356   the ascent is the pixels of the font above the baseline
357*/
358int Font::getMaxAscent(void)
359{
360  if (this->font)
361    return TTF_FontAscent(this->font);
362  else
363    return 0;
364}
365
366/**
367   \returns the maximum descent of the Font, if the font was initialized, 0 otherwise
368
369   the descent is the pixels of the font below the baseline
370*/
371int Font::getMaxDescent(void)
372{
373  if (this->font)
374    return TTF_FontDescent(this->font);
375  else
376    return 0;
377}
378
379/**
380   \param character The character to get info about.
381   \returns a Glyph struct of a character. This Glyph is a pointer,
382   and MUST be deleted by the user..
383
384   This only works for horizontal fonts. see
385   http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
386   for more info about vertical Fonts
387*/
388Glyph* Font::getGlyphMetrics(Uint16 character)
389{
390  Glyph* rg = new Glyph;
391  rg->character = character;
392  TTF_GlyphMetrics(this->font, rg->character,
393                   &rg->minX, &rg->maxX,
394                   &rg->minY, &rg->maxY,
395                   &rg->advance);
396  rg->height = rg->maxY - rg->minY;
397  rg->width = rg->maxX - rg->minX;
398  rg->bearingX = (rg->advance - rg->width) / 2;
399  rg->bearingY = rg->maxY;
400  return rg;
401}
402
403GLuint Font::createFastTexture(void)
404{
405  /* interesting GLYPHS:
406   *  33-47: Special Characters.
407   *  48-57: 0-9
408   *  58-63: some more special chars (minor)
409   *  65-90: A-Z
410   *  97-122: a-z
411   */
412  int numberOfGlyphs = 90;
413
414  this->initGlyphs(33, numberOfGlyphs);
415
416  int rectSize = this->findOptimalFastTextureSize();
417
418  // setting default values. (maybe not needed afterwards)
419  SDL_Color tmpColor;  tmpColor.r = tmpColor.g = tmpColor.b = 0;
420  // Surface definition.
421  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
422  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_SWSURFACE,
423                                               rectSize, rectSize,
424                                               32,
425#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
426                                               0x000000FF, 
427                                               0x0000FF00, 
428                                               0x00FF0000, 
429                                               0xFF000000
430#else
431                                               0xFF000000,
432                                               0x00FF0000, 
433                                               0x0000FF00, 
434                                               0x000000FF
435#endif
436                                               );
437  tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h;
438  SDL_SetClipRect(tmpSurf, &tmpRect);
439  int maxLineHeight = 0;
440
441  // all the interessting Glyphs
442  for (int i = 33; i <= 122; i++)
443    {
444      SDL_Surface* glyphSurf = NULL;
445      Glyph* tmpGlyph;
446
447      if (tmpGlyph = this->glyphArray[i])
448        {
449          if (tmpGlyph->height > maxLineHeight)
450            maxLineHeight = tmpGlyph->height;
451         
452          if (tmpRect.x+tmpGlyph->width > tmpSurf->w)
453            {
454              tmpRect.x = 0;
455              tmpRect.y = tmpRect.y + maxLineHeight + 1;
456              maxLineHeight = 0;
457            }
458          if (tmpRect.y + maxLineHeight > tmpSurf->h)
459            {
460              PRINTF(1)("Protection, so font cannot write over the boundraries error (this should not heappen\n");
461              break;
462            }
463          // reading in the new Glyph
464          glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->fastColor);
465          if( glyphSurf ) 
466            {
467
468              SDL_SetAlpha(glyphSurf, 0, 0);
469
470              SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect);
471              TexCoord tmpTexCoord;
472              tmpTexCoord.minU = (float)tmpRect.x/(float)tmpSurf->w;
473              tmpTexCoord.maxU = (float)(tmpRect.x+tmpGlyph->width)/(float)tmpSurf->w;
474              tmpTexCoord.minV = (float)tmpRect.y/(float)tmpSurf->w;
475              tmpTexCoord.maxV = (float)(tmpRect.y+tmpGlyph->height)/(float)tmpSurf->w;
476              tmpGlyph->displayList = glGenLists(1);
477
478              glNewList(tmpGlyph->displayList, GL_COMPILE);
479              glBegin(GL_QUADS);
480              glTexCoord2f(tmpTexCoord.minU, tmpTexCoord.minV);
481              glVertex2d(0, 0);
482              glTexCoord2f(tmpTexCoord.minU, tmpTexCoord.maxV);
483              glVertex2d(0, tmpGlyph->height);
484              glTexCoord2f(tmpTexCoord.maxU, tmpTexCoord.maxV);
485              glVertex2d(tmpGlyph->width, tmpGlyph->height);
486              glTexCoord2f(tmpTexCoord.maxU, tmpTexCoord.minV);
487              glVertex2d(tmpGlyph->width, 0);
488              glEnd();
489              glEndList();
490              SDL_FreeSurface(glyphSurf);
491
492              tmpRect.x += tmpGlyph->width + 1;
493
494              // Outputting Glyphs to BMP-files.
495              /*
496                char outname[64];
497                if (i < 10)
498                sprintf( outname, "glyph-00%d.bmp", i );
499                else if (i <100)
500                sprintf( outname, "glyph-0%d.bmp", i );
501                else
502                sprintf( outname, "glyph-%d.bmp", i );
503                SDL_SaveBMP(tmpSurf, outname);
504              */
505            }
506        }
507    }
508  GLuint texture;
509  glGenTextures(1, &texture);
510  glBindTexture(GL_TEXTURE_2D, texture);
511  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
512  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
513  glTexImage2D(GL_TEXTURE_2D,
514               0,
515               GL_RGBA,
516               tmpSurf->w, tmpSurf->h,
517               0,
518               GL_RGBA,
519               GL_UNSIGNED_BYTE,
520               tmpSurf->pixels);
521  SDL_FreeSurface(tmpSurf);
522  return texture;
523}
524
525/**
526   \brief stores Glyph Metrics in an Array.
527   \param from The Glyph to start from.
528   \param count The number of Glyphs to start From.
529*/
530void Font::initGlyphs(Uint16 from, Uint16 count)
531{
532  /* initialize the Array, and set all its entries to NULL
533   *  only if the Glyph-array has not been initialized
534   */
535  if (!this->glyphArray)
536    {
537      this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
538      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
539        this->glyphArray[i] = NULL;
540    }
541 
542  Uint16 lastGlyph = from + count;
543 
544  for (int i = from; i <= lastGlyph; i++)
545    {
546      // setting up all the Glyphs we like.
547      glyphArray[i] = getGlyphMetrics(i);
548    }
549  return;
550}
551
552/**
553   \returns the optimal size to use as the texture size
554
555   \todo: this algorithm can be a lot more faster, althought it does
556   not really matter within the init-context, and 128 glyphs.
557
558   This function searches for a 2^n sizes texture-size, this is for
559   openGL-version < 1.2 compatibility. and because it is realy easy like this.
560*/
561int Font::findOptimalFastTextureSize(void)
562{
563  int i;
564  int x,y; // the counters
565  int maxLineHeight;
566  int size = 32;      // starting Value, we have to start somewhere 32 seems reasonable.
567  bool sizeOK = false;
568  Glyph* tmpGlyph;
569
570  while (!sizeOK)
571    {
572      x = 0; y = 0;
573      maxLineHeight = 0;
574      for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
575        {
576          if(tmpGlyph = this->glyphArray[i])
577            {
578              // getting the height of the highest Glyph in the Line.
579              if (tmpGlyph->height > maxLineHeight)
580                maxLineHeight = tmpGlyph->height;
581
582              if (x + tmpGlyph->width > size)
583                {
584                  x = 0;
585                  y = y + maxLineHeight;
586                  maxLineHeight = 0;
587                }
588              if (y + maxLineHeight + 1 > size)
589                break;
590              x += tmpGlyph->width + 1;
591
592            }
593        }
594      if (i == FONT_HIGHEST_KNOWN_CHAR)
595        sizeOK = true;
596      else
597        size *= 2;
598    }
599  return size;
600 
601}
602
603
604/**
605   \brief a simple function to get some interesting information about this class
606*/
607void Font::debug(void)
608{
609
610  // print the loaded font's style
611  int style;
612  style=TTF_GetFontStyle(this->font);
613  PRINTF(0)("The font style is:");
614  if(style==TTF_STYLE_NORMAL)
615    PRINTF(0)(" normal");
616  else {
617    if(style&TTF_STYLE_BOLD)
618      PRINTF(0)(" bold");
619    if(style&TTF_STYLE_ITALIC)
620      PRINTF(0)(" italic");
621    if(style&TTF_STYLE_UNDERLINE)
622      PRINTF(0)(" underline");
623  }
624  PRINTF(0)("\n");
625
626
627}
628
629////////////
630/// UTIL ///
631////////////
632/**
633   \brief Loads a Font from an SDL_surface into a texture.
634   \param surface The surface to make the texture of
635   \param texCoord The texture coordinates of the 4 corners of the texture
636   \returns the ID of the texture
637*/
638GLuint loadTexture(SDL_Surface *surface, TexCoord* texCoord)
639{
640  GLuint texture;
641  int w, h;
642  SDL_Surface *image;
643  SDL_Rect area;
644  Uint32 saved_flags;
645  Uint8  saved_alpha;
646 
647  /* Use the surface width and height expanded to powers of 2 */
648  w = powerOfTwo(surface->w);
649  h = powerOfTwo(surface->h);
650  if (texCoord)
651    {
652      texCoord->minU = 0.0f;
653      texCoord->minV = 0.0f;
654      texCoord->maxU = (GLfloat)surface->w / w;
655      texCoord->maxV = (GLfloat)surface->h / h;
656    }
657  image = SDL_CreateRGBSurface(SDL_SWSURFACE,
658                               w, h,
659                               32,
660#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
661                               0x000000FF, 
662                               0x0000FF00, 
663                               0x00FF0000, 
664                               0xFF000000
665#else
666                               0xFF000000,
667                               0x00FF0000, 
668                               0x0000FF00, 
669                               0x000000FF
670#endif
671                               );
672  if ( image == NULL ) {
673    return 0;
674  }
675 
676  /* Save the alpha blending attributes */
677  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
678  saved_alpha = surface->format->alpha;
679  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
680    SDL_SetAlpha(surface, 0, 0);
681  }
682 
683  /* Copy the surface into the GL texture image */
684  area.x = 0;
685  area.y = 0;
686  area.w = surface->w;
687  area.h = surface->h;
688  SDL_BlitSurface(surface, &area, image, &area);
689 
690  /* Restore the alpha blending attributes */
691  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
692    SDL_SetAlpha(surface, saved_flags, saved_alpha);
693  }
694 
695  /* Create an OpenGL texture for the image */
696  glGenTextures(1, &texture);
697  glBindTexture(GL_TEXTURE_2D, texture);
698  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
699  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
700  glTexImage2D(GL_TEXTURE_2D,
701               0,
702               GL_RGBA,
703               w, h,
704               0,
705               GL_RGBA,
706               GL_UNSIGNED_BYTE,
707               image->pixels);
708  SDL_FreeSurface(image); /* No longer needed */
709 
710  return texture;
711}
712
713/**
714   \brief Quick utility function for texture creation
715   \param input an integer
716   \returns the next bigger 2^n-integer than input
717*/
718int powerOfTwo(int input)
719{
720  int value = 1;
721 
722  while ( value < input ) {
723    value <<= 1;
724  }
725  return value;
726}
727
728
729
730
731
732
733///////////////////
734/// TEXT-ENGINE ///
735///////////////////
736/**
737   \brief standard constructor
738*/
739TextEngine::TextEngine () 
740{
741   this->setClassName ("TextEngine");
742   this->enableFonts();
743
744   this->textList = new tList<Text>;
745}
746
747/**
748   \brief the singleton reference to this class
749*/
750TextEngine* TextEngine::singletonRef = NULL;
751
752/**
753   \returns a Pointer to this Class
754*/
755TextEngine* TextEngine::getInstance(void)
756{
757  if (!TextEngine::singletonRef)
758    TextEngine::singletonRef = new TextEngine();
759  return TextEngine::singletonRef;
760}
761
762/**
763   \brief standard deconstructor
764
765*/
766TextEngine::~TextEngine () 
767{
768  this->disableFonts();
769 
770  delete this->textList;
771
772  TextEngine::singletonRef = NULL;
773
774}
775
776/**
777   \brief function to enable TTF_Fonts
778*/
779void TextEngine::enableFonts(void)
780{
781  if (!TTF_WasInit())
782    {
783      if(TTF_Init()==-1)
784        PRINTF(1)("TTF_Init: %s\n", TTF_GetError());
785
786      TextEngine::checkVersion();
787    }
788  else
789    PRINTF(4)("Fonts already initialized\n");
790     
791}
792
793/**
794   \brief function to disable TTF_fonts
795*/
796void TextEngine::disableFonts(void)
797{
798  if (TTF_WasInit())
799    {
800      TTF_Quit();
801    }
802  else
803    PRINTF(4)("Fonts were not initialized.\n");
804}
805
806/**
807   \brief creates a new Text with a certain font.
808   \see Font::Font
809   \see Text::Text
810*/
811Text* TextEngine::createText(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b)
812{
813  Font* tmpFont;
814  Text* newText;
815
816  tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME);
817  if (!tmpFont)
818    {
819      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile);
820      return NULL;
821    }
822
823  newText = new Text(tmpFont, TEXT_DYNAMIC);
824  textList->add(newText);
825
826  return newText;
827}
828
829void TextEngine::draw(void)
830{
831  tIterator<Text>* textIterator = textList->getIterator();
832  Text* text = textIterator->nextElement();
833  while( text != NULL)
834    {
835      text->draw();
836      text = textIterator->nextElement();
837    }
838  delete textIterator;
839}
840
841/**
842   \brief checks if the compiled version and the local version of SDL_ttf match.
843   \returns true if match, false otherwise
844*/
845bool TextEngine::checkVersion(void)
846{
847  SDL_version compile_version;
848  SDL_version link_version;
849  TTF_VERSION(&compile_version);
850  link_version = *TTF_Linked_Version();
851
852  if (compile_version.major == link_version.major &&
853      compile_version.minor == link_version.minor &&
854      compile_version.patch == link_version.patch)
855    {
856      return true;
857    }
858  else
859    {
860      PRINTF(2)("compiled with SDL_ttf version: %d.%d.%d\n", 
861                compile_version.major,
862                compile_version.minor,
863                compile_version.patch);
864     
865      PRINTF(2)("running with SDL_ttf version: %d.%d.%d\n", 
866                link_version.major,
867                link_version.minor,
868                link_version.patch);
869      return false;
870    }
871}
Note: See TracBrowser for help on using the repository browser.