6 SFont_FontInfo InternalFont;
10 Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
16 if (X<0) puts("SFONT ERROR: x too small in GetPixel. Report this to <karlb@gmx.net>");
17 if (X>=Surface->w) puts("SFONT ERROR: x too big in GetPixel. Report this to <karlb@gmx.net>");
19 Bpp = Surface->format->BytesPerPixel;
21 bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
23 // Paint the walls with the fresh blood of your enemies
28 return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
31 return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
33 case 3: { // Format/endian independent
35 r = *((bits)+Surface->format->Rshift/8);
36 g = *((bits)+Surface->format->Gshift/8);
37 b = *((bits)+Surface->format->Bshift/8);
38 return SDL_MapRGB(Surface->format, r, g, b);
42 return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
49 void InitFont2(SFont_FontInfo *Font)
53 if ( Font->Surface==NULL ) {
54 printf("The font has not been loaded!\n");
58 while ( x < Font->Surface->w ) {
59 if(GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)) {
61 while (( x < Font->Surface->w-1) && (GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)))
68 Font->h=Font->Surface->h;
69 font_height = Font->h;
70 SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, GetPixel(Font->Surface, 0, Font->Surface->h-1));
73 void InitFont(SDL_Surface *Font)
75 InternalFont.Surface=Font;
76 InitFont2(&InternalFont);
79 int SFont_wide(char *text) {
82 SFont_FontInfo *Font = &InternalFont;
86 xwide += (int)(Font->CharPos[2]-Font->CharPos[1]);
88 ofs = (text[i]-33)*2+1;
89 xwide += (int)(Font->CharPos[ofs+1]-Font->CharPos[ofs]);
97 return InternalFont.Surface->h-1;
100 void PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, char *text)
104 SDL_Rect srcrect,dstrect;
108 x+=Font->CharPos[2]-Font->CharPos[1];
111 // printf("-%c- %c - %u\n",228,text[i],text[i]);
112 ofs=(text[i]-33)*2+1;
113 //printf("printing %c %d\n",text[i],ofs);
114 srcrect.w = dstrect.w = (Font->CharPos[ofs+2]+Font->CharPos[ofs+1])/2-(Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
115 srcrect.h = dstrect.h = Font->Surface->h-1;
116 srcrect.x = (Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
118 dstrect.x = x-(float)(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
121 //SDL_SetAlpha ( Font->Surface, SDL_SRCALPHA, 127);
122 SDL_BlitSurface( Font->Surface, &srcrect, Surface, &dstrect);
124 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
130 // Return a new surface, with the text on it.
131 // This surface is new, fresh, and must eventually be freed.
132 // Create the new surface with the same colour system as a parent surface.
133 SDL_Surface *new_Surface_PutString(SDL_Surface *parent, char *text) {
135 Uint32 rmask = parent->format->Rmask;
136 Uint32 gmask = parent->format->Gmask;
137 Uint32 bmask = parent->format->Bmask;
138 Uint32 amask = parent->format->Amask;
139 Uint32 bytesperpixel = parent->format->BytesPerPixel;
141 return SDL_CreateRGBSurface(
145 bytesperpixel, rmask, gmask, bmask, amask
149 void PutString(SDL_Surface *Surface, int x, int y, char *text) {
150 PutString2(Surface, &InternalFont, x, y, text);
153 int TextWidth2(SFont_FontInfo *Font, char *text)
156 unsigned char ofs = 0;
157 while (text[i]!='\0') {
159 x+=Font->CharPos[2]-Font->CharPos[1];
161 ofs=(text[i]-33)*2+1;
162 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
166 return x+Font->CharPos[ofs+2]-Font->CharPos[ofs+1];
169 int TextWidth(char *text)
171 return TextWidth2(&InternalFont, text);
174 void TextAlpha(int a) {
178 void XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text)
180 PutString2(Surface, &InternalFont, Surface->w/2-TextWidth(text)/2, y, text);
183 void XCenteredString(SDL_Surface *Surface, int y, char *text)
185 XCenteredString2(Surface, &InternalFont, y, text);
194 // Delete the event buffer
195 while (SDL_PollEvent(&event))
198 SDL_EnableUNICODE(1);
201 int SFont_Input2( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
206 int ofs=(text[0]-33)*2+1;
213 leftshift = (Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
219 rect.h=Font->Surface->h;
221 //SDL_SetAlpha (Dest, SDL_SRCALPHA, 127);
223 SDL_BlitSurface(Dest, &rect, Back, NULL);
224 sprintf(tmp,"%s_",text);
225 PutString2(Dest,Font,x,y,tmp);
226 SDL_UpdateRect(Dest, x-leftshift, y, PixelWidth, Font->h);
228 while (SDL_PollEvent(&event) && event.type==SDL_KEYDOWN) {
230 // Find the character pressed
231 ch=event.key.keysym.unicode;
233 // If backspace and the length of the text > 0, reduce the string by 1 character
235 if (strlen(text)>0) {
236 text[strlen(text)-1]='\0';
240 sprintf(text,"%s%c",text,ch);
243 // If the new character would exceed the allowed width
244 if (TextWidth2(Font,text)>PixelWidth) {
245 text[strlen(text)-1]='\0';
248 //SDL_SetAlpha (Back, SDL_SRCALPHA, 127);
249 SDL_BlitSurface( Back, NULL, Dest, &rect);
250 PutString2(Dest, Font, x, y, text);
252 SDL_UpdateRect(Dest, x-(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2, y, PixelWidth, Font->Surface->h);
256 //text[strlen(text)-1]='\0';
257 if (ch==SDLK_RETURN) {
258 SDL_FreeSurface(Back);
265 int SFont_Input( SDL_Surface *Dest, int x, int y, int PixelWidth, char *text)
268 Back = SDL_AllocSurface(Dest->flags,
271 Dest->format->BitsPerPixel,
274 Dest->format->Bmask, 0);
276 return SFont_Input2( Dest, &InternalFont, x, y, PixelWidth, text);
278 // Returns 1 when the return key is pressed
279 // Returns 0 when nothing exceptional happened