[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 | |
---|
| 32 | Font::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] | 44 | Font::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] | 58 | Font::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 | */ |
---|
| 83 | Font::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] | 101 | Font::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 | */ |
---|
| 113 | Font::~Font() |
---|
[8751] | 114 | { } |
---|
[5343] | 115 | |
---|
[8761] | 116 | Font& 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 | */ |
---|
| 128 | void 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] | 140 | FontDataPointer Font::defaultFontData(NULL); |
---|
[5343] | 141 | |
---|
| 142 | /** |
---|
[8761] | 143 | * @brief initializes the default font |
---|
| 144 | */ |
---|
| 145 | void 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 | /** |
---|
[8751] | 154 | * @brief sets The Font. |
---|
[5343] | 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 | */ |
---|
[8761] | 158 | bool Font::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize) |
---|
[5343] | 159 | { |
---|
[8761] | 160 | this->data = FontDataPointer (new FontData()); |
---|
[8764] | 161 | bool retVal = this->data->loadFontFromTTF(fontFile, renderSize); |
---|
| 162 | if (!retVal) |
---|
[8761] | 163 | this->data = Font::defaultFontData; |
---|
[5347] | 164 | |
---|
[8766] | 165 | this->setTexture(this->data->textureData()); |
---|
[8764] | 166 | return retVal; |
---|
[5343] | 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
[7449] | 170 | * @brief loads a font From an XPM-array. |
---|
[5343] | 171 | * @param xpmArray the array of the XPM to load the font from. |
---|
| 172 | */ |
---|
| 173 | bool Font::loadFontFromSDL_Surface(SDL_Surface* surface) |
---|
| 174 | { |
---|
[8761] | 175 | this->data = FontDataPointer (new FontData()); |
---|
[8764] | 176 | bool retVal = this->data->loadFontFromSDL_Surface(surface); |
---|
| 177 | if (!retVal) |
---|
[8761] | 178 | this->data = Font::defaultFontData; |
---|
[5347] | 179 | |
---|
[8766] | 180 | this->setTexture(this->data->textureData()); |
---|
[8764] | 181 | return retVal; |
---|
[5343] | 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | /** |
---|
[8751] | 186 | * @brief sets a specific data->renderStyle |
---|
| 187 | * @param data->renderStyle the Style to render: a string (char-array) containing: |
---|
[5347] | 188 | * i: italic, b: bold, u, underline |
---|
[5343] | 189 | */ |
---|
[7221] | 190 | void Font::setStyle(const std::string& renderStyle) |
---|
[5343] | 191 | { |
---|
[8765] | 192 | /// FIXME |
---|
| 193 | //this->data->setStyle(renderStyle); |
---|
[8764] | 194 | } |
---|
[5343] | 195 | |
---|
[8764] | 196 | |
---|
| 197 | void Font::setTexture(const TextureDataPointer& texDataPointer) |
---|
| 198 | { |
---|
| 199 | this->setDiffuseMap(texDataPointer); |
---|
[5343] | 200 | } |
---|
| 201 | |
---|
[8764] | 202 | |
---|
[5768] | 203 | /** |
---|
[8316] | 204 | * @brief creates and exports an Image, that has all the characters |
---|
[5768] | 205 | * stored in a Array (as an image) |
---|
| 206 | * @param fileName the File to write the image into. |
---|
| 207 | */ |
---|
[8765] | 208 | void Font::createAsciiImage(const std::string& ttfFile, const std::string& fileName, unsigned int size) |
---|
[5343] | 209 | { |
---|
[8765] | 210 | TTF_Font* fontTTF = TTF_OpenFont(ttfFile.c_str(), size); |
---|
| 211 | |
---|
| 212 | if (fontTTF == NULL) |
---|
[5343] | 213 | return; |
---|
[8765] | 214 | int height = TTF_FontHeight(fontTTF); |
---|
[5343] | 215 | |
---|
| 216 | // |
---|
| 217 | // Surface definition. |
---|
| 218 | SDL_Rect tmpRect; // this represents a Rectangle for blitting. |
---|
| 219 | SDL_Surface* tmpSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
[7221] | 220 | height*size, height*size, |
---|
| 221 | 32, |
---|
[5343] | 222 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
[7221] | 223 | 0x000000FF, |
---|
| 224 | 0x0000FF00, |
---|
| 225 | 0x00FF0000, |
---|
| 226 | 0xFF000000 |
---|
[5343] | 227 | #else |
---|
[7221] | 228 | 0xFF000000, |
---|
| 229 | 0x00FF0000, |
---|
| 230 | 0x0000FF00, |
---|
| 231 | 0x000000FF |
---|
[5343] | 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; |
---|
[8765] | 244 | SDL_Color white = {255, 255, 255}; |
---|
| 245 | glyphSurf = TTF_RenderGlyph_Blended(fontTTF, posX+size*posY, white); |
---|
| 246 | |
---|
[5343] | 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 | } |
---|
[7221] | 258 | SDL_SaveBMP(tmpSurf, fileName.c_str()); |
---|
[5343] | 259 | SDL_FreeSurface(tmpSurf); |
---|
[8765] | 260 | |
---|
| 261 | TTF_CloseFont(fontTTF); |
---|
[5343] | 262 | } |
---|
| 263 | |
---|
| 264 | |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | /** |
---|
[8316] | 268 | * @brief a simple function to get some interesting information about this class |
---|
[5343] | 269 | */ |
---|
[8761] | 270 | void Font::debug() const |
---|
[5343] | 271 | { |
---|
[8761] | 272 | Material::debug(); |
---|
| 273 | |
---|
[8766] | 274 | PRINT(0)("TEST %p and %p\n", this->data.get(), this->data->textureData().get()); |
---|
[5343] | 275 | // print the loaded font's style |
---|
[8765] | 276 | /* int style = TTF_STYLE_NORMAL; |
---|
[8751] | 277 | if (likely(this->data->fontTTF != NULL)) |
---|
| 278 | style = TTF_GetFontStyle(this->data->fontTTF); |
---|
[5343] | 279 | PRINTF(0)("The font style is:"); |
---|
| 280 | if(style==TTF_STYLE_NORMAL) |
---|
| 281 | PRINTF(0)(" normal"); |
---|
[7221] | 282 | else |
---|
| 283 | { |
---|
[5343] | 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) |
---|
[8765] | 289 | PRINTF(0)(" underline");*/ |
---|
| 290 | // } |
---|
[5343] | 291 | PRINTF(0)("\n"); |
---|
| 292 | } |
---|