5 SFont_FontInfo InternalFont;
9 Uint32 GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
15 if (X<0) puts("SFONT ERROR: x too small in GetPixel. Report this to <karlb@gmx.net>");
16 if (X>=Surface->w) puts("SFONT ERROR: x too big in GetPixel. Report this to <karlb@gmx.net>");
18 Bpp = Surface->format->BytesPerPixel;
20 bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
22 // Paint the walls with the fresh blood of your enemies
27 return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
30 return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
32 case 3: { // Format/endian independent
34 r = *((bits)+Surface->format->Rshift/8);
35 g = *((bits)+Surface->format->Gshift/8);
36 b = *((bits)+Surface->format->Bshift/8);
37 return SDL_MapRGB(Surface->format, r, g, b);
41 return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
48 void InitFont2(SFont_FontInfo *Font)
52 if ( Font->Surface==NULL ) {
53 printf("The font has not been loaded!\n");
57 while ( x < Font->Surface->w ) {
58 if(GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)) {
60 while (( x < Font->Surface->w-1) && (GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)))
67 Font->h=Font->Surface->h;
68 font_height = Font->h;
69 SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, GetPixel(Font->Surface, 0, Font->Surface->h-1));
72 void InitFont(SDL_Surface *Font)
74 InternalFont.Surface=Font;
75 InitFont2(&InternalFont);
78 int SFont_wide(char *text) {
81 SFont_FontInfo *Font = &InternalFont;
85 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];
163 ofs=(text[i]-33)*2+1;
164 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
168 return x+Font->CharPos[ofs+2]-Font->CharPos[ofs+1];
171 int TextWidth(char *text)
173 return TextWidth2(&InternalFont, text);
176 void TextAlpha(int a) {
180 void XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text)
182 PutString2(Surface, &InternalFont, Surface->w/2-TextWidth(text)/2, y, text);
185 void XCenteredString(SDL_Surface *Surface, int y, char *text)
187 XCenteredString2(Surface, &InternalFont, y, text);
196 // Delete the event buffer
197 while (SDL_PollEvent(&event))
200 SDL_EnableUNICODE(1);
203 int SFont_Input2( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
208 int ofs=(text[0]-33)*2+1;
215 leftshift = (Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
221 rect.h=Font->Surface->h;
223 //SDL_SetAlpha (Dest, SDL_SRCALPHA, 127);
225 SDL_BlitSurface(Dest, &rect, Back, NULL);
226 sprintf(tmp,"%s_",text);
227 PutString2(Dest,Font,x,y,tmp);
228 SDL_UpdateRect(Dest, x-leftshift, y, PixelWidth, Font->h);
230 while (SDL_PollEvent(&event) && event.type==SDL_KEYDOWN) {
232 // Find the character pressed
233 ch=event.key.keysym.unicode;
235 // If backspace and the length of the text > 0, reduce the string by 1 character
237 if (strlen(text)>0) {
238 text[strlen(text)-1]='\0';
242 sprintf(text,"%s%c",text,ch);
245 // If the new character would exceed the allowed width
246 if (TextWidth2(Font,text)>PixelWidth) {
247 text[strlen(text)-1]='\0';
250 //SDL_SetAlpha (Back, SDL_SRCALPHA, 127);
251 SDL_BlitSurface( Back, NULL, Dest, &rect);
252 PutString2(Dest, Font, x, y, text);
254 SDL_UpdateRect(Dest, x-(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2, y, PixelWidth, Font->Surface->h);
258 //text[strlen(text)-1]='\0';
259 if (ch==SDLK_RETURN) {
260 SDL_FreeSurface(Back);
267 int SFont_Input( SDL_Surface *Dest, int x, int y, int PixelWidth, char *text)
270 Back = SDL_AllocSurface(Dest->flags,
273 Dest->format->BitsPerPixel,
276 Dest->format->Bmask, 0);
278 return SFont_Input2( Dest, &InternalFont, x, y, PixelWidth, text);
280 // Returns 1 when the return key is pressed
281 // Returns 0 when nothing exceptional happened