#include <text.h>
Inheritance diagram for Text:
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 |
Font * | font |
Color | color |
bool | shadowed |
uint | bg_offset |
uint | max_width |
Definition at line 28 of file text.h.
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:
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 |
void Text::DrawCenterOnMap | ( | int | x, | |
int | y | |||
) | const |
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 |
const std::string & Text::GetText | ( | ) | const |
int Text::GetWidth | ( | ) | const |
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 | ) |
void Text::SetColor | ( | const Color & | new_color | ) |
void Text::SetMaxWidth | ( | uint | max_w | ) |
Surface Text::background [private] |
uint Text::bg_offset [private] |
Color Text::color [private] |
Font* Text::font [private] |
uint Text::max_width [private] |
bool Text::shadowed [private] |
Surface Text::surf [private] |