Text Class Reference

#include <text.h>

Inheritance diagram for Text:

Inheritance graph
[legend]
Collaboration diagram for Text:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 Text (const std::string &new_txt, Color new_color=white_color, Font *new_font=NULL, bool shadowed=true)
 ~Text ()
void DrawCenter (int x, int y) const
void DrawCenter (const Point2i &position) const
void DrawTopLeft (int x, int y) const
void DrawTopLeft (const Point2i &position) const
void DrawTopRight (int x, int y) const
void DrawCenterTop (int x, int y) const
void DrawCenterOnMap (int x, int y) const
void DrawTopLeftOnMap (int x, int y) const
void DrawCenterTopOnMap (int x, int y) const
void Set (const std::string &new_txt)
const std::string & GetText () const
void SetColor (const Color &new_color)
void SetMaxWidth (uint max_w)
int GetWidth () const
int GetHeight () const

Private Member Functions

void Render ()
void RenderMultiLines ()

Private Attributes

Surface surf
Surface background
std::string txt
Fontfont
Color color
bool shadowed
uint bg_offset
uint max_width

Detailed Description

Definition at line 28 of file text.h.


Constructor & Destructor Documentation

Text::Text ( const std::string &  new_txt,
Color  new_color = white_color,
Font new_font = NULL,
bool  shadowed = true 
)

Definition at line 34 of file text.cpp.

00036 {
00037                                 
00038   if( new_font == NULL )
00039     new_font = Font::GetInstance(Font::FONT_SMALL);
00040         
00041   txt = new_txt;
00042   color = new_color;
00043   font = new_font;
00044   this->shadowed = shadowed;
00045 
00046   if( shadowed ){
00047     int width = font->GetWidth("x");
00048     bg_offset = (unsigned int)width/8; // shadow offset = 0.125ex
00049     if (bg_offset < 1) bg_offset = 1;
00050   }
00051   else {
00052     bg_offset = 0;
00053   }
00054   max_width = 0;
00055 
00056   Render();
00057 }

Here is the call graph for this function:

Text::~Text (  ) 

Definition at line 59 of file text.cpp.

00060 {
00061 }


Member Function Documentation

void Text::DrawCenter ( const Point2i position  )  const

Definition at line 219 of file text.cpp.

00220 {
00221   DrawCenter(position.GetX(), position.GetY());
00222 }

Here is the call graph for this function:

void Text::DrawCenter ( int  x,
int  y 
) const

Definition at line 214 of file text.cpp.

00215 { 
00216   DrawTopLeft(x - surf.GetWidth() / 2, y - surf.GetHeight() / 2);
00217 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::DrawCenterOnMap ( int  x,
int  y 
) const

Definition at line 263 of file text.cpp.

00264 {
00265   DrawTopLeftOnMap(x - surf.GetWidth()/2, y - surf.GetHeight()/2 );
00266 }

Here is the call graph for this function:

void Text::DrawCenterTop ( int  x,
int  y 
) const

Definition at line 229 of file text.cpp.

00230 { 
00231   DrawTopLeft( x - surf.GetWidth()/2, y);
00232 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::DrawCenterTopOnMap ( int  x,
int  y 
) const

Definition at line 268 of file text.cpp.

00269 {
00270   DrawTopLeftOnMap(x - surf.GetWidth()/2, y);
00271 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::DrawTopLeft ( const Point2i position  )  const

Definition at line 234 of file text.cpp.

00235 {
00236   if(txt == "") return;
00237 
00238   Rectanglei dst_rect(position, surf.GetSize());
00239   AppWormux * app = AppWormux::GetInstance();
00240 
00241   if(shadowed){
00242     Rectanglei shad_rect;
00243     
00244     shad_rect.SetPosition(dst_rect.GetPosition() + bg_offset);
00245     shad_rect.SetSize(background.GetWidth(), background.GetHeight() );
00246     
00247     app->video.window.Blit(background, shad_rect.GetPosition());
00248     app->video.window.Blit(surf, dst_rect.GetPosition());
00249                 
00250     world.ToRedrawOnScreen(Rectanglei(dst_rect.GetPosition(),
00251                                       shad_rect.GetSize() + bg_offset));
00252   }else{
00253     app->video.window.Blit(surf, dst_rect.GetPosition());
00254     world.ToRedrawOnScreen(dst_rect);
00255   }             
00256 }

Here is the call graph for this function:

void Text::DrawTopLeft ( int  x,
int  y 
) const

Definition at line 258 of file text.cpp.

00259 { 
00260   DrawTopLeft( Point2i(x, y) );
00261 }

Here is the caller graph for this function:

void Text::DrawTopLeftOnMap ( int  x,
int  y 
) const

Definition at line 273 of file text.cpp.

00274 {
00275   if(shadowed)
00276     AbsoluteDraw(background, Point2i(bg_offset + x, bg_offset + y) );
00277   AbsoluteDraw(surf, Point2i(x, y) );
00278 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::DrawTopRight ( int  x,
int  y 
) const

Definition at line 224 of file text.cpp.

00225 { 
00226   DrawTopLeft( x - surf.GetWidth(), y);
00227 }

Here is the call graph for this function:

Here is the caller graph for this function:

int Text::GetHeight (  )  const

Definition at line 296 of file text.cpp.

00297 {
00298   if(txt=="") return 0;
00299   return surf.GetHeight();
00300 }

Here is the call graph for this function:

Here is the caller graph for this function:

const std::string & Text::GetText (  )  const

Definition at line 199 of file text.cpp.

00200 {
00201   return txt;
00202 }

Here is the caller graph for this function:

int Text::GetWidth (  )  const

Definition at line 290 of file text.cpp.

00291 {
00292   if(txt=="") return 0;
00293   return surf.GetWidth();
00294 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::Render (  )  [private]

Definition at line 63 of file text.cpp.

00064 {
00065   if (txt=="") return;
00066 
00067   if (max_width != 0) {
00068     RenderMultiLines();
00069     return;
00070   }
00071 
00072   surf = font->CreateSurface(txt, color);
00073   if ( shadowed ) {
00074     background = font->CreateSurface(txt, black_color);
00075   }
00076 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::RenderMultiLines (  )  [private]

Definition at line 78 of file text.cpp.

00079 {
00080   if (txt=="") return;
00081 
00082   // Make a first try
00083   if (font->GetWidth(txt) < int(max_width)) {
00084     surf = font->CreateSurface(txt, color);
00085     if ( shadowed ) {
00086       background = font->CreateSurface(txt, black_color);
00087     }
00088     return;
00089   }
00090 
00091   // Cut the text on space
00092   std::vector<std::string> tokens;
00093   std::string::size_type old_pos = 0, current_pos = 0;
00094 
00095   while ( old_pos < txt.size() &&
00096           (current_pos = txt.find_first_of(" ", old_pos)) != std::string::npos )
00097     {
00098       std::string tmp = txt.substr(old_pos, current_pos-old_pos);
00099       if (tmp != " ") {
00100         tokens.push_back(tmp);
00101       }
00102       old_pos = current_pos+1;
00103     }
00104   tokens.push_back(txt.substr(old_pos));
00105 
00106   // Compute size
00107   std::vector<std::string> lines;
00108   uint index_lines = 0;
00109   uint index_word = 0;
00110 
00111   while (index_word < tokens.size()) 
00112     {
00113       if ( lines.size() == index_lines ) {
00114         // first word of a line
00115         lines.push_back(tokens.at(index_word));
00116 
00117       } else {
00118 
00119         if ( font->GetWidth(lines.at(index_lines)+" "+tokens.at(index_word)) > int(max_width) ) {
00120           
00121           // line will be too long : prepare next line!
00122           index_lines++;
00123           index_word--;
00124         } else {
00125           lines.at(index_lines) += " " + tokens.at(index_word);
00126         }
00127         
00128       }
00129       
00130       index_word++;
00131     }
00132   
00133   // really Render !
00134 
00135   // First, creating a destination surface
00136   Point2i size(max_width, (font->GetHeight()+2) * lines.size());
00137   surf.NewSurface(size, SDL_SWSURFACE|SDL_SRCALPHA, true);
00138   surf = surf.DisplayFormatAlpha();
00139 
00140   // Puting pixels of each image in destination surface
00141   surf.Lock();
00142 
00143   // for each lines
00144   for (uint i = 0; i < lines.size(); i++) {
00145     Surface tmp=(font->CreateSurface(lines.at(i), color)).DisplayFormatAlpha();
00146     tmp.Lock();
00147 
00148     // for each pixel lines of a source image
00149     for (int x=0; x < tmp.GetWidth() && x < int(max_width); x++) 
00150       { // for each pixel rows of a source image
00151         for (int y=0; y < tmp.GetHeight(); y++) 
00152           { 
00153             surf.PutPixel(x, ((font->GetHeight()+2)*i)+y, 
00154                           tmp.GetPixel(x, y));
00155           }
00156       }
00157     tmp.Unlock();
00158   }
00159   surf.Unlock();
00160 
00161   // Render the shadow !
00162   if (!shadowed) return;
00163 
00164   background.NewSurface(size, SDL_SWSURFACE|SDL_SRCALPHA, true);
00165   background = background.DisplayFormatAlpha();
00166 
00167   // Puting pixels of each image in destination surface
00168   background.Lock();
00169 
00170   // for each lines
00171   for (uint i = 0; i < lines.size(); i++) {
00172     Surface tmp=(font->CreateSurface(lines.at(i), black_color)).DisplayFormatAlpha();
00173     tmp.Lock();
00174 
00175     // for each pixel lines of a source image
00176     for (int x=0; x < tmp.GetWidth() && x < int(max_width); x++) 
00177       { // for each pixel rows of a source image
00178         for (int y=0; y < tmp.GetHeight(); y++) 
00179           { 
00180             background.PutPixel(x, ((font->GetHeight()+2)*i)+y, 
00181                                 tmp.GetPixel(x, y));
00182           }
00183       }
00184     tmp.Unlock();
00185   }
00186   background.Unlock();
00187 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::Set ( const std::string &  new_txt  ) 

Definition at line 189 of file text.cpp.

00190 {
00191   if(txt == new_txt)
00192     return;
00193 
00194   txt = new_txt;
00195         
00196   Render();
00197 }

Here is the call graph for this function:

Here is the caller graph for this function:

void Text::SetColor ( const Color new_color  ) 

Definition at line 204 of file text.cpp.

00205 {
00206   if(color == new_color)
00207     return;
00208 
00209   color = new_color;
00210         
00211   Render();
00212 }

Here is the call graph for this function:

void Text::SetMaxWidth ( uint  max_w  ) 

Definition at line 280 of file text.cpp.

00281 {
00282   if (max_width == max_w)
00283     return;
00284 
00285   max_width = max_w;
00286 
00287   Render();
00288 }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Surface Text::background [private]

Definition at line 31 of file text.h.

uint Text::bg_offset [private]

Definition at line 36 of file text.h.

Color Text::color [private]

Definition at line 34 of file text.h.

Font* Text::font [private]

Definition at line 33 of file text.h.

uint Text::max_width [private]

Definition at line 37 of file text.h.

bool Text::shadowed [private]

Definition at line 35 of file text.h.

Surface Text::surf [private]

Definition at line 30 of file text.h.

std::string Text::txt [private]

Definition at line 32 of file text.h.


The documentation for this class was generated from the following files:
Generated on Mon Jan 1 14:25:40 2007 for Wormux by  doxygen 1.4.7