3 // SFont bitmap font support
5 // SFont library code by Karl Bartel <karlb@gmx.net>
7 // SFont.xs Copyright (C) 2004 David J. Goehrig
23 #define HAVE_TLS_CONTEXT
30 #include <SDL_image.h>
32 SFont_FontInfo InternalFont;
33 Uint32 SFont_GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
38 if (X<0) puts("SFONT ERROR: x too small in SFont_GetPixel. Report this to <karlb@gmx.net>");
39 if (X>=Surface->w) puts("SFONT ERROR: x too big in SFont_GetPixel. Report this to <karlb@gmx.net>");
41 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);
69 void SFont_InitFont2(SFont_FontInfo *Font)
73 if ( Font->Surface==NULL ) {
74 printf("The font has not been loaded!\n");
78 if (SDL_MUSTLOCK(Font->Surface)) SDL_LockSurface(Font->Surface);
80 while ( x < Font->Surface->w ) {
81 if(SFont_GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)) {
83 while (( x < Font->Surface->w-1) && (SFont_GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)))
89 if (SDL_MUSTLOCK(Font->Surface)) SDL_UnlockSurface(Font->Surface);
91 Font->h=Font->Surface->h;
92 SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, SFont_GetPixel(Font->Surface, 0, Font->Surface->h-1));
95 void SFont_InitFont(SDL_Surface *Font)
97 InternalFont.Surface=Font;
98 SFont_InitFont2(&InternalFont);
101 void SFont_PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, char *text)
105 SDL_Rect srcrect,dstrect;
107 while (text[i]!='\0') {
109 x+=Font->CharPos[2]-Font->CharPos[1];
113 // printf("-%c- %c - %u\n",228,text[i],text[i]);
114 ofs=(text[i]-33)*2+1;
115 // printf("printing %c %d\n",text[i],ofs);
116 srcrect.w = dstrect.w = (Font->CharPos[ofs+2]+Font->CharPos[ofs+1])/2-(Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
117 srcrect.h = dstrect.h = Font->Surface->h-1;
118 srcrect.x = (Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
120 dstrect.x = x-(float)(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
123 SDL_BlitSurface( Font->Surface, &srcrect, Surface, &dstrect);
125 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
131 void SFont_PutString(SDL_Surface *Surface, int x, int y, char *text)
133 SFont_PutString2(Surface, &InternalFont, x, y, text);
136 int SFont_TextWidth2(SFont_FontInfo *Font, char *text)
141 while (text[i]!='\0') {
143 x+=Font->CharPos[2]-Font->CharPos[1];
147 ofs=(text[i]-33)*2+1;
148 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
152 // printf ("--%d\n",x);
156 int SFont_TextWidth(char *text)
158 return SFont_TextWidth2(&InternalFont, text);
161 void SFont_XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text)
163 SFont_PutString2(Surface, Font, Surface->w/2-SFont_TextWidth2(Font,text)/2, y, text);
166 void SFont_XCenteredString(SDL_Surface *Surface, int y, char *text)
168 SFont_XCenteredString2(Surface, &InternalFont, y, text);
171 void SFont_InternalInput( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
179 // int ofs=(text[0]-33)*2+1;
180 // int leftshift=(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
182 Back = SDL_AllocSurface(Dest->flags,
185 Dest->format->BitsPerPixel,
188 Dest->format->Bmask, 0);
192 rect.h=Font->Surface->h;
193 SDL_BlitSurface(Dest, &rect, Back, NULL);
194 SFont_PutString2(Dest,Font,x,y,text);
195 SDL_UpdateRects(Dest, 1, &rect);
198 previous=SDL_EnableUNICODE(1);
199 blinktimer=SDL_GetTicks();
200 while (ch!=SDLK_RETURN) {
201 if (event.type==SDL_KEYDOWN) {
202 ch=event.key.keysym.unicode;
203 if (((ch>31)||(ch=='\b')) && (ch<128)) {
204 if ((ch=='\b')&&(strlen(text)>0))
205 text[strlen(text)-1]='\0';
207 sprintf(text,"%s%c",text,ch);
208 if (SFont_TextWidth2(Font,text)>PixelWidth) text[strlen(text)-1]='\0';
209 SDL_BlitSurface( Back, NULL, Dest, &rect);
210 SFont_PutString2(Dest, Font, x, y, text);
211 SDL_UpdateRects(Dest, 1, &rect);
212 // printf("%s ## %d\n",text,strlen(text));
213 SDL_WaitEvent(&event);
216 if (SDL_GetTicks()>blinktimer) {
218 blinktimer=SDL_GetTicks()+500;
220 SFont_PutString2(Dest, Font, x+SFont_TextWidth2(Font,text), y, "|");
221 SDL_UpdateRects(Dest, 1, &rect);
222 // SDL_UpdateRect(Dest, x+SFont_TextWidth2(Font,text), y, SFont_TextWidth2(Font,"|"), Font->Surface->h);
224 SDL_BlitSurface( Back, NULL, Dest, &rect);
225 SFont_PutString2(Dest, Font, x, y, text);
226 SDL_UpdateRects(Dest, 1, &rect);
227 // SDL_UpdateRect(Dest, x-(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2, y, PixelWidth, Font->Surface->h);
231 SDL_PollEvent(&event);
233 text[strlen(text)]='\0';
234 SDL_FreeSurface(Back);
235 SDL_EnableUNICODE(previous); //restore the previous state
238 void SFont_Input2( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
240 SFont_InternalInput( Dest, Font, x, y, PixelWidth, text);
242 void SFont_Input( SDL_Surface *Dest, int x, int y, int PixelWidth, char *text)
244 SFont_Input2( Dest, &InternalFont, x, y, PixelWidth, text);
249 MODULE = SDL::SFont PACKAGE = SDL::SFont
252 #ifdef HAVE_SDL_IMAGE
258 RETVAL = IMG_Load(filename);
259 SFont_InitFont(RETVAL);
267 SFont_InitFont(surface);
270 PutString ( surface, x, y, text )
276 SFont_PutString( surface, x, y, text );
282 RETVAL = SFont_TextWidth(text);