6 SFont_FontInfo InternalFont;
11 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
17 if (X<0) puts("SFONT ERROR: x too small in GetPixel. Report this to <karlb@gmx.net>");
18 if (X>=Surface->w) puts("SFONT ERROR: x too big in GetPixel. Report this to <karlb@gmx.net>");
20 Bpp = Surface->format->BytesPerPixel;
22 bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
24 // Paint the walls with the fresh blood of your enemies
29 return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
32 return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
34 case 3: { // Format/endian independent
36 r = *((bits)+Surface->format->Rshift/8);
37 g = *((bits)+Surface->format->Gshift/8);
38 b = *((bits)+Surface->format->Bshift/8);
39 return SDL_MapRGB(Surface->format, r, g, b);
43 return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
51 InitFont2(SFont_FontInfo *Font)
55 if ( Font->Surface==NULL ) {
56 printf("The font has not been loaded!\n");
60 while ( x < Font->Surface->w ) {
61 if(GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)) {
63 while (( x < Font->Surface->w-1) && (GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)))
70 Font->h=Font->Surface->h;
71 font_height = Font->h;
72 SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, GetPixel(Font->Surface, 0, Font->Surface->h-1));
76 InitFont(SDL_Surface *Font)
78 InternalFont.Surface=Font;
79 InitFont2(&InternalFont);
83 SFont_wide(char *text) {
86 SFont_FontInfo *Font = &InternalFont;
90 xwide += (int)(Font->CharPos[2]-Font->CharPos[1]);
92 ofs = (text[i]-33)*2+1;
93 xwide += (int)(Font->CharPos[ofs+1]-Font->CharPos[ofs]);
102 return InternalFont.Surface->h-1;
106 PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, char *text)
110 SDL_Rect srcrect,dstrect;
114 x+=Font->CharPos[2]-Font->CharPos[1];
117 // printf("-%c- %c - %u\n",228,text[i],text[i]);
118 ofs=(text[i]-33)*2+1;
119 //printf("printing %c %d\n",text[i],ofs);
120 srcrect.w = dstrect.w = (Font->CharPos[ofs+2]+Font->CharPos[ofs+1])/2-(Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
121 srcrect.h = dstrect.h = Font->Surface->h-1;
122 srcrect.x = (Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
124 dstrect.x = x-(float)(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
127 //SDL_SetAlpha ( Font->Surface, SDL_SRCALPHA, 127);
128 SDL_BlitSurface( Font->Surface, &srcrect, Surface, &dstrect);
130 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
136 // Return a new surface, with the text on it.
137 // This surface is new, fresh, and must eventually be freed.
138 // Create the new surface with the same colour system as a parent surface.
140 new_Surface_PutString(SDL_Surface *parent, char *text) {
142 Uint32 rmask = parent->format->Rmask;
143 Uint32 gmask = parent->format->Gmask;
144 Uint32 bmask = parent->format->Bmask;
145 Uint32 amask = parent->format->Amask;
146 Uint32 bytesperpixel = parent->format->BytesPerPixel;
148 return SDL_CreateRGBSurface(
152 bytesperpixel, rmask, gmask, bmask, amask
157 PutString(SDL_Surface *Surface, int x, int y, char *text) {
158 PutString2(Surface, &InternalFont, x, y, text);
162 TextWidth2(SFont_FontInfo *Font, char *text)
165 unsigned char ofs = 0;
166 while (text[i]!='\0') {
168 x+=Font->CharPos[2]-Font->CharPos[1];
170 ofs=(text[i]-33)*2+1;
171 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
175 return x+Font->CharPos[ofs+2]-Font->CharPos[ofs+1];
179 TextWidth(char *text)
181 return TextWidth2(&InternalFont, text);
190 XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text)
192 PutString2(Surface, &InternalFont, Surface->w/2-TextWidth(text)/2, y, text);
196 XCenteredString(SDL_Surface *Surface, int y, char *text)
198 XCenteredString2(Surface, &InternalFont, y, text);
208 // Delete the event buffer
209 while (SDL_PollEvent(&event))
212 SDL_EnableUNICODE(1);
216 SFont_Input2( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
221 int ofs=(text[0]-33)*2+1;
228 leftshift = (Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
234 rect.h=Font->Surface->h;
236 //SDL_SetAlpha (Dest, SDL_SRCALPHA, 127);
238 SDL_BlitSurface(Dest, &rect, Back, NULL);
239 sprintf(tmp,"%s_",text);
240 PutString2(Dest,Font,x,y,tmp);
241 SDL_UpdateRect(Dest, x-leftshift, y, PixelWidth, Font->h);
243 while (SDL_PollEvent(&event) && event.type==SDL_KEYDOWN) {
245 // Find the character pressed
246 ch=event.key.keysym.unicode;
248 // If backspace and the length of the text > 0, reduce the string by 1 character
250 if (strlen(text)>0) {
251 text[strlen(text)-1]='\0';
255 sprintf(text,"%s%c",text,ch);
258 // If the new character would exceed the allowed width
259 if (TextWidth2(Font,text)>PixelWidth) {
260 text[strlen(text)-1]='\0';
263 //SDL_SetAlpha (Back, SDL_SRCALPHA, 127);
264 SDL_BlitSurface( Back, NULL, Dest, &rect);
265 PutString2(Dest, Font, x, y, text);
267 SDL_UpdateRect(Dest, x-(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2, y, PixelWidth, Font->Surface->h);
271 //text[strlen(text)-1]='\0';
272 if (ch==SDLK_RETURN) {
273 SDL_FreeSurface(Back);
281 SFont_Input( SDL_Surface *Dest, int x, int y, int PixelWidth, char *text)
284 Back = SDL_AllocSurface(Dest->flags,
287 Dest->format->BitsPerPixel,
290 Dest->format->Bmask, 0);
292 return SFont_Input2( Dest, &InternalFont, x, y, PixelWidth, text);
294 // Returns 1 when the return key is pressed
295 // Returns 0 when nothing exceptional happened