examples/memory_sdl/sdlfont.cpp

00001 #include <SDL/SDL.h>
00002 
00003 extern SDL_Surface* screen;
00004 void SDLFontDrawChar(char t, SDL_Surface* img, int x1, int y1) {
00005         t -= 32;
00006     int x = (int)(t % 32)*16.;
00007     int y = (int)(t / 32)*16.;
00008 
00009     int w = 10;
00010     int h = 10;
00011 
00012         SDL_Rect dest;
00013         dest.x = x1;
00014         dest.y = y1;
00015         SDL_Rect src;
00016         src.x = x;
00017         src.y = y;
00018         src.w = w;
00019         src.h = h;
00020         SDL_BlitSurface(img, &src, screen, &dest);
00021 }
00022 
00023 
00024 void SDLFontDraw(const char* txt, const void* font, float x, float y){
00025 
00026         SDL_Surface *img = (SDL_Surface*)font;
00027 
00028         float cx=x*10,cy=y*10;
00029 
00030         volatile register float sizesofar = 0.;
00031         volatile register float linessofar = 0.;
00032         for (volatile register unsigned char *t = (unsigned char*)txt; *t; ++t) {
00033                 switch (*t) {
00034                         default:
00035                                 SDLFontDrawChar(*t, img, cx, cy);
00036                                 cx += 10;
00037                                 sizesofar += 10;
00038                                 break;
00039                         case '\n':
00040                         case '\r':
00041                                 cx -= sizesofar;
00042                                 cy += 10;
00043                                 linessofar += 1.;
00044                                 sizesofar = 0;
00045                                 if (*t == '\n' && *(t+1)=='\r' || *t == '\r' && *(t+1)=='\n' ) t++;
00046                                 break;
00047 
00048                 }
00049         }
00050 
00051 
00052 }
00053 float SDLFontSize(const char* txt, const void* font) {
00054         return strlen(txt);
00055 }
00056 

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007