Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: notifier notifies

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