1 /* SFont: a simple font library that uses special images as fonts
2 Copyright (C) 2003 Karl Bartel
4 WWW: http://www.linux-games.com/sfont/
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
41 Bpp = Surface->format->BytesPerPixel;
42 bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
47 return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
50 return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
52 case 3: { // Format/endian independent
54 r = *((bits)+Surface->format->Rshift/8);
55 g = *((bits)+Surface->format->Gshift/8);
56 b = *((bits)+Surface->format->Bshift/8);
57 return SDL_MapRGB(Surface->format, r, g, b);
61 return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
69 SFont_InitFont(SDL_Surface* Surface)
79 Font = (SFont_Font *) malloc(sizeof(SFont_Font));
80 Font->Surface = Surface;
82 SDL_LockSurface(Surface);
84 pink = SDL_MapRGB(Surface->format, 255, 0, 255);
85 while (x < Surface->w) {
86 if (GetPixel(Surface, x, 0) == pink) {
88 while((x < Surface->w) && (GetPixel(Surface, x, 0)== pink))
96 pixel = GetPixel(Surface, 0, Surface->h-1);
97 SDL_UnlockSurface(Surface);
98 SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel);
104 SFont_FreeFont(SFont_Font* FontInfo)
106 SDL_FreeSurface(FontInfo->Surface);
111 SFont_Write(SDL_Surface *Surface, const SFont_Font *Font,
112 int x, int y, const char *text)
116 SDL_Rect srcrect, dstrect;
121 // these values won't change in the loop
124 srcrect.h = dstrect.h = Font->Surface->h - 1;
126 for(c = text; *c != '\0' && x <= Surface->w ; c++) {
127 charoffset = ((int) (*c - 33)) * 2 + 1;
128 // skip spaces and nonprintable characters
129 if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) {
130 x += Font->CharPos[2]-Font->CharPos[1];
134 srcrect.w = dstrect.w =
135 (Font->CharPos[charoffset+2] + Font->CharPos[charoffset+1])/2 -
136 (Font->CharPos[charoffset] + Font->CharPos[charoffset-1])/2;
137 srcrect.x = (Font->CharPos[charoffset]+Font->CharPos[charoffset-1])/2;
138 dstrect.x = x - (float)(Font->CharPos[charoffset]
139 - Font->CharPos[charoffset-1])/2;
141 SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect);
143 x += Font->CharPos[charoffset+1] - Font->CharPos[charoffset];
148 SFont_TextWidth(const SFont_Font *Font, const char *text)
157 for(c = text; *c != '\0'; c++) {
158 charoffset = ((int) *c - 33) * 2 + 1;
159 // skip spaces and nonprintable characters
160 if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) {
161 width += Font->CharPos[2]-Font->CharPos[1];
165 width += Font->CharPos[charoffset+1] - Font->CharPos[charoffset];
172 SFont_TextHeight(const SFont_Font* Font)
174 return Font->Surface->h - 1;
178 SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font,
179 int y, const char *text)
181 SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2,