| 1 | // $Id: dfont.cpp,v 1.3 2003/04/14 05:51:03 mbridak Exp $ |
| 2 | #include "dfont.h" |
| 3 | #include "files.h" |
| 4 | |
| 5 | DFont::DFont (char * IFile, char * DFile) |
| 6 | { |
| 7 | loadImage(IFile); |
| 8 | |
| 9 | ifstream inCredit(DFile, ios::in); |
| 10 | if(!inCredit) |
| 11 | // if (!fstream(DFile, ios::in)) //does the file exist? |
| 12 | initMap(DFile); //if the font file doesn't exist then create one |
| 13 | else |
| 14 | loadMap(DFile); |
| 15 | |
| 16 | loadMap(DFile); |
| 17 | gap = fonts['!'].w; |
| 18 | } |
| 19 | |
| 20 | DFont::~DFont () |
| 21 | { |
| 22 | SDL_FreeSurface(fontSurface); |
| 23 | } |
| 24 | |
| 25 | void DFont::loadImage (char *name) |
| 26 | { |
| 27 | SDL_Surface *temp; |
| 28 | temp = Load_Image(name); |
| 29 | fontSurface = SDL_DisplayFormat(temp); |
| 30 | SDL_FreeSurface(temp); |
| 31 | if (fontSurface == NULL) { |
| 32 | cerr << "Error loading font!\n"; |
| 33 | abort(); |
| 34 | } |
| 35 | // SDL_SetColorKey(fontSurface, SDL_SRCCOLORKEY, |
| 36 | // GetPixel(fontSurface, 0, 1)); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | Uint32 DFont::GetPixel (SDL_Surface *screen, Sint32 x, Sint32 y) |
| 41 | { |
| 42 | switch (screen->format->BytesPerPixel) { |
| 43 | case 1: { /* Assuming 8-bpp */ |
| 44 | return *((Uint8 *)screen->pixels + y*screen->pitch + x); |
| 45 | } |
| 46 | break; |
| 47 | |
| 48 | case 2: { /* Probably 15-bpp or 16-bpp */ |
| 49 | return *((Uint16 *)screen->pixels + y*screen->pitch/2 + x); |
| 50 | } |
| 51 | break; |
| 52 | |
| 53 | case 3: { /* Slow 24-bpp mode, usually not used */ |
| 54 | //Fixme |
| 55 | return 0; |
| 56 | } |
| 57 | break; |
| 58 | |
| 59 | case 4: { /* Probably 32-bpp */ |
| 60 | return *((Uint32 *)screen->pixels + y*screen->pitch/4 + x); |
| 61 | } |
| 62 | break; |
| 63 | } |
| 64 | return 0xffffffff; |
| 65 | } |
| 66 | |
| 67 | void DFont::loadMap (char * name) |
| 68 | { |
| 69 | ifstream file (name); |
| 70 | char c='!'; //all fonts must start with ! and then increment through the |
| 71 | //ASCII table!! |
| 72 | while (file >> fonts[c++]) ; |
| 73 | } |
| 74 | |
| 75 | void DFont::PutString (SDL_Surface * screen, Sint16 x, Sint16 y, char * |
| 76 | str) |
| 77 | { |
| 78 | SDL_Rect dest; |
| 79 | for (; *str != '\0'; ++str) |
| 80 | { |
| 81 | if (*str == ' ') |
| 82 | x+=gap; |
| 83 | else |
| 84 | { |
| 85 | dest.w = fonts[*str].w; |
| 86 | dest.h = fonts[*str].h; |
| 87 | dest.x = x; |
| 88 | dest.y = y; |
| 89 | SDL_BlitSurface (fontSurface, &fonts[*str], screen, &dest); |
| 90 | SDL_UpdateRects(screen, 1, &dest); |
| 91 | x+=dest.w; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void DFont::PutChar (SDL_Surface * screen, Sint16 x, Sint16 y, char c) |
| 97 | { |
| 98 | SDL_Rect dest; |
| 99 | dest.w = fonts[c].w; |
| 100 | dest.h = fonts[c].h; |
| 101 | dest.x = x; |
| 102 | dest.y = y; |
| 103 | SDL_BlitSurface (fontSurface, &fonts[c], screen, &dest); |
| 104 | SDL_UpdateRects(screen, 1, &dest); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* |
| 109 | This function used to save our newly created fonts to a file. |
| 110 | This is a bad idea as we do not know where the file is being |
| 111 | created. It usually ends up in ./data/ and that is not |
| 112 | nice behaviour. Commenting out the file creation and |
| 113 | writes to the file to avoid this. We ship with the proper |
| 114 | font files in the tarball, that should be enough. |
| 115 | -- Jesse |
| 116 | */ |
| 117 | void DFont::initMap (char * name) |
| 118 | { |
| 119 | // ofstream file(name); |
| 120 | //Sentry value |
| 121 | Uint32 sentry = SDL_MapRGB(fontSurface->format, 255, 0, 255); |
| 122 | char index = '!'; //Start of the fonts in ASCII |
| 123 | //Uint16 w; |
| 124 | Sint16 x(0); |
| 125 | //Read the file from left to right detecting the font break markers along the way |
| 126 | while ( x < fontSurface->w ) |
| 127 | { |
| 128 | if (GetPixel(fontSurface, x, 0) != sentry) { |
| 129 | fonts[index].x = x; |
| 130 | fonts[index].y = 1; |
| 131 | fonts[index].h = fontSurface->h; |
| 132 | for (; GetPixel(fontSurface, x, 0) != sentry && x < fontSurface->w; ++x) |
| 133 | ; |
| 134 | fonts[index].w = (x - fonts[index].x); |
| 135 | // file << fonts[index]; |
| 136 | ++index; |
| 137 | } |
| 138 | else |
| 139 | ++x; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | |
| 144 | //Good Ol operator overloading Ahhhhh |
| 145 | ostream& operator << (ostream & os, SDL_Rect const & r) |
| 146 | { |
| 147 | return os << r.x << ' ' << r.y << ' '<< r.w << ' ' << r.h << ' '; |
| 148 | } |
| 149 | |
| 150 | istream& operator >> (istream & is, SDL_Rect & r) |
| 151 | { |
| 152 | return is >> r.x >> r.y >> r.w >> r.h; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 |