Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/text_engine/font.cc @ 8767

Last change on this file since 8767 was 8767, checked in by bensch, 18 years ago

font: less debug

File size: 6.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 "font.h"
19
20#ifdef HAVE_SDL_IMAGE_H
21#include <SDL_image.h>
22#else
23#include <SDL/SDL_image.h>
24#endif
25
26#include "default_font.xpm"
27
28#include "debug.h"
29#include "compiler.h"
30
31
32Font::Font()
33    : data(Font::defaultFontData)
34{
35  this->init();
36
37}
38
39/**
40 * @brief constructs a Font out of a TTF-FIle
41 * @param fontFile the File to load the font from
42 * @param fontSize the Size of the Font in Pixels
43 */
44Font::Font(const std::string& fontFile, unsigned int renderSize)
45    : data(Font::defaultFontData)
46{
47  this->init();
48
49  if (!fontFile.empty())
50    this->loadFontFromTTF(fontFile, renderSize);
51}
52
53
54/**
55 * @brief constructs a Font out of an ImageFile
56 * @param imageFile the ImageFile to load the Font From.
57 */
58Font::Font(const std::string& imageFile)
59    : data(Font::defaultFontData)
60{
61  this->init();
62
63  this->setName(imageFile);
64  //  this->setSize(fontSize);
65  SDL_Surface* image = NULL;
66  if (!imageFile.empty())
67    image = IMG_Load(imageFile.c_str());
68  else
69    return;
70  if (image != NULL)
71  {
72    this->loadFontFromSDL_Surface(image);
73    SDL_FreeSurface(image);
74  }
75  else
76    PRINTF(1)("loading from surface %s failed: %s\n", imageFile.c_str(), IMG_GetError());
77}
78
79/**
80 * @brief constructs a Font
81 * @param xpmArray the xpm-ARRAY to load the font from
82 */
83Font::Font(char** xpmArray)
84    : data(Font::defaultFontData)
85{
86  this->init();
87  this->setName("XPM-array-font");
88  //  this->setSize(fontSize);
89  SDL_Surface* image = NULL;
90  if (xpmArray != NULL)
91    image = IMG_ReadXPMFromArray(xpmArray);
92  if (image != NULL)
93  {
94    this->loadFontFromSDL_Surface(image);
95    SDL_FreeSurface(image);
96  }
97  else
98    PRINTF(1)("Loading from XPM-array failed: %s\n", IMG_GetError());
99}
100
101Font::Font(const Font& font)
102{
103  this->init();
104  *this = font;
105}
106
107/**
108 * @brief destructs a font
109 *
110 * this releases the memory a font uses to be opened.
111 * deletes the glLists, and the TTF-handler, if present.
112 */
113Font::~Font()
114{ }
115
116Font& Font::operator=(const Font& font)
117{
118  Material::operator=(font);
119  this->data = font.data;
120
121  return *this;
122};
123
124
125/**
126 * @brief initializes a Font (with default values)
127 */
128void Font::init()
129{
130  this->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
131
132  this->setClassID(CL_FONT, "Font");
133  if (Font::defaultFontData.get() == NULL)
134  {
135    Font::initDefaultFont();
136    this->data = Font::defaultFontData;
137  }
138}
139
140FontDataPointer Font::defaultFontData(NULL);
141
142/**
143 * @brief initializes the default font
144 */
145void Font::initDefaultFont()
146{
147  // temporarily create a Font.
148  Font::defaultFontData = FontDataPointer(new FontData);
149  // apply the Data.
150  Font::defaultFontData = Font(font_xpm).data;
151}
152
153/**
154 * @brief sets The Font.
155 * @param fontFile The file containing the font.
156 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
157 */
158bool Font::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize)
159{
160  this->data = FontDataPointer (new FontData());
161  bool retVal = this->data->loadFontFromTTF(fontFile, renderSize);
162  if (!retVal)
163    this->data = Font::defaultFontData;
164
165  this->setTexture(this->data->textureData());
166  return retVal;
167}
168
169/**
170 * @brief loads a font From an XPM-array.
171 * @param xpmArray the array of the XPM to load the font from.
172 */
173bool Font::loadFontFromSDL_Surface(SDL_Surface* surface)
174{
175  this->data = FontDataPointer (new FontData());
176  bool retVal = this->data->loadFontFromSDL_Surface(surface);
177  if (!retVal)
178    this->data = Font::defaultFontData;
179
180  this->setTexture(this->data->textureData());
181  return retVal;
182}
183
184
185/**
186 * @brief sets a specific data->renderStyle
187 * @param data->renderStyle the Style to render: a string (char-array) containing:
188 *   i: italic, b: bold, u, underline
189 */
190void Font::setStyle(const std::string& renderStyle)
191{
192  /// FIXME
193  //this->data->setStyle(renderStyle);
194}
195
196
197void Font::setTexture(const TextureDataPointer& texDataPointer)
198{
199  this->setDiffuseMap(texDataPointer);
200}
201
202
203/**
204 * @brief creates and exports an Image, that has all the characters
205 * stored in a Array (as an image)
206 * @param fileName the File to write the image into.
207 */
208void Font::createAsciiImage(const std::string& ttfFile, const std::string& fileName, unsigned int size)
209{
210  TTF_Font* fontTTF = TTF_OpenFont(ttfFile.c_str(), size);
211
212  if (fontTTF == NULL)
213    return;
214  int height = TTF_FontHeight(fontTTF);
215
216  //
217  // Surface definition.
218  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
219  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_SWSURFACE,
220                          height*size, height*size,
221                          32,
222#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
223                          0x000000FF,
224                          0x0000FF00,
225                          0x00FF0000,
226                          0xFF000000
227#else
228                          0xFF000000,
229                          0x00FF0000,
230                          0x0000FF00,
231                          0x000000FF
232#endif
233                                              );
234  tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h;
235  SDL_SetClipRect(tmpSurf, &tmpRect);
236
237  int posX, posY;
238  // all the interessting Glyphs
239  for (posY = 0; posY < 16; posY++)
240  {
241    for (posX = 0; posX < 16; posX++)
242    {
243      SDL_Surface* glyphSurf = NULL;
244      SDL_Color white = {255, 255, 255};
245      glyphSurf = TTF_RenderGlyph_Blended(fontTTF, posX+size*posY, white);
246
247      if( glyphSurf != NULL )
248      {
249        tmpRect.x = height*posX;
250        tmpRect.y = height*posY;
251        SDL_SetAlpha(glyphSurf, 0, 0);
252
253        SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect);
254        SDL_FreeSurface(glyphSurf);
255      }
256    }
257  }
258  SDL_SaveBMP(tmpSurf, fileName.c_str());
259  SDL_FreeSurface(tmpSurf);
260
261  TTF_CloseFont(fontTTF);
262}
263
264
265
266
267/**
268 * @brief a simple function to get some interesting information about this class
269 */
270void Font::debug() const
271{
272  Material::debug();
273
274  PRINT(0)("TEST %p and %p\n", this->data.get(), this->data->textureData().get());
275  // print the loaded font's style
276/*  int style = TTF_STYLE_NORMAL;
277  if (likely(this->data->fontTTF != NULL))
278    style = TTF_GetFontStyle(this->data->fontTTF);
279  PRINTF(0)("The font style is:");
280  if(style==TTF_STYLE_NORMAL)
281    PRINTF(0)(" normal");
282  else
283  {
284    if(style&TTF_STYLE_BOLD)
285      PRINTF(0)(" bold");
286    if(style&TTF_STYLE_ITALIC)
287      PRINTF(0)(" italic");
288    if(style&TTF_STYLE_UNDERLINE)
289      PRINTF(0)(" underline");*/
290//  }
291  PRINTF(0)("\n");
292}
Note: See TracBrowser for help on using the repository browser.