Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/text_engine/font.cc @ 9718

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

more thoughts on Resources, and soon going to adapt… i think i've got a clue :)

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