00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <exception>
00021 #include <SDL_image.h>
00022 #include <SDL_video.h>
00023 #include <iostream>
00024 #include <string>
00025 #include "font.h"
00026 #include "colors.h"
00027 #include "../game/config.h"
00028 #include "../include/app.h"
00029 #include "../map/map.h"
00030 #include "../tool/error.h"
00031 #include "../tool/file_tools.h"
00032
00033 Font* Font::FONT_ARRAY[] = {NULL, NULL, NULL, NULL, NULL, NULL};
00034 Font* Font::FONT_ARRAY_BOLD[] = {NULL, NULL, NULL, NULL, NULL, NULL};
00035 Font* Font::FONT_ARRAY_ITALIC[] = {NULL, NULL, NULL, NULL, NULL, NULL};
00036
00037
00038
00039
00040
00041 const int Font::FONT_SIZE[] = {40, 32, 24, 16, 12, 8};
00042 const int Font::FONT_HUGE = 0;
00043 const int Font::FONT_LARGE = 1;
00044 const int Font::FONT_BIG = 2;
00045 const int Font::FONT_NORMAL = 3;
00046 const int Font::FONT_SMALL = 4;
00047 const int Font::FONT_TINY = 5;
00048
00049
00050 const int Font::NORMAL = 0;
00051 const int Font::BOLD = 1;
00052 const int Font::ITALIC = 2;
00053
00054 Font* Font::GetInstance(int type, int font_style) {
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 }
00077
00078 Font::Font(int size){
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 }
00085
00086 Font::~Font(){
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
00098 surface_text_table.erase(it->first);
00099 }
00100 }
00101
00102 bool Font::Load (const std::string& filename, int size) {
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 }
00119
00120 void Font::SetBold()
00121 {
00122 TTF_SetFontStyle(m_font, TTF_STYLE_BOLD);
00123 }
00124
00125 void Font::SetItalic()
00126 {
00127 TTF_SetFontStyle(m_font, TTF_STYLE_ITALIC);
00128 }
00129
00130 void Font::Write(const Point2i &pos, Surface &surface){
00131 AppWormux::GetInstance()->video.window.Blit(surface, pos);
00132
00133
00134 world.ToRedrawOnScreen( Rectanglei(pos, surface.GetSize()) );
00135 }
00136
00137 void Font::WriteLeft(const Point2i &pos, const std::string &txt, const Color &color){
00138 Surface surface(Render(txt, color, true));
00139 Write(pos, surface);
00140 }
00141
00142 void Font::WriteLeftBottom(const Point2i &pos, const std::string &txt,
00143 const Color &color){
00144 Surface surface(Render(txt, color, true));
00145 Write(pos - Point2i(0, surface.GetHeight()), surface);
00146 }
00147
00148 void Font::WriteRight(const Point2i &pos, const std::string &txt,
00149 const Color &color){
00150 Surface surface(Render(txt, color, true));
00151 Write(pos - Point2i(surface.GetWidth(), 0), surface);
00152 }
00153
00154 void Font::WriteCenter (const Point2i &pos, const std::string &txt,
00155 const Color &color){
00156 Surface surface(Render(txt, color, true));
00157 Write(pos - Point2i(surface.GetWidth()/2, surface.GetHeight()), surface);
00158 }
00159
00160 void Font::WriteCenterTop(const Point2i &pos, const std::string &txt,
00161 const Color &color){
00162 Surface surface(Render(txt, color, true));
00163 Write(pos - Point2i(surface.GetWidth()/2, 0), surface);
00164 }
00165
00166 Surface Font::CreateSurface(const std::string &txt, const Color &color){
00167 return Surface( TTF_RenderUTF8_Blended(m_font, txt.c_str(), color.GetSDLColor()) );
00168 }
00169
00170 Surface Font::Render(const std::string &txt, const Color &color, bool cache){
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
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 }
00192
00193 int Font::GetWidth (const std::string &txt){
00194 int width=-1;
00195
00196 TTF_SizeUTF8(m_font, txt.c_str(), &width, NULL);
00197
00198 return width;
00199 }
00200
00201 int Font::GetHeight (){
00202 return TTF_FontHeight(m_font);
00203 }
00204
00205 int Font::GetHeight (const std::string &str){
00206 int height=-1;
00207
00208 TTF_SizeUTF8(m_font, str.c_str(), NULL, &height);
00209
00210 return height;
00211 }
00212
00213 Point2i Font::GetSize(const std::string &txt){
00214 return Point2i(GetWidth(txt), GetHeight(txt));
00215 }