1 /* SFont: a simple font library that uses special images as fonts
2 Copyright (C) 2003 Karl Bartel
4 License: GPL or LGPL (at your choice)
5 WWW: http://www.linux-games.com/sfont/
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
42 Bpp = Surface->format->BytesPerPixel;
43 bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
48 return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
51 return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
53 case 3: { // Format/endian independent
55 r = *((bits)+Surface->format->Rshift/8);
56 g = *((bits)+Surface->format->Gshift/8);
57 b = *((bits)+Surface->format->Bshift/8);
58 return SDL_MapRGB(Surface->format, r, g, b);
62 return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
70 SFont_InitFont(SDL_Surface* Surface)
80 Font = (SFont_Font *) malloc(sizeof(SFont_Font));
81 Font->Surface = Surface;
83 SDL_LockSurface(Surface);
85 pink = SDL_MapRGB(Surface->format, 255, 0, 255);
86 while (x < Surface->w) {
87 if (GetPixel(Surface, x, 0) == pink) {
89 while((x < Surface->w) && (GetPixel(Surface, x, 0)== pink))
97 pixel = GetPixel(Surface, 0, Surface->h-1);
98 SDL_UnlockSurface(Surface);
99 SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel);
105 SFont_FreeFont(SFont_Font* FontInfo)
107 SDL_FreeSurface(FontInfo->Surface);
112 SFont_Write(SDL_Surface *Surface, const SFont_Font *Font,
113 int x, int y, const char *text)
117 SDL_Rect srcrect, dstrect;
122 // these values won't change in the loop
125 srcrect.h = dstrect.h = Font->Surface->h - 1;
127 for(c = text; *c != '\0' && x <= Surface->w ; c++) {
128 charoffset = ((int) (*c - 33)) * 2 + 1;
129 // skip spaces and nonprintable characters
130 if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) {
131 x += Font->CharPos[2]-Font->CharPos[1];
135 srcrect.w = dstrect.w =
136 (Font->CharPos[charoffset+2] + Font->CharPos[charoffset+1])/2 -
137 (Font->CharPos[charoffset] + Font->CharPos[charoffset-1])/2;
138 srcrect.x = (Font->CharPos[charoffset]+Font->CharPos[charoffset-1])/2;
139 dstrect.x = x - (float)(Font->CharPos[charoffset]
140 - Font->CharPos[charoffset-1])/2;
142 SDL_BlitSurface(Font->Surface, &srcrect, Surface, &dstrect);
144 x += Font->CharPos[charoffset+1] - Font->CharPos[charoffset];
149 SFont_TextWidth(const SFont_Font *Font, const char *text)
158 for(c = text; *c != '\0'; c++) {
159 charoffset = ((int) *c - 33) * 2 + 1;
160 // skip spaces and nonprintable characters
161 if (*c == ' ' || charoffset < 0 || charoffset > Font->MaxPos) {
162 width += Font->CharPos[2]-Font->CharPos[1];
166 width += Font->CharPos[charoffset+1] - Font->CharPos[charoffset];
173 SFont_TextHeight(const SFont_Font* Font)
175 return Font->Surface->h - 1;
179 SFont_WriteCenter(SDL_Surface *Surface, const SFont_Font *Font,
180 int y, const char *text)
182 SFont_Write(Surface, Font, Surface->w/2 - SFont_TextWidth(Font, text)/2,