4 // Original SFont code Copyright (C) Karl Bartel
5 // Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
7 // ------------------------------------------------------------------------------
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 // ------------------------------------------------------------------------------
25 // Please feel free to send questions, suggestions or improvements to:
44 #define HAVE_TLS_CONTEXT
47 #include "../../src/defines.h"
48 #include "../../src/SFont.h"
51 #include <SDL_image.h>
53 SFont_FontInfo InternalFont;
54 Uint32 SFont_GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
59 if (X<0) puts("SFONT ERROR: x too small in SFont_GetPixel. Report this to <karlb@gmx.net>");
60 if (X>=Surface->w) puts("SFONT ERROR: x too big in SFont_GetPixel. Report this to <karlb@gmx.net>");
62 Bpp = Surface->format->BytesPerPixel;
64 bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
69 return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
72 return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
74 case 3: { // Format/endian independent
76 r = *((bits)+Surface->format->Rshift/8);
77 g = *((bits)+Surface->format->Gshift/8);
78 b = *((bits)+Surface->format->Bshift/8);
79 return SDL_MapRGB(Surface->format, r, g, b);
83 return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
90 void SFont_InitFont2(SFont_FontInfo *Font)
94 if ( Font->Surface==NULL ) {
95 printf("The font has not been loaded!\n");
99 if (SDL_MUSTLOCK(Font->Surface)) SDL_LockSurface(Font->Surface);
101 while ( x < Font->Surface->w ) {
102 if(SFont_GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)) {
103 Font->CharPos[i++]=x;
104 while (( x < Font->Surface->w-1) && (SFont_GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)))
106 Font->CharPos[i++]=x;
110 if (SDL_MUSTLOCK(Font->Surface)) SDL_UnlockSurface(Font->Surface);
112 Font->h=Font->Surface->h;
113 SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, SFont_GetPixel(Font->Surface, 0, Font->Surface->h-1));
116 void SFont_InitFont(SDL_Surface *Font)
118 InternalFont.Surface=Font;
119 SFont_InitFont2(&InternalFont);
122 void SFont_PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, char *text)
126 SDL_Rect srcrect,dstrect;
128 while (text[i]!='\0') {
130 x+=Font->CharPos[2]-Font->CharPos[1];
134 // printf("-%c- %c - %u\n",228,text[i],text[i]);
135 ofs=(text[i]-33)*2+1;
136 // printf("printing %c %d\n",text[i],ofs);
137 srcrect.w = dstrect.w = (Font->CharPos[ofs+2]+Font->CharPos[ofs+1])/2-(Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
138 srcrect.h = dstrect.h = Font->Surface->h-1;
139 srcrect.x = (Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
141 dstrect.x = x-(float)(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
144 SDL_BlitSurface( Font->Surface, &srcrect, Surface, &dstrect);
146 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
152 void SFont_PutString(SDL_Surface *Surface, int x, int y, char *text)
154 SFont_PutString2(Surface, &InternalFont, x, y, text);
157 int SFont_TextWidth2(SFont_FontInfo *Font, char *text)
162 while (text[i]!='\0') {
164 x+=Font->CharPos[2]-Font->CharPos[1];
168 ofs=(text[i]-33)*2+1;
169 x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
173 // printf ("--%d\n",x);
177 int SFont_TextWidth(char *text)
179 return SFont_TextWidth2(&InternalFont, text);
182 void SFont_XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text)
184 SFont_PutString2(Surface, Font, Surface->w/2-SFont_TextWidth2(Font,text)/2, y, text);
187 void SFont_XCenteredString(SDL_Surface *Surface, int y, char *text)
189 SFont_XCenteredString2(Surface, &InternalFont, y, text);
192 void SFont_InternalInput( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
200 // int ofs=(text[0]-33)*2+1;
201 // int leftshift=(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
203 Back = SDL_AllocSurface(Dest->flags,
206 Dest->format->BitsPerPixel,
209 Dest->format->Bmask, 0);
213 rect.h=Font->Surface->h;
214 SDL_BlitSurface(Dest, &rect, Back, NULL);
215 SFont_PutString2(Dest,Font,x,y,text);
216 SDL_UpdateRects(Dest, 1, &rect);
219 previous=SDL_EnableUNICODE(1);
220 blinktimer=SDL_GetTicks();
221 while (ch!=SDLK_RETURN) {
222 if (event.type==SDL_KEYDOWN) {
223 ch=event.key.keysym.unicode;
224 if (((ch>31)||(ch=='\b')) && (ch<128)) {
225 if ((ch=='\b')&&(strlen(text)>0))
226 text[strlen(text)-1]='\0';
228 sprintf(text,"%s%c",text,ch);
229 if (SFont_TextWidth2(Font,text)>PixelWidth) text[strlen(text)-1]='\0';
230 SDL_BlitSurface( Back, NULL, Dest, &rect);
231 SFont_PutString2(Dest, Font, x, y, text);
232 SDL_UpdateRects(Dest, 1, &rect);
233 // printf("%s ## %d\n",text,strlen(text));
234 SDL_WaitEvent(&event);
237 if (SDL_GetTicks()>blinktimer) {
239 blinktimer=SDL_GetTicks()+500;
241 SFont_PutString2(Dest, Font, x+SFont_TextWidth2(Font,text), y, "|");
242 SDL_UpdateRects(Dest, 1, &rect);
243 // SDL_UpdateRect(Dest, x+SFont_TextWidth2(Font,text), y, SFont_TextWidth2(Font,"|"), Font->Surface->h);
245 SDL_BlitSurface( Back, NULL, Dest, &rect);
246 SFont_PutString2(Dest, Font, x, y, text);
247 SDL_UpdateRects(Dest, 1, &rect);
248 // SDL_UpdateRect(Dest, x-(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2, y, PixelWidth, Font->Surface->h);
252 SDL_PollEvent(&event);
254 text[strlen(text)]='\0';
255 SDL_FreeSurface(Back);
256 SDL_EnableUNICODE(previous); //restore the previous state
259 void SFont_Input2( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
261 SFont_InternalInput( Dest, Font, x, y, PixelWidth, text);
263 void SFont_Input( SDL_Surface *Dest, int x, int y, int PixelWidth, char *text)
265 SFont_Input2( Dest, &InternalFont, x, y, PixelWidth, text);
270 MODULE = SDL::SFont PACKAGE = SDL::SFont
273 #ifdef HAVE_SDL_IMAGE
279 RETVAL = IMG_Load(filename);
280 SFont_InitFont(RETVAL);
288 SFont_InitFont(surface);
291 PutString ( surface, x, y, text )
297 SFont_PutString( surface, x, y, text );
303 RETVAL = SFont_TextWidth(text);