Main Page   Packages   Class Hierarchy   Alphabetical List   Data Structures   File List   Namespace Members   Data Fields   Globals  

Font.cpp

Go to the documentation of this file.
00001 /*
00002  * $Id: Font.cpp,v 1.13 2002/08/07 07:16:15 virtualcasino Exp $
00003  */
00004 #include<Font.h>
00005 #include<Tokenizer.h>
00006 #include<string.h>
00007 #include<Exception.h>
00008 
00009 namespace clawsoft{
00010 
00011         Font::Font():GUIObject(){
00012                 setClassName("Font");
00013                 thefont = 0;
00014                 currentcolor.r = 0;
00015                 currentcolor.g = 0;
00016                 currentcolor.b = 0;
00017                 size(0);
00018         }
00019 
00020         Font::Font(const char *fname, int s):GUIObject(){
00021                 setClassName("Font");
00022                 currentcolor.r = 0;
00023                 currentcolor.g = 0;
00024                 currentcolor.b = 0;
00025                 thefont = 0;
00026                 load(fname, s);
00027         }
00028 
00029         Font::Font(String fname, int s):GUIObject(){
00030                 setClassName("Font");
00031                 currentcolor.r = 0;
00032                 currentcolor.g = 0;
00033                 currentcolor.b = 0;
00034                 thefont = 0;
00035                 load(fname.toCharPtr(), s);
00036         }
00037 
00038         Font::Font(TTF_Font *f, int s):GUIObject(){
00039                 setClassName("Font");
00040                 currentcolor.r = 0;
00041                 currentcolor.g = 0;
00042                 currentcolor.b = 0;
00043                 thefont = f;
00044         }
00045 
00046         void Font::load(const char *fname, int s){
00047                 if(thefont)
00048                         destroy();
00049                 thefont = TTF_OpenFont(fname, s);
00050                 if(thefont == 0){
00051                         cerr << "Unable to load font: " << fname << endl;
00052                         size(0);
00053                         throw IOException();
00054                         return;
00055                 }
00056                 size(s);
00057         }
00058 
00059         void Font::load(String fname, int s){
00060                 load(fname.toCharPtr(), s);
00061         }
00062 
00063         void Font::destroy(){
00064                 if(thefont)
00065                         TTF_CloseFont(thefont);
00066         }
00067 
00068         void Font::setStyle(int s){
00069                 if(thefont){
00070                         TTF_SetFontStyle(thefont, s);
00071                 }
00072         }
00073 
00074         void Font::setColor(int r, int g, int b){
00075                 currentcolor.r = r;
00076                 currentcolor.g = g;
00077                 currentcolor.b = b;
00078         }
00079 
00080         void Font::setColor(int rgb[]){
00081                 currentcolor.r = rgb[0];
00082                 currentcolor.g = rgb[1];
00083                 currentcolor.b = rgb[2];
00084         }
00085 
00086         void Font::setColor(SDL_Color rgb){
00087                 currentcolor.r = rgb.r;
00088                 currentcolor.g = rgb.g;
00089                 currentcolor.b = rgb.b;
00090         }
00091 
00092         void Font::setBGColor(int r, int g, int b){
00093                 bgcolor.r = r;
00094                 bgcolor.g = g;
00095                 bgcolor.b = b;
00096         }
00097 
00098         void Font::setBGColor(int rgb[]){
00099                 bgcolor.r = rgb[0];
00100                 bgcolor.g = rgb[1];
00101                 bgcolor.b = rgb[2];
00102         }
00103 
00104         void Font::setBGColor(SDL_Color rgb){
00105                 bgcolor.r = rgb.r;
00106                 bgcolor.g = rgb.g;
00107                 bgcolor.b = rgb.b;
00108         }
00109 
00110         SDL_Surface *Font::render(const char *str){
00111                 SDL_Surface *surf = 0;
00112                 if(str == 0){
00113                         cerr << "NULL" << endl;
00114                         return 0;
00115                 }
00116                 else{
00117                         if(str[0] == 0){
00118                                 cerr << "NULL" << endl;
00119                                 return 0;
00120                         }
00121                         else if(str[0] == '\n' && strlen(str) == 1){
00122                         }
00123                         
00124                 }
00125                 //cerr << "rt: " << str << endl;
00126                 surf = TTF_RenderText_Blended(thefont, str, currentcolor);
00127                 return surf;
00128         }
00129 
00130         SDL_Surface *Font::render(String str){
00131                 return render(str.toCharPtr());
00132         }
00133 
00134         SDL_Surface *Font::renderText(const char *s1){
00135                 //String str;
00136                 char *buf;
00137                 char *s;
00138                 int i, k;
00139                 int num_lines = 1, hh = 0, ww;
00140                 bool flag;
00141                 int siz;
00142                 Uint32 rmask, gmask, bmask, amask;
00143                 SDL_Surface *surfT;
00144                 SDL_Rect src, dst;
00145 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
00146                 rmask = 0xff000000;
00147                 gmask = 0x00ff0000;
00148                 bmask = 0x0000ff00;
00149                 amask = 0x000000ff;
00150 #else
00151                 rmask = 0x000000ff;
00152                 gmask = 0x0000ff00;
00153                 bmask = 0x00ff0000;
00154                 amask = 0xff000000;
00155 #endif
00156                 //str = s1;
00157                 s = strdup(s1);
00158                 siz = strlen(s);
00159                 while(s[siz - 1] == '\n'){
00160                         s[siz - 1] = '\0';
00161                         siz--;
00162                 }
00163                 for(i = 0; i < siz; i++){
00164                         if(s[i] == '\n'){
00165                                 num_lines++;
00166                         }
00167                 }
00168                 if(num_lines == 1){
00169                         return render(s);
00170                 }
00171                 SDL_Surface *surf[num_lines + 1];
00172                 for(i = 0; i <= num_lines; i++)
00173                         surf[i] = 0;
00174                 Tokenizer stoken(s, "\n");
00175                 k = 0;
00176                 flag = false;
00177                 while(true){
00178                         try{
00179                                 buf = (char *)stoken.next().toCharPtr();
00180                         }
00181                         catch(NoMoreTokensException e1){
00182                                 break;
00183                         }
00184                         surf[k] = render(buf);
00185                         if(flag){
00186                                 if(surf[k]->w > ww)
00187                                         ww = surf[k]->w;
00188 
00189                         }
00190                         else{
00191                                 ww = surf[k]->w;
00192                                 flag = true;
00193                         }
00194                         hh += surf[k]->h;
00195                         k++;
00196                 }
00197                 surfT = SDL_CreateRGBSurface(SDL_HWSURFACE, ww, hh, 32, rmask, bmask, gmask, 0);
00198                 src.x = 0;
00199                 src.y = 0;
00200                 dst.x = 0;
00201                 dst.y = 0;
00202                 for(k = 0; k < num_lines; k++){
00203                         src.w = surf[k]->w;
00204                         src.h = surf[k]->h;
00205                         SDL_BlitSurface(surf[k], &src, surfT, &dst);
00206                         SDL_UpdateRect(surfT, dst.x, dst.y, dst.w, dst.h);
00207                         dst.y += surf[k]->h;
00208                         SDL_FreeSurface(surf[k]);
00209                 }
00210                 return surfT;
00211         }
00212 
00213         const int Font::size(){
00214                 return siz;
00215         }
00216 
00217         void Font::size(int s){
00218                 siz = s;
00219         }
00220 
00221         void Font::draw(){ 
00222         }
00223 
00224 }

Powered by:

SourceForge Logo