Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5347 in orxonox.OLD for trunk/src/lib/graphics/text_engine/font.cc


Ignore:
Timestamp:
Oct 10, 2005, 3:07:04 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: building Graphics as a lib names libORXgraphics.a

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/text_engine/font.cc

    r5343 r5347  
    2424#include <SDL/SDL_image.h>
    2525#endif
    26 #include "font.xpm"
     26#include "default_font.xpm"
    2727
    2828#include "debug.h"
     
    3232
    3333/**
    34  * constructs a Font
     34 * constructs a Font out of a TTF-FIle
    3535 * @param fontFile the File to load the font from
    3636 * @param fontSize the Size of the Font in Pixels
     
    4141
    4242  this->setSize(fontSize);
     43  this->setStyle("c");
    4344
    4445  if (fontFile != NULL)
    4546    this->loadFont(fontFile);
    46 
    47   this->setStyle("c");//TTF_STYLE_NORMAL);
    48 
    49   this->fastTextureID = this->createFastTexture();
    50 
    51 
    52 //  this->createAsciiImage("test.bmp");
     47}
     48
     49/**
     50 * constructs a Font out of an ImageFile
     51 * @param imageFile the ImageFile to load the Font From.
     52 */
     53Font::Font(const char* imageFile)
     54{
     55  this->init();
     56  this->setName(imageFile);
     57  //  this->setSize(fontSize);
     58  SDL_Surface* image = NULL;
     59  if (imageFile != NULL)
     60    image = IMG_Load(imageFile);
     61  else
     62    return;
     63  if (image != NULL)
     64  {
     65    this->loadFontFromSDL_Surface(image);
     66    SDL_FreeSurface(image);
     67  }
     68  else
     69    PRINTF(1)("loading from surface %s failed: %s\n", imageFile, IMG_GetError());
    5370}
    5471
    5572/**
    5673 * constructs a Font
    57  * @param fontFile the File to load the font from
    58  * @param fontSize the Size of the Font in Pixels
     74 * @param xpmArray the xpm-ARRAY to load the font from
    5975 */
    6076Font::Font(char** xpmArray)
    6177{
    6278  this->init();
    63 
     79  this->setName("XPM-array-font");
    6480  //  this->setSize(fontSize);
    6581  SDL_Surface* image = NULL;
     
    7389  else
    7490    PRINTF(1)("loading from surface failed: %s\n", IMG_GetError());
    75 
    7691}
    7792
     
    91106      if (this->glyphArray[i] != NULL)
    92107      {
    93         glDeleteLists(this->glyphArray[i]->displayList, 1);
     108        if (this->glyphArray[i]->displayList != 0)
     109          glDeleteLists(this->glyphArray[i]->displayList, 1);
    94110        delete this->glyphArray[i];
    95111      }
     
    97113    delete[] this->glyphArray;
    98114  }
     115
     116  //! @todo check if we really do not need to delete the fastTextureID here.
     117//   if (this->fastTextureID != 0)
     118//     if(glIsTexture(this->fastTextureID))
     119//       glDeleteTextures(1, &this->fastTextureID);
    99120
    100121  // erease this font out of the memory.
     
    123144bool Font::loadFont(const char* fontFile)
    124145{
    125   if (!this->getName())
    126   {
    127     this->setName(fontFile);
    128 
    129     this->font = TTF_OpenFont(this->getName(), this->fontSize);
    130     if(!this->font)
    131     {
    132       PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
     146  // checking for existent Font.
     147  if (this->font != NULL)
     148  {
     149    TTF_CloseFont(this->font);
     150    this->font = NULL;
     151  }
     152  if (this->fastTextureID != 0)
     153  {
     154    if(glIsTexture(this->fastTextureID))
     155      glDeleteTextures(1, &this->fastTextureID);
     156    this->fastTextureID = 0;
     157  }
     158
     159  this->setName(fontFile);
     160  this->font = TTF_OpenFont(this->getName(), this->fontSize);
     161
     162  if(this->font != NULL)
     163  {
     164    this->fastTextureID = this->createFastTexture();
     165    if (this->fastTextureID != 0)
     166      return true;
     167    else
    133168      return false;
    134     }
    135     else
    136       return true;
    137169  }
    138170  else
    139171  {
    140     PRINTF(2)("Font already initialized, unable to change it now.\n");
     172    PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
    141173    return false;
    142174  }
     175
    143176}
    144177
     
    152185  if(surface == NULL)
    153186    return false;
     187
     188  if (this->font != NULL)
     189  {
     190    TTF_CloseFont(this->font);
     191    this->font = NULL;
     192  }
     193  if (this->fastTextureID != 0)
     194  {
     195    if(glIsTexture(this->fastTextureID))
     196      glDeleteTextures(1, &this->fastTextureID);
     197    this->fastTextureID = 0;
     198  }
     199
    154200  this->fastTextureID = Text::loadTexture(surface, NULL);
    155201
     
    195241/**
    196242 *  sets a specific renderStyle
    197  * @param renderStyle the Style to render: a char-array containing:
    198    i: italic, b: bold, u, underline
     243 * @param renderStyle the Style to render: a string (char-array) containing:
     244 *   i: italic, b: bold, u, underline
    199245 */
    200246void Font::setStyle(const char* renderStyle)
     
    212258  if (likely(this->font != NULL))
    213259    TTF_SetFontStyle(this->font, this->renderStyle);
    214   else
    215     PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
     260//  else
     261//    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
    216262}
    217263
     
    363409{
    364410  Glyph* rg = new Glyph;
     411  rg->displayList = 0;
    365412  rg->character = character;
    366413  if (likely (this->font!= NULL))
     
    537584 * @returns the optimal size to use as the texture size
    538585
    539    @todo: this algorithm can be a lot more faster, althought it does
     586   @todo: this algorithm can be a lot faster, althought it does
    540587   not really matter within the init-context, and 128 glyphs.
    541588
     
    545592int Font::findOptimalFastTextureSize()
    546593{
     594  if (this->glyphArray == NULL)
     595    return 0;
     596
    547597  int i;
    548598  int x,y; // the counters
     
    555605  {
    556606    x = 0; y = 0;
    557     for (i = 0; i <= FONT_HIGHEST_KNOWN_CHAR; i++)
     607    for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
    558608    {
    559609      if((tmpGlyph = this->glyphArray[i]) != NULL)
Note: See TracChangeset for help on using the changeset viewer.