Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Jun 24, 2006, 2:16:12 AM (18 years ago)
Author:
bensch
Message:

merged the new Font-Implementation back here
merged with svn merge https://svn.orxonox.net/orxonox/branches/fontdata . -r8752:HEAD
no conflicts, naturally

File:
1 edited

Legend:

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

    r8751 r8761  
    1717
    1818#include "font.h"
    19 #include "text.h"
    2019
    2120#ifdef HAVE_SDL_IMAGE_H
     
    2423#include <SDL/SDL_image.h>
    2524#endif
     25
    2626#include "default_font.xpm"
    2727
    2828#include "debug.h"
    29 #include "stdlibincl.h"
    3029#include "compiler.h"
    31 using namespace std;
     30
     31
     32Font::Font()
     33  : data(Font::defaultFontData)
     34{
     35  this->init();
     36
     37}
    3238
    3339/**
     
    3743 */
    3844Font::Font(const std::string& fontFile, unsigned int renderSize)
    39   : data(new FontData())
     45  : data(Font::defaultFontData)
    4046{
    4147  this->init();
    4248
    43 
    44   this->data->renderSize = renderSize;
    45   this->setStyle("c");
    46 
    4749  if (!fontFile.empty())
    48     this->loadFontFromTTF(fontFile);
    49 }
     50    this->loadFontFromTTF(fontFile, renderSize);
     51}
     52
    5053
    5154/**
     
    5457 */
    5558Font::Font(const std::string& imageFile)
    56   : data(new FontData())
     59  : data(Font::defaultFontData)
    5760{
    5861  this->init();
     62
    5963  this->setName(imageFile);
    6064  //  this->setSize(fontSize);
     
    7882 */
    7983Font::Font(char** xpmArray)
    80   : data(new FontData())
     84  : data(Font::defaultFontData)
    8185{
    8286  this->init();
     
    9296  }
    9397  else
    94     PRINTF(1)("loading from surface failed: %s\n", IMG_GetError());
     98    PRINTF(1)("Loading from XPM-array failed: %s\n", IMG_GetError());
    9599}
    96100
     
    105109{ }
    106110
     111Font& Font::operator=(const Font& font)
     112{
     113  Material::operator=(font);
     114  this->data = font.data;
     115
     116  return *this;
     117};
     118
     119
    107120/**
    108121 * @brief initializes a Font (with default values)
     
    110123void Font::init()
    111124{
     125  this->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     126
    112127  this->setClassID(CL_FONT, "Font");
    113 }
    114 
     128  if (Font::defaultFontData.get() == NULL)
     129  {
     130     Font::initDefaultFont();
     131    this->data = Font::defaultFontData;
     132  }
     133}
     134
     135FontDataPointer Font::defaultFontData(NULL);
     136
     137/**
     138 * @brief initializes the default font
     139 */
     140void Font::initDefaultFont()
     141{
     142  // temporarily create a Font.
     143  Font::defaultFontData = FontDataPointer(new FontData);
     144  // apply the Data.
     145  printf("before: %p\n", defaultFontData.get());
     146  Font::defaultFontData = Font(font_xpm).data;
     147  printf("after: %p\n", defaultFontData.get());
     148}
    115149
    116150/**
     
    119153 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
    120154 */
    121 bool Font::loadFontFromTTF(const std::string& fontFile)
    122 {
    123   // checking for existent Font.
    124   if (this->data->fontTTF != NULL)
    125   {
    126     TTF_CloseFont(this->data->fontTTF);
    127     this->data->fontTTF = NULL;
    128   }
    129 
     155bool Font::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize)
     156{
     157  this->data = FontDataPointer (new FontData());
    130158
    131159  this->setName(fontFile);
    132   this->data->fontTTF = TTF_OpenFont(this->getName(), this->data->renderSize);
     160  this->data->fontTTF = TTF_OpenFont(fontFile.c_str(), renderSize);
     161  this->data->renderSize = renderSize;
    133162
    134163  if(this->data->fontTTF != NULL)
    135164  {
    136     this->createFastTexture();
    137     return (this->getTexture() != 0);
     165    this->setStyle("c");
     166    if (this->createFastTexture())
     167      return true;
     168    else
     169    {
     170      this->data = Font::defaultFontData;
     171      return false;
     172    }
    138173  }
    139174  else
    140175  {
    141176    PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
     177    this->data = Font::defaultFontData;
    142178    return false;
    143179  }
     
    151187bool Font::loadFontFromSDL_Surface(SDL_Surface* surface)
    152188{
    153   // loading to a texture.
    154189  if(surface == NULL)
    155190    return false;
    156191
    157   if (this->data->fontTTF != NULL)
    158   {
    159     TTF_CloseFont(this->data->fontTTF);
    160     this->data->fontTTF = NULL;
    161   }
     192  this->data = FontDataPointer (new FontData());
     193  // loading to a texture.
     194
    162195  bool hasAlpha;
    163   SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);
     196  SDL_Surface* newSurf = Texture::prepareSurface(surface, hasAlpha);
    164197  if (newSurf != NULL)
    165198  {
    166     this->setSurface(newSurf);
    167     this->setAlpha(hasAlpha);
     199    this->data->texData->setSurface(newSurf);
     200    this->data->texData->setAlpha(hasAlpha);
    168201    this->setTexture(Texture::loadTexToGL(newSurf));
     202  }
     203  else
     204  {
     205    this->data = Font::defaultFontData;
     206    return false;
    169207  }
    170208
     
    223261  //    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
    224262}
    225 
    226 Font* Font::defaultFont = NULL;
    227263
    228264/**
     
    283319  SDL_SaveBMP(tmpSurf, fileName.c_str());
    284320  SDL_FreeSurface(tmpSurf);
    285 }
    286 
    287 /**
    288  * @brief initializes the default font
    289  */
    290 void Font::initDefaultFont()
    291 {
    292   if (Font::defaultFont == NULL)
    293     Font::defaultFont = new Font(font_xpm);
    294 }
    295 
    296 /**
    297  * @brief deletes the default font
    298  */
    299 void Font::removeDefaultFont()
    300 {
    301   if (Font::defaultFont != NULL)
    302     delete Font::defaultFont;
    303   Font::defaultFont = NULL;
    304321}
    305322
     
    473490//       sprintf( outName, "%s-glyphs.bmp", this->getName());
    474491//       SDL_SaveBMP(tmpSurf, outName);
    475   this->setAlpha(true);
    476   if (this->setSurface(tmpSurf))
    477     (this->setTexture(Texture::loadTexToGL(tmpSurf)));
     492  this->data->texData->setAlpha(true);
     493  if (this->data->texData->setSurface(tmpSurf))
     494    this->setTexture(Texture::loadTexToGL(tmpSurf));
    478495  return true;
    479496}
     497
     498/**
     499 * @brief the Internal implementation of setting up the Texture.
     500 * @param texture the Texture to load.
     501 * @returns true on success, false otherwise.
     502 */
     503bool Font::setTexture(GLuint texture)
     504{
     505  bool retVal = this->data->texData->setTexture(texture);
     506  this->setDiffuseMap(data->texData, 0);
     507  printf("this->texture %d\n", texture);
     508  //printf(this->getT)
     509  this->debug();
     510  return retVal;
     511};
    480512
    481513/**
     
    569601 * @brief a simple function to get some interesting information about this class
    570602 */
    571 void Font::debug()
    572 {
     603void Font::debug() const
     604{
     605  Material::debug();
     606
     607  PRINT(0)("TEST %p and %p\n", this->data.get(), this->data->texData.get());
    573608  // print the loaded font's style
    574609  int style = TTF_STYLE_NORMAL;
Note: See TracChangeset for help on using the changeset viewer.