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, 2006 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 int TextWidth2(SFont_FontInfo
* Font
, char *text
)
150 while (text
[i
] != '\0') {
151 if (text
[i
] == ' ') {
152 x
+= Font
->CharPos
[2] - Font
->CharPos
[1];
155 ofs
= ((unsigned char) text
[i
] - 33) * 2 + 1;
156 x
+= Font
->CharPos
[ofs
+ 1] - Font
->CharPos
[ofs
];
163 void SFont_InternalInput(SDL_Surface
* Dest
, SFont_FontInfo
* Font
, int x
,
164 int y
, int PixelWidth
, char *text
)
167 int ch
= -1, blink
= 0;
173 Back
= SDL_AllocSurface(Dest
->flags
,
176 Dest
->format
->BitsPerPixel
,
178 Dest
->format
->Gmask
, Dest
->format
->Bmask
, 0);
182 rect
.h
= Font
->Surface
->h
;
183 SDL_BlitSurface(Dest
, &rect
, Back
, NULL
);
184 PutString2(Dest
, Font
, x
, y
, text
);
185 SDL_UpdateRects(Dest
, 1, &rect
);
188 previous
= SDL_EnableUNICODE(1);
189 blinktimer
= SDL_GetTicks();
190 while (ch
!= SDLK_RETURN
) {
191 if (event
.type
== SDL_KEYDOWN
) {
192 ch
= event
.key
.keysym
.unicode
;
193 if (((ch
> 31) || (ch
== '\b')) && (ch
< 128)) {
194 if ((ch
== '\b') && (strlen(text
) > 0))
195 text
[strlen(text
) - 1] = '\0';
197 sprintf(text
, "%s%c", text
, ch
);
198 if (TextWidth2(Font
, text
) > PixelWidth
)
199 text
[strlen(text
) - 1] = '\0';
200 SDL_BlitSurface(Back
, NULL
, Dest
, &rect
);
201 PutString2(Dest
, Font
, x
, y
, text
);
202 SDL_UpdateRects(Dest
, 1, &rect
);
203 SDL_WaitEvent(&event
);
206 if (SDL_GetTicks() > blinktimer
) {
208 blinktimer
= SDL_GetTicks() + 500;
210 PutString2(Dest
, Font
,
211 x
+ TextWidth2(Font
, text
), y
, "|");
212 SDL_UpdateRects(Dest
, 1, &rect
);
214 SDL_BlitSurface(Back
, NULL
, Dest
, &rect
);
215 PutString2(Dest
, Font
, x
, y
, text
);
216 SDL_UpdateRects(Dest
, 1, &rect
);
220 SDL_PollEvent(&event
);
222 text
[strlen(text
)] = '\0';
223 SDL_FreeSurface(Back
);
224 /* restore the previous state */
225 SDL_EnableUNICODE(previous
);
228 void SFont_Input2(SDL_Surface
* Dest
, SFont_FontInfo
* Font
, int x
, int y
,
229 int PixelWidth
, char *text
)
231 SFont_InternalInput(Dest
, Font
, x
, y
, PixelWidth
, text
);