Font Class Reference

#include <font.h>

Collaboration diagram for Font:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ~Font ()
bool Load (const std::string &filename, int size)
void WriteLeft (const Point2i &pos, const std::string &txt, const Color &color)
void WriteLeftBottom (const Point2i &pos, const std::string &txt, const Color &color)
void WriteRight (const Point2i &pos, const std::string &txt, const Color &color)
void WriteCenterTop (const Point2i &pos, const std::string &txt, const Color &color)
void WriteCenter (const Point2i &pos, const std::string &txt, const Color &color)
int GetWidth (const std::string &txt)
int GetHeight ()
int GetHeight (const std::string &txt)
Point2i GetSize (const std::string &txt)
Surface Render (const std::string &txt, const Color &color, bool cache=false)
Surface CreateSurface (const std::string &txt, const Color &color)
void SetBold ()
void SetItalic ()

Static Public Member Functions

static FontGetInstance (int type, int font_style=NORMAL)

Static Public Attributes

static const int FONT_HUGE = 0
static const int FONT_LARGE = 1
static const int FONT_BIG = 2
static const int FONT_NORMAL = 3
static const int FONT_SMALL = 4
static const int FONT_TINY = 5
static const int NORMAL = 0
static const int BOLD = 1
static const int ITALIC = 2

Private Types

typedef std::map< std::string,
Surface >::value_type 
txt_sample
typedef std::map< std::string,
Surface >::iterator 
txt_iterator

Private Member Functions

void Write (const Point2i &pos, Surface &surface)
 Font (int size)

Private Attributes

std::map< std::string, Surfacesurface_text_table
TTF_Font * m_font

Static Private Attributes

static const int FONT_SIZE [] = {40, 32, 24, 16, 12, 8}
static FontFONT_ARRAY [6] = {NULL, NULL, NULL, NULL, NULL, NULL}
static FontFONT_ARRAY_BOLD [6] = {NULL, NULL, NULL, NULL, NULL, NULL}
static FontFONT_ARRAY_ITALIC [6] = {NULL, NULL, NULL, NULL, NULL, NULL}

Detailed Description

Definition at line 31 of file font.h.


Member Typedef Documentation

typedef std::map<std::string, Surface>::iterator Font::txt_iterator [private]

Definition at line 37 of file font.h.

typedef std::map<std::string, Surface>::value_type Font::txt_sample [private]

Definition at line 35 of file font.h.


Constructor & Destructor Documentation

Font::Font ( int  size  )  [private]

Definition at line 78 of file font.cpp.

00078                   {
00079   m_font = NULL;
00080   bool ok = Load(Config::GetInstance()->GetTtfFilename(), size);
00081   
00082   if( !ok )
00083     Error("Error during initialisation of a font!");
00084 }

Here is the call graph for this function:

Here is the caller graph for this function:

Font::~Font (  ) 

Definition at line 86 of file font.cpp.

00086            {
00087   if( m_font != NULL ){
00088     TTF_CloseFont(m_font);
00089     m_font = NULL;
00090   }
00091   
00092   txt_iterator it;
00093   
00094   for( it = surface_text_table.begin(); 
00095        it != surface_text_table.end(); 
00096        ++it ){
00097     //SDL_FreeSurface(it->second);
00098     surface_text_table.erase(it->first);
00099   }
00100 }


Member Function Documentation

Surface Font::CreateSurface ( const std::string &  txt,
const Color color 
)

Definition at line 166 of file font.cpp.

00166                                                                    {
00167   return Surface( TTF_RenderUTF8_Blended(m_font, txt.c_str(), color.GetSDLColor()) );
00168 }

Here is the call graph for this function:

Here is the caller graph for this function:

int Font::GetHeight ( const std::string &  txt  ) 

Definition at line 205 of file font.cpp.

00205                                         { 
00206   int height=-1;
00207   
00208   TTF_SizeUTF8(m_font, str.c_str(), NULL, &height);
00209 
00210   return height;
00211 }

int Font::GetHeight (  ) 

Definition at line 201 of file font.cpp.

00201                     { 
00202   return TTF_FontHeight(m_font);
00203 }

Here is the caller graph for this function:

Font * Font::GetInstance ( int  type,
int  font_style = NORMAL 
) [static]

Definition at line 54 of file font.cpp.

00054                                                 {
00055   Font * font;
00056   if (FONT_ARRAY[type] == NULL) {
00057     switch(font_style) {
00058       case BOLD:
00059         font = FONT_ARRAY_BOLD[type] = new Font(FONT_SIZE[type]);
00060         font->SetBold();
00061         break;
00062       case ITALIC:
00063         font = FONT_ARRAY_ITALIC[type] = new Font(FONT_SIZE[type]);
00064         font->SetItalic();
00065         break;
00066       default:     font = FONT_ARRAY[type] = new Font(FONT_SIZE[type]); break;
00067     }
00068   } else {
00069     switch(font_style) {
00070       case BOLD:   font = FONT_ARRAY_BOLD[type]; break;
00071       case ITALIC: font = FONT_ARRAY_ITALIC[type]; break;
00072       default  :   font = FONT_ARRAY[type]; break;
00073     }
00074   }
00075   return font;
00076 }

Here is the call graph for this function:

Here is the caller graph for this function:

Point2i Font::GetSize ( const std::string &  txt  ) 

Definition at line 213 of file font.cpp.

00213                                          {
00214         return Point2i(GetWidth(txt), GetHeight(txt));
00215 }

Here is the call graph for this function:

Here is the caller graph for this function:

int Font::GetWidth ( const std::string &  txt  ) 

Definition at line 193 of file font.cpp.

00193                                        { 
00194   int width=-1;
00195   
00196   TTF_SizeUTF8(m_font, txt.c_str(), &width, NULL);
00197 
00198   return width;
00199 }

Here is the caller graph for this function:

bool Font::Load ( const std::string &  filename,
int  size 
)

Definition at line 102 of file font.cpp.

00102                                                     {
00103   bool ok = false;
00104 
00105   if( IsFileExist(filename) ){
00106       m_font = TTF_OpenFont(filename.c_str(), size);
00107       ok = (m_font != NULL);
00108   }
00109   
00110   if( !ok ){
00111       std::cout << "Error: Font " << filename << " can't be found!" << std::endl;
00112       return false;
00113   }
00114   
00115   TTF_SetFontStyle(m_font, TTF_STYLE_NORMAL);
00116 
00117   return true;
00118 }

Here is the call graph for this function:

Here is the caller graph for this function:

Surface Font::Render ( const std::string &  txt,
const Color color,
bool  cache = false 
)

Definition at line 170 of file font.cpp.

00170                                                                         {
00171   Surface surface;
00172   
00173   if( cache ){
00174     txt_iterator p = surface_text_table.find(txt);
00175     if( p == surface_text_table.end() ){ 
00176       if( surface_text_table.size() > 5 ){
00177         //SDL_FreeSurface( surface_text_table.begin()->second );
00178         surface_text_table.erase( surface_text_table.begin() );
00179       }
00180       surface = CreateSurface(txt, color);
00181       surface_text_table.insert( txt_sample(txt, surface) );
00182     } else {
00183       txt_iterator p = surface_text_table.find( txt );
00184       surface = p->second;
00185     }
00186   } else
00187     surface = CreateSurface(txt, color);
00188   
00189   assert( !surface.IsNull() );
00190   return surface;
00191 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Font::SetBold (  ) 

Definition at line 120 of file font.cpp.

00121 {
00122   TTF_SetFontStyle(m_font, TTF_STYLE_BOLD);
00123 }

Here is the caller graph for this function:

void Font::SetItalic (  ) 

Definition at line 125 of file font.cpp.

00126 {
00127   TTF_SetFontStyle(m_font, TTF_STYLE_ITALIC);
00128 }

Here is the caller graph for this function:

void Font::Write ( const Point2i pos,
Surface surface 
) [private]

Definition at line 130 of file font.cpp.

00130                                                     {
00131   AppWormux::GetInstance()->video.window.Blit(surface, pos);
00132                   
00133   // TODO: Remove this line! (and use GameFont instead of Font)
00134   world.ToRedrawOnScreen( Rectanglei(pos, surface.GetSize()) );
00135 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Font::WriteCenter ( const Point2i pos,
const std::string &  txt,
const Color color 
)

Definition at line 154 of file font.cpp.

00155                                             { 
00156   Surface surface(Render(txt, color, true));
00157   Write(pos - Point2i(surface.GetWidth()/2, surface.GetHeight()), surface);
00158 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Font::WriteCenterTop ( const Point2i pos,
const std::string &  txt,
const Color color 
)

Definition at line 160 of file font.cpp.

00161                                    {
00162         Surface surface(Render(txt, color, true));
00163         Write(pos - Point2i(surface.GetWidth()/2, 0), surface);
00164 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Font::WriteLeft ( const Point2i pos,
const std::string &  txt,
const Color color 
)

Definition at line 137 of file font.cpp.

00137                                                                                  {
00138   Surface surface(Render(txt, color, true));
00139   Write(pos, surface);
00140 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Font::WriteLeftBottom ( const Point2i pos,
const std::string &  txt,
const Color color 
)

Definition at line 142 of file font.cpp.

00143                                                 { 
00144   Surface surface(Render(txt, color, true));
00145   Write(pos - Point2i(0, surface.GetHeight()), surface);
00146 }

Here is the call graph for this function:

void Font::WriteRight ( const Point2i pos,
const std::string &  txt,
const Color color 
)

Definition at line 148 of file font.cpp.

00149                                            { 
00150   Surface surface(Render(txt, color, true));
00151   Write(pos - Point2i(surface.GetWidth(), 0), surface);
00152 }

Here is the call graph for this function:


Member Data Documentation

const int Font::BOLD = 1 [static]

Definition at line 59 of file font.h.

Font * Font::FONT_ARRAY = {NULL, NULL, NULL, NULL, NULL, NULL} [static, private]

Definition at line 39 of file font.h.

Font * Font::FONT_ARRAY_BOLD = {NULL, NULL, NULL, NULL, NULL, NULL} [static, private]

Definition at line 40 of file font.h.

Font * Font::FONT_ARRAY_ITALIC = {NULL, NULL, NULL, NULL, NULL, NULL} [static, private]

Definition at line 41 of file font.h.

const int Font::FONT_BIG = 2 [static]

Definition at line 53 of file font.h.

const int Font::FONT_HUGE = 0 [static]

Definition at line 51 of file font.h.

const int Font::FONT_LARGE = 1 [static]

Definition at line 52 of file font.h.

const int Font::FONT_NORMAL = 3 [static]

Definition at line 54 of file font.h.

const int Font::FONT_SIZE = {40, 32, 24, 16, 12, 8} [static, private]

Definition at line 38 of file font.h.

const int Font::FONT_SMALL = 4 [static]

Definition at line 55 of file font.h.

const int Font::FONT_TINY = 5 [static]

Definition at line 56 of file font.h.

const int Font::ITALIC = 2 [static]

Definition at line 60 of file font.h.

TTF_Font* Font::m_font [private]

Definition at line 44 of file font.h.

const int Font::NORMAL = 0 [static]

Definition at line 58 of file font.h.

std::map<std::string, Surface> Font::surface_text_table [private]

Definition at line 43 of file font.h.


The documentation for this class was generated from the following files:
Generated on Mon Jan 1 13:46:08 2007 for Wormux by  doxygen 1.4.7