2 * Copyright (C) Karl Bartel <karlb@gmx.net> WWW: http://www.linux-games.com
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /*Modified 2003 by Andre Noll */
23 #include <stdlib.h> /* exit */
24 #include <string.h> /* strlen */
25 SFont_FontInfo InternalFont
;
27 Uint32
GetPixel(SDL_Surface
* Surface
, Sint32 X
, Sint32 Y
)
34 puts("SFONT ERROR: x too small in GetPixel. Report this "
35 "to <karlb@gmx.net>");
37 puts("SFONT ERROR: x too big in GetPixel. Report this to "
39 Bpp
= Surface
->format
->BytesPerPixel
;
40 bits
= ((Uint8
*) Surface
->pixels
) + Y
* Surface
->pitch
+ X
* Bpp
;
45 return *((Uint8
*) Surface
->pixels
+ Y
* Surface
->pitch
+ X
);
48 return *((Uint16
*) Surface
->pixels
+ Y
* Surface
->pitch
/ 2 +
51 case 3:{ /* Format/endian independent */
53 r
= *((bits
) + Surface
->format
->Rshift
/ 8);
54 g
= *((bits
) + Surface
->format
->Gshift
/ 8);
55 b
= *((bits
) + Surface
->format
->Bshift
/ 8);
56 return SDL_MapRGB(Surface
->format
, r
, g
, b
);
60 return *((Uint32
*) Surface
->pixels
+ Y
* Surface
->pitch
/ 4 +
68 void InitFont2(SFont_FontInfo
* Font
)
73 printf("The font has not been loaded!\n");
77 if (SDL_MUSTLOCK(Font
->Surface
))
78 SDL_LockSurface(Font
->Surface
);
80 while (x
< Font
->Surface
->w
) {
81 if (GetPixel(Font
->Surface
, x
, 0) ==
82 SDL_MapRGB(Font
->Surface
->format
, 255, 0, 255)) {
83 Font
->CharPos
[i
++] = x
;
84 while ((x
< Font
->Surface
->w
- 1)
85 && (GetPixel(Font
->Surface
, x
, 0) ==
86 SDL_MapRGB(Font
->Surface
->format
, 255, 0,
89 Font
->CharPos
[i
++] = x
;
93 if (SDL_MUSTLOCK(Font
->Surface
))
94 SDL_UnlockSurface(Font
->Surface
);
96 Font
->h
= Font
->Surface
->h
;
97 SDL_SetColorKey(Font
->Surface
, SDL_SRCCOLORKEY
,
98 GetPixel(Font
->Surface
, 0, Font
->Surface
->h
- 1));
102 void InitFont(SDL_Surface
* Font
)
104 InternalFont
.Surface
= Font
;
105 InitFont2(&InternalFont
);
112 void PutString2(SDL_Surface
* Surface
, SFont_FontInfo
* Font
, int x
, int y
,
117 SDL_Rect srcrect
, dstrect
;
119 while (text
[i
] != '\0') {
120 if (text
[i
] == ' ') {
121 x
+= Font
->CharPos
[2] - Font
->CharPos
[1];
124 ofs
= ((unsigned char) text
[i
] - 33) * 2 + 1;
125 srcrect
.w
= dstrect
.w
= (Font
->CharPos
[ofs
+ 2]
126 + Font
->CharPos
[ofs
+ 1]) / 2
127 - (Font
->CharPos
[ofs
] + Font
->CharPos
[ofs
- 1])
129 srcrect
.h
= dstrect
.h
= Font
->Surface
->h
- 1;
131 (Font
->CharPos
[ofs
] + Font
->CharPos
[ofs
- 1]) / 2;
134 x
- (float) (Font
->CharPos
[ofs
] -
135 Font
->CharPos
[ofs
- 1]) / 2;
137 SDL_BlitSurface(Font
->Surface
, &srcrect
, Surface
,
139 x
+= Font
->CharPos
[ofs
+ 1] - Font
->CharPos
[ofs
];
145 void PutString(SDL_Surface
* Surface
, int x
, int y
, char *text
)
147 PutString2(Surface
, &InternalFont
, x
, y
, text
);
151 int TextWidth2(SFont_FontInfo
* Font
, char *text
)
156 while (text
[i
] != '\0') {
157 if (text
[i
] == ' ') {
158 x
+= Font
->CharPos
[2] - Font
->CharPos
[1];
161 ofs
= ((unsigned char) text
[i
] - 33) * 2 + 1;
162 x
+= Font
->CharPos
[ofs
+ 1] - Font
->CharPos
[ofs
];
170 int TextWidth(char *text
)
172 return TextWidth2(&InternalFont
, text
);
176 void XCenteredString2(SDL_Surface
* Surface
, SFont_FontInfo
* Font
, int y
,
179 PutString2(Surface
, Font
, Surface
->w
/ 2 - TextWidth2(Font
, text
) / 2,
184 void XCenteredString(SDL_Surface
* Surface
, int y
, char *text
)
186 XCenteredString2(Surface
, &InternalFont
, y
, text
);
190 void SFont_InternalInput(SDL_Surface
* Dest
, SFont_FontInfo
* Font
, int x
,
191 int y
, int PixelWidth
, char *text
)
194 int ch
= -1, blink
= 0;
200 Back
= SDL_AllocSurface(Dest
->flags
,
203 Dest
->format
->BitsPerPixel
,
205 Dest
->format
->Gmask
, Dest
->format
->Bmask
, 0);
209 rect
.h
= Font
->Surface
->h
;
210 SDL_BlitSurface(Dest
, &rect
, Back
, NULL
);
211 PutString2(Dest
, Font
, x
, y
, text
);
212 SDL_UpdateRects(Dest
, 1, &rect
);
215 previous
= SDL_EnableUNICODE(1);
216 blinktimer
= SDL_GetTicks();
217 while (ch
!= SDLK_RETURN
) {
218 if (event
.type
== SDL_KEYDOWN
) {
219 ch
= event
.key
.keysym
.unicode
;
220 if (((ch
> 31) || (ch
== '\b')) && (ch
< 128)) {
221 if ((ch
== '\b') && (strlen(text
) > 0))
222 text
[strlen(text
) - 1] = '\0';
224 sprintf(text
, "%s%c", text
, ch
);
225 if (TextWidth2(Font
, text
) > PixelWidth
)
226 text
[strlen(text
) - 1] = '\0';
227 SDL_BlitSurface(Back
, NULL
, Dest
, &rect
);
228 PutString2(Dest
, Font
, x
, y
, text
);
229 SDL_UpdateRects(Dest
, 1, &rect
);
230 SDL_WaitEvent(&event
);
233 if (SDL_GetTicks() > blinktimer
) {
235 blinktimer
= SDL_GetTicks() + 500;
237 PutString2(Dest
, Font
,
238 x
+ TextWidth2(Font
, text
), y
, "|");
239 SDL_UpdateRects(Dest
, 1, &rect
);
241 SDL_BlitSurface(Back
, NULL
, Dest
, &rect
);
242 PutString2(Dest
, Font
, x
, y
, text
);
243 SDL_UpdateRects(Dest
, 1, &rect
);
247 SDL_PollEvent(&event
);
249 text
[strlen(text
)] = '\0';
250 SDL_FreeSurface(Back
);
251 /* restore the previous state */
252 SDL_EnableUNICODE(previous
);
255 void SFont_Input2(SDL_Surface
* Dest
, SFont_FontInfo
* Font
, int x
, int y
,
256 int PixelWidth
, char *text
)
258 SFont_InternalInput(Dest
, Font
, x
, y
, PixelWidth
, text
);