Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc @ 3692

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

orxonox/branches/textEngine: some minor changes

File size: 11.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   ***
16   * This file is extended to the needs of the orxonox-project.         *
17   * the Copyright of the original file is below this copyright         *
18   *                                                                  ***
19
20   for some fonts and licenses visit: =http://www.dafont.com/en/font.php=
21*/
22
23/*
24    glfont:  An example of using the SDL_ttf library with OpenGL.
25    Copyright (C) 1997-2004 Sam Lantinga
26
27    This library is free software; you can redistribute it and/or
28    modify it under the terms of the GNU Library General Public
29    License as published by the Free Software Foundation; either
30    version 2 of the License, or (at your option) any later version.
31
32    This library is distributed in the hope that it will be useful,
33    but WITHOUT ANY WARRANTY; without even the implied warranty of
34    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35    Library General Public License for more details.
36
37    You should have received a copy of the GNU Library General Public
38    License along with this library; if not, write to the Free
39    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
40
41    The SDL_GL_* functions in this file are available in the public domain.
42
43    Sam Lantinga
44    slouken@libsdl.org
45*/
46
47#include "glfont.h"
48
49#include <stdlib.h>
50#include <stdio.h>
51#include <string.h>
52
53#include "debug.h"
54
55
56#define DEFAULT_SIZE    18
57#define DEFAULT_TEXT    "orxonox 1234567890"
58#define DEFAULT_COLOR_R 256
59#define DEFAULT_COLOR_G 256
60#define DEFAULT_COLOR_B 256
61#define NUM_COLORS      256
62
63
64
65GLFont::GLFont()
66{
67  this->init("");
68
69}
70
71
72GLFont::GLFont(const char* fontFile)
73{
74  this->init(fontFile);
75}
76
77GLFont::~GLFont()
78{
79  if (this->font)
80    TTF_CloseFont(this->font);
81}
82
83/**
84   \function to enable TTF_Fonts
85*/
86void GLFont::enableFonts(void)
87{
88  if (!GLFont::ttfInitialized)
89    {
90      if(TTF_Init()==-1) {
91        PRINTF(1)("TTF_Init: %s\n", TTF_GetError());
92        exit(2);
93      }
94      GLFont::checkVersion();
95      GLFont::ttfInitialized = true;
96    }
97  else
98    PRINTF(4)("Fonts already initialized\n");
99     
100}
101
102/**
103   \brief function to disable TTF_fonts
104*/
105void GLFont::disableFonts(void)
106{
107  if (GLFont::ttfInitialized)
108    {
109      TTF_Quit();
110      GLFont::ttfInitialized = false;
111    }
112  else
113    PRINTF(4)("Fonts were not initialized.\n");
114}
115
116bool GLFont::ttfInitialized = false;
117
118
119bool GLFont::init(const char* fontFile)
120{
121  // setting default values.
122  this->font = NULL;
123  this->fontFile = NULL;
124  this->fontSize = 16;
125 
126  this->renderStyle = TTF_STYLE_NORMAL;
127
128
129  if (!GLFont::ttfInitialized)
130    GLFont::enableFonts();
131}
132
133
134bool GLFont::setFont(const char* fontFile)
135{
136  if (!this->fontFile)
137    {
138      this->fontFile = new char[strlen(fontFile)+1];
139      strcpy(this->fontFile, fontFile);
140     
141      this->font = TTF_OpenFont(this->fontFile, this->fontSize);
142      if(!this->font) 
143        {
144          PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
145          // handle error
146      }
147    }
148  else
149    PRINTF(2)("Font already initialized, unable to change it now.\n");
150}
151
152
153void GLFont::setText(const char* text)
154{
155  this->text = new char[strlen(text)+1];
156  strcpy(this->text, text);
157}
158
159/**
160   \brief sets a specific renderStyle
161   \param renderStyle the Style to render: a char-array containing:
162   i: italic, b: bold, u, underline
163*/
164void GLFont::setStyle(char* renderStyle)
165{
166  this->renderStyle = TTF_STYLE_NORMAL;
167 
168  for (int i = 0; i < strlen(renderStyle); i++)
169    if (strncmp(renderStyle+i, "b", 1) == 0) 
170      this->renderStyle |= TTF_STYLE_BOLD;
171    else if (strncmp(renderStyle+i, "i", 1) == 0)
172      this->renderStyle |= TTF_STYLE_ITALIC;
173    else if (strncmp(renderStyle+i, "u", 1) == 0) 
174      this->renderStyle |= TTF_STYLE_UNDERLINE;
175
176  if (this->font)
177    TTF_SetFontStyle(this->font, this->renderStyle);
178  else
179    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
180}
181
182void GLFont::setSize(unsigned int fontSize)
183{
184  this->fontSize = fontSize;
185}
186
187void GLFont::setColor(Uint8 r, Uint8 g, Uint8 b)
188{
189  this->color.r = r;
190  this->color.g = g;
191  this->color.b = b;
192}
193
194
195
196void GLFont::createTexture(void)
197{
198  GLfloat texcoord[4];
199  SDL_Surface* tmpSurf = TTF_RenderText_Blended(this->font, this->text, this->color);
200  texture = loadTexture(tmpSurf, texcoord);
201
202}
203
204
205/**
206   \returns the maximum height of the Font, if the font was initialized, 0 otherwise
207*/
208int GLFont::getMaxHeight(void)
209{
210  if (this->font)
211    return TTF_FontHeight(this->font);
212  else
213    return 0;
214}
215
216/**
217   \returns the maximum ascent of the Font, if the font was initialized, 0 otherwise
218
219   the ascent is the pixels of the font above the baseline
220*/
221int GLFont::getMaxAscent(void)
222{
223  if (this->font)
224    return TTF_FontAscent(this->font);
225  else
226    return 0;
227}
228
229/**
230   \returns the maximum descent of the Font, if the font was initialized, 0 otherwise
231
232   the descent is the pixels of the font below the baseline
233*/
234int GLFont::getMaxDescent(void)
235{
236  if (this->font)
237    return TTF_FontDescent(this->font);
238  else
239    return 0;
240}
241
242/**
243   \param character The character to get info about.
244   \returns a Glyph struct of a character.
245
246   This only works for horizontal fonts. see
247   http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
248   for more info about vertical Fonts
249*/
250glyph GLFont::getGlyphMetrics(Uint16 character)
251{
252  glyph rg;
253  rg.character = character;
254  TTF_GlyphMetrics(this->font, rg.character,
255                   &rg.minX, &rg.maxX,
256                   &rg.minY, &rg.maxY,
257                   &rg.advance);
258  rg.height = rg.maxY - rg.minY;
259  rg.width = rg.maxX - rg.minX;
260  rg.bearingX = (rg.advance - rg.width) / 2;
261  rg.bearingY = rg.maxY;
262  return rg;
263}
264
265void GLFont::setPosition(int x, int y)
266{
267
268}
269
270void GLFont::renderText(void)
271{
272
273}
274
275
276void GLFont::renderText(const char* text, int x, int y)
277{
278  // enter the 2D-Mode
279
280  SDL_Surface *screen = SDL_GetVideoSurface();
281 
282  /* Note, there may be other things you need to change,
283     depending on how you have your OpenGL state set up.
284  */
285  glPushAttrib(GL_ENABLE_BIT);
286  glDisable(GL_DEPTH_TEST);
287  glDisable(GL_CULL_FACE);
288  glEnable(GL_TEXTURE_2D);
289 
290  /* This allows alpha blending of 2D textures with the scene */
291  glEnable(GL_BLEND);
292  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
293 
294  glViewport(0, 0, screen->w, screen->h);
295 
296  glMatrixMode(GL_PROJECTION);
297  glPushMatrix();
298  glLoadIdentity();
299 
300  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
301 
302  glMatrixMode(GL_MODELVIEW);
303  glPushMatrix();
304  glLoadIdentity();
305 
306  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
307
308
309
310
311
312  // Leave the 2D-Mode
313  glMatrixMode(GL_MODELVIEW);
314  glPopMatrix();
315 
316  glMatrixMode(GL_PROJECTION);
317  glPopMatrix();
318 
319  glPopAttrib();
320}
321
322void GLFont::enter2DMode()
323{
324  SDL_Surface *screen = SDL_GetVideoSurface();
325 
326  /* Note, there may be other things you need to change,
327     depending on how you have your OpenGL state set up.
328  */
329  glPushAttrib(GL_ENABLE_BIT);
330  glDisable(GL_DEPTH_TEST);
331  glDisable(GL_CULL_FACE);
332  glEnable(GL_TEXTURE_2D);
333 
334  /* This allows alpha blending of 2D textures with the scene */
335  glEnable(GL_BLEND);
336  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
337 
338  glViewport(0, 0, screen->w, screen->h);
339 
340  glMatrixMode(GL_PROJECTION);
341  glPushMatrix();
342  glLoadIdentity();
343 
344  glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
345 
346  glMatrixMode(GL_MODELVIEW);
347  glPushMatrix();
348  glLoadIdentity();
349 
350  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
351}
352
353
354void GLFont::leave2DMode()
355{
356        glMatrixMode(GL_MODELVIEW);
357        glPopMatrix();
358
359        glMatrixMode(GL_PROJECTION);
360        glPopMatrix();
361
362        glPopAttrib();
363}
364
365
366GLuint GLFont::loadTexture(SDL_Surface *surface, GLfloat *texcoord)
367{
368  GLuint texture;
369  int w, h;
370  SDL_Surface *image;
371  SDL_Rect area;
372  Uint32 saved_flags;
373  Uint8  saved_alpha;
374 
375  /* Use the surface width and height expanded to powers of 2 */
376  w = powerOfTwo(surface->w);
377  h = powerOfTwo(surface->h);
378  texcoord[0] = 0.0f;                   /* Min X */
379  texcoord[1] = 0.0f;                   /* Min Y */
380  texcoord[2] = (GLfloat)surface->w / w;        /* Max X */
381  texcoord[3] = (GLfloat)surface->h / h;        /* Max Y */
382 
383  image = SDL_CreateRGBSurface(
384                               SDL_SWSURFACE,
385                               w, h,
386                               32,
387#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
388                               0x000000FF, 
389                               0x0000FF00, 
390                               0x00FF0000, 
391                               0xFF000000
392#else
393                               0xFF000000,
394                               0x00FF0000, 
395                               0x0000FF00, 
396                               0x000000FF
397#endif
398                               );
399  if ( image == NULL ) {
400    return 0;
401  }
402 
403  /* Save the alpha blending attributes */
404  saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
405  saved_alpha = surface->format->alpha;
406  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
407    SDL_SetAlpha(surface, 0, 0);
408  }
409 
410  /* Copy the surface into the GL texture image */
411  area.x = 0;
412  area.y = 0;
413  area.w = surface->w;
414  area.h = surface->h;
415  SDL_BlitSurface(surface, &area, image, &area);
416 
417  /* Restore the alpha blending attributes */
418  if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
419    SDL_SetAlpha(surface, saved_flags, saved_alpha);
420  }
421 
422  /* Create an OpenGL texture for the image */
423  glGenTextures(1, &texture);
424  glBindTexture(GL_TEXTURE_2D, texture);
425  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
426  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
427  glTexImage2D(GL_TEXTURE_2D,
428               0,
429               GL_RGBA,
430               w, h,
431               0,
432               GL_RGBA,
433               GL_UNSIGNED_BYTE,
434               image->pixels);
435  SDL_FreeSurface(image); /* No longer needed */
436 
437  return texture;
438}
439
440
441bool GLFont::checkVersion(void)
442{
443  SDL_version compile_version;
444  SDL_version link_version;
445  TTF_VERSION(&compile_version);
446  link_version = *TTF_Linked_Version();
447
448  if (compile_version.major == link_version.major &&
449      compile_version.minor == link_version.minor &&
450      compile_version.patch == link_version.patch)
451    {
452      return true;
453    }
454  else
455    {
456      PRINTF(2)("compiled with SDL_ttf version: %d.%d.%d\n", 
457                compile_version.major,
458                compile_version.minor,
459                compile_version.patch);
460     
461      PRINTF(2)("running with SDL_ttf version: %d.%d.%d\n", 
462                link_version.major,
463                link_version.minor,
464                link_version.patch);
465      return false;
466    }
467}
468
469
470
471/* Quick utility function for texture creation */
472int GLFont::powerOfTwo(int input)
473{
474  int value = 1;
475 
476  while ( value < input ) {
477    value <<= 1;
478  }
479  return value;
480}
481
482
483
484void GLFont::debug(void)
485{
486
487  // print the loaded font's style
488  int style;
489  style=TTF_GetFontStyle(this->font);
490  PRINTF(0)("The font style is:");
491  if(style==TTF_STYLE_NORMAL)
492    PRINTF(0)(" normal");
493  else {
494    if(style&TTF_STYLE_BOLD)
495      PRINTF(0)(" bold");
496    if(style&TTF_STYLE_ITALIC)
497      PRINTF(0)(" italic");
498    if(style&TTF_STYLE_UNDERLINE)
499      PRINTF(0)(" underline");
500  }
501  PRINTF(0)("\n");
502
503
504}
Note: See TracBrowser for help on using the repository browser.