Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5336 in orxonox.OLD for trunk/src/lib/graphics


Ignore:
Timestamp:
Oct 9, 2005, 12:54:09 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: implemented default font.

Location:
trunk/src/lib/graphics
Files:
2 edited

Legend:

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

    r5306 r5336  
    2929#include <stdio.h>
    3030#include <string.h>
     31
     32#ifdef HAVE_SDL_IMAGE_H
     33#include <SDL_image.h>
     34#else
     35#include <SDL/SDL_image.h>
     36#endif
     37#include "font.xpm"
     38
    3139
    3240#include "graphics_engine.h"
     
    333341  w = powerOfTwo(surface->w);
    334342  h = powerOfTwo(surface->h);
    335   if (texCoord)
     343  if (texCoord != NULL)
    336344    {
    337345      texCoord->minU = 0.0f;
     
    391399               GL_UNSIGNED_BYTE,
    392400               image->pixels);
    393   SDL_FreeSurface(image); /* No longer needed */
     401  SDL_FreeSurface(image); /* No longer needed the data */
    394402
    395403  return texture;
     
    416424////////////
    417425/**
    418  *  constructs a Font
     426 * constructs a Font
    419427 * @param fontFile the File to load the font from
    420428 * @param fontSize the Size of the Font in Pixels
    421  * @param r Red value of the Font.
    422  * @param g Green value of the Font.
    423  * @param b Blue value of the Font.
    424 */
     429 */
    425430Font::Font(const char* fontFile, unsigned int fontSize)
    426431{
     
    433438  this->setSize(fontSize);
    434439
    435   this->loadFont(fontFile);
     440  if (fontFile != NULL)
     441    this->loadFont(fontFile);
    436442
    437443  this->setStyle("c");//TTF_STYLE_NORMAL);
     
    439445  this->fastTextureID = this->createFastTexture();
    440446}
     447
     448/**
     449 * constructs a Font
     450 * @param fontFile the File to load the font from
     451 * @param fontSize the Size of the Font in Pixels
     452 */
     453Font::Font(char** xpmArray)
     454{
     455  this->setClassID(CL_FONT, "Font");
     456  // setting default values.
     457  this->font = NULL;
     458  this->glyphArray = NULL;
     459  this->fastTextureID = 0;
     460
     461//  this->setSize(fontSize);
     462
     463  if (xpmArray != NULL)
     464    this->loadFontFromXPMArray(xpmArray);
     465}
     466
    441467
    442468/**
    443469 * destructs a font
    444470 * this releases the memory a font uses to be opened.
    445 */
     471 * deletes the glLists, and the TTF-handler, if present.
     472 */
    446473Font::~Font()
    447474{
    448   // deleting the List of all Texts
     475  // deleting the Lists of all Texts
    449476
    450477  // deleting all Glyphs
    451   if (this->glyphArray)
     478  if (this->glyphArray != NULL)
    452479    {
    453480      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
    454         delete this->glyphArray[i];
     481      {
     482        if (this->glyphArray[i] != NULL)
     483        {
     484          glDeleteLists(this->glyphArray[i]->displayList, 1);
     485          delete this->glyphArray[i];
     486        }
     487      }
    455488      delete[] this->glyphArray;
    456489    }
     
    461494}
    462495
    463 /**
    464  *  sets The Font.
     496
     497/**
     498 * sets The Font.
    465499 * @param fontFile The file containing the font.
    466500 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
     
    489523
    490524/**
     525 * loads a font From an XPM-array.
     526 * @param xpmArray the array of the XPM to load the font from.
     527 */
     528bool Font::loadFontFromXPMArray(char** xpmArray)
     529{
     530  if (xpmArray == NULL)
     531    return false;
     532
     533  // loading to a texture.
     534  SDL_Surface* image = IMG_ReadXPMFromArray(xpmArray);
     535  if(image == NULL)
     536  {
     537    PRINTF(1)("loading from XPM-array failed: %s\n", IMG_GetError());
     538    return false;
     539  }
     540  TexCoord texCoord;
     541  this->fastTextureID = Text::loadTexture(image, &texCoord);
     542
     543  // initializing the Glyphs.
     544  if (!this->glyphArray)
     545  {
     546    float cx,cy;
     547    this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
     548    for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
     549    {
     550      this->glyphArray[i] = new Glyph;
     551      this->glyphArray[i]->displayList = glGenLists(1);
     552      cx=(float)(i%16)/16.0f;                  // X Position Of Current Character
     553      cy=(float)(i/16)/16.0f;                  // Y Position Of Current Character
     554      glNewList(this->glyphArray[i]->displayList, GL_COMPILE); // Start Building A List
     555       glBegin(GL_QUADS);                           // Use A Quad For Each Character
     556        glTexCoord2f(cx,1.0f-cy-0.0625f);            // Texture Coord (Bottom Left)
     557        glVertex2d(0,16);                            // Vertex Coord (Bottom Left)
     558        glTexCoord2f(cx+0.0625f,1.0f-cy-0.0625f);    // Texture Coord (Bottom Right)
     559        glVertex2i(16,16);                           // Vertex Coord (Bottom Right)
     560        glTexCoord2f(cx+0.0625f,1.0f-cy-0.001f);     // Texture Coord (Top Right)
     561        glVertex2i(16,0);                            // Vertex Coord (Top Right)
     562        glTexCoord2f(cx,1.0f-cy-0.001f);             // Texture Coord (Top Left)
     563        glVertex2i(0,0);                             // Vertex Coord (Top Left)
     564       glEnd();                                     // Done Building Our Quad (Character)
     565//       glTranslated(12,0,0);                        // Move To The Right Of The Character
     566      glEndList();                                 // Done Building The Display List
     567
     568      this->glyphArray[i]->width = 12;
     569    }
     570  }
     571  SDL_FreeSurface(image);
     572  return true;
     573}
     574
     575
     576/**
    491577 *  sets a specific renderStyle
    492578 * @param renderStyle the Style to render: a char-array containing:
     
    519605  this->fontSize = fontSize;
    520606}
     607
     608Font* Font::defaultFont = NULL;
     609/**
     610 * initializes the default font
     611 */
     612void Font::initDefaultFont()
     613{
     614  if (Font::defaultFont == NULL)
     615    Font::defaultFont = new Font(font_xpm);
     616}
     617
     618/**
     619 * deletes the default font
     620 */
     621void Font::removeDefaultFont()
     622{
     623  if (Font::defaultFont != NULL)
     624    delete Font::defaultFont;
     625  Font::defaultFont = NULL;
     626}
     627
    521628
    522629/**
     
    582689}
    583690
     691/**
     692 * creates a Fast-Texture of this Font
     693 */
    584694GLuint Font::createFastTexture()
    585695{
     
    878988
    879989      TextEngine::checkVersion();
     990      Font::initDefaultFont();
    880991    }
    881992  else
     
    8901001  if (TTF_WasInit())
    8911002    {
     1003      Font::removeDefaultFont();
    8921004      TTF_Quit();
    8931005    }
  • trunk/src/lib/graphics/text_engine.h

    r5306 r5336  
    2121
    2222#ifdef HAVE_SDL_TTF_H
    23 #include "SDL_ttf.h"
     23#include <SDL_ttf.h>
    2424#else
    25 #include "SDL/SDL_ttf.h"
     25#include <SDL/SDL_ttf.h>
    2626#endif
    2727
     
    4646#define FONT_NUM_COLORS              256                        //!< number of colors.
    4747
    48 #define FONT_HIGHEST_KNOWN_CHAR      128                        //!< The highest character known to the textEngine.
     48#define FONT_HIGHEST_KNOWN_CHAR      256                        //!< The highest character known to the textEngine.
    4949
    5050#define TEXT_DEFAULT_ALIGNMENT       TEXT_ALIGN_CENTER          //!< default alignment
     
    129129  void debug() const;
    130130
     131  // helpers.
     132  static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
     133  static int powerOfTwo(int input);
     134
    131135 private:
    132136   Text(Font* font = NULL, TEXT_RENDER_TYPE type = TEXT_RENDER_DYNAMIC);
    133137   void setFont(Font* font);
    134138
    135    static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
    136    static int powerOfTwo(int input);
    137139
    138140 private:
     
    163165  Font(const char* fontFile,
    164166       unsigned int fontSize = FONT_DEFAULT_SIZE);
     167  Font(char** xpmArray);
    165168
    166169  virtual ~Font();
     
    168171  // font
    169172  bool loadFont(const char* fontFile);
     173  bool loadFontFromXPMArray(char** xpmArray);
     174
    170175  void setSize(unsigned int fontSize);
    171176  void setStyle(const char* renderStyle);
     
    175180  /** @returns the texture to the fast-texture */
    176181  inline GLuint getFastTextureID() const { return fastTextureID; };
     182  /** @returns the default Font */
     183  inline static Font* getDefaultFont() { return Font::defaultFont; };
     184
     185  static void initDefaultFont();
     186  static void removeDefaultFont();
    177187
    178188 private:
     
    190200
    191201 private:
    192   // general purpose
    193   GLdouble      projMat[16];         //!< The Projection Matrix
    194 
     202  static Font*  defaultFont;         //!< a default font, that is used, if other fonts were unable to be loaded.
    195203  // information about the Font
    196204  TTF_Font*     font;                //!< The font we use for this.
Note: See TracChangeset for help on using the changeset viewer.