/
SFont.xs
316 lines (274 loc) · 8.56 KB
/
SFont.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* */
/* SFont.xs */
/* */
/* Original SFont code Copyright (C) Karl Bartel */
/* Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org> */
/* */
/* ------------------------------------------------------------------------------ */
/* */
/* This library is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Lesser General Public */
/* License as published by the Free Software Foundation; either */
/* version 2.1 of the License, or (at your option) any later version. */
/* */
/* This library is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
/* Lesser General Public License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
/* */
/* ------------------------------------------------------------------------------ */
/* */
/* Please feel free to send questions, suggestions or improvements to: */
/* */
/* David J. Goehrig */
/* dgoehrig@cpan.org */
/* */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#ifndef aTHX_
#define aTHX_
#endif
#include <SDL.h>
#ifdef HAVE_SDL_IMAGE
#include <SDL_image.h>
#endif
#include <string.h>
#include <stdlib.h>
#ifdef USE_THREADS
#define HAVE_TLS_CONTEXT
#endif
#include "SDLx/SFont.h"
SFont_FontInfo InternalFont;
Uint32 SFont_GetPixel(SDL_Surface *Surface, Sint32 X, Sint32 Y)
{
Uint8 *bits;
Uint32 Bpp;
if (X<0) puts("SFONT ERROR: x too small in SFont_GetPixel. Report this to <karlb@gmx.net>");
if (X>=Surface->w) puts("SFONT ERROR: x too big in SFont_GetPixel. Report this to <karlb@gmx.net>");
Bpp = Surface->format->BytesPerPixel;
bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp;
/* Get the pixel */
switch(Bpp) {
case 1:
return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X);
break;
case 2:
return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X);
break;
case 3: { /* Format/endian independent */
Uint8 r, g, b;
r = *((bits)+Surface->format->Rshift/8);
g = *((bits)+Surface->format->Gshift/8);
b = *((bits)+Surface->format->Bshift/8);
return SDL_MapRGB(Surface->format, r, g, b);
}
break;
case 4:
return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X);
break;
}
return -1;
}
void SFont_InitFont2(SFont_FontInfo *Font)
{
int x = 0, i = 0;
if ( Font->Surface==NULL ) {
printf("The font has not been loaded!\n");
exit(1);
}
if (SDL_MUSTLOCK(Font->Surface)) SDL_LockSurface(Font->Surface);
while ( x < Font->Surface->w ) {
if(SFont_GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)) {
Font->CharPos[i++]=x;
while (( x < Font->Surface->w-1) && (SFont_GetPixel(Font->Surface,x,0)==SDL_MapRGB(Font->Surface->format,255,0,255)))
x++;
Font->CharPos[i++]=x;
}
x++;
}
if (SDL_MUSTLOCK(Font->Surface)) SDL_UnlockSurface(Font->Surface);
Font->h=Font->Surface->h;
SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, SFont_GetPixel(Font->Surface, 0, Font->Surface->h-1));
}
void SFont_InitFont(SDL_Surface *Font)
{
InternalFont.Surface=Font;
SFont_InitFont2(&InternalFont);
}
void SFont_PutString2(SDL_Surface *Surface, SFont_FontInfo *Font, int x, int y, char *text)
{
int ofs;
int i=0;
SDL_Rect srcrect,dstrect;
while (text[i]!='\0') {
if (text[i]==' ') {
x+=Font->CharPos[2]-Font->CharPos[1];
i++;
}
else {
/* warn("-%c- %c - %u\n",228,text[i],text[i]); */
ofs=(text[i]-33)*2+1;
/* warn("printing %c %d\n",text[i],ofs); */
srcrect.w = dstrect.w = (Font->CharPos[ofs+2]+Font->CharPos[ofs+1])/2-(Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
srcrect.h = dstrect.h = Font->Surface->h-1;
srcrect.x = (Font->CharPos[ofs]+Font->CharPos[ofs-1])/2;
srcrect.y = 1;
dstrect.x = x-(float)(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2;
dstrect.y = y;
SDL_BlitSurface( Font->Surface, &srcrect, Surface, &dstrect);
x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
i++;
}
}
}
void SFont_PutString(SDL_Surface *Surface, int x, int y, char *text)
{
/* warn("putString \n"); */
SFont_PutString2(Surface, &InternalFont, x, y, text);
}
int SFont_TextWidth2(SFont_FontInfo *Font, char *text)
{
int ofs=0;
int i=0,x=0;
while (text[i]!='\0') {
if (text[i]==' ') {
x+=Font->CharPos[2]-Font->CharPos[1];
i++;
}
else {
ofs=(text[i]-33)*2+1;
x+=Font->CharPos[ofs+1]-Font->CharPos[ofs];
i++;
}
}
/* printf ("--%d\n",x); */
return x;
}
int SFont_TextWidth(char *text)
{
return SFont_TextWidth2(&InternalFont, text);
}
void SFont_XCenteredString2(SDL_Surface *Surface, SFont_FontInfo *Font, int y, char *text)
{
SFont_PutString2(Surface, Font, Surface->w/2-SFont_TextWidth2(Font,text)/2, y, text);
}
void SFont_XCenteredString(SDL_Surface *Surface, int y, char *text)
{
SFont_XCenteredString2(Surface, &InternalFont, y, text);
}
void SFont_InternalInput( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
{
SDL_Event event;
int ch=-1,blink=0;
long blinktimer=0;
SDL_Surface *Back;
SDL_Rect rect;
int previous;
/* int ofs=(text[0]-33)*2+1; */
/* int leftshift=(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2; */
Back = SDL_AllocSurface(Dest->flags,
Dest->w,
Font->h,
Dest->format->BitsPerPixel,
Dest->format->Rmask,
Dest->format->Gmask,
Dest->format->Bmask, 0);
rect.x=0;
rect.y=y;
rect.w=Dest->w;
rect.h=Font->Surface->h;
SDL_BlitSurface(Dest, &rect, Back, NULL);
SFont_PutString2(Dest,Font,x,y,text);
SDL_UpdateRects(Dest, 1, &rect);
/* start input */
previous=SDL_EnableUNICODE(1);
blinktimer=SDL_GetTicks();
while (ch!=SDLK_RETURN) {
if (event.type==SDL_KEYDOWN) {
ch=event.key.keysym.unicode;
if (((ch>31)||(ch=='\b')) && (ch<128)) {
if ((ch=='\b')&&(strlen(text)>0))
text[strlen(text)-1]='\0';
else if (ch!='\b')
sprintf(text+strlen(text),"%c",ch);
if (SFont_TextWidth2(Font,text)>PixelWidth) text[strlen(text)-1]='\0';
SDL_BlitSurface( Back, NULL, Dest, &rect);
SFont_PutString2(Dest, Font, x, y, text);
SDL_UpdateRects(Dest, 1, &rect);
/* printf("%s ## %d\n",text,strlen(text)); */
SDL_WaitEvent(&event);
}
}
if (SDL_GetTicks()>blinktimer) {
blink=1-blink;
blinktimer=SDL_GetTicks()+500;
if (blink) {
SFont_PutString2(Dest, Font, x+SFont_TextWidth2(Font,text), y, "|");
SDL_UpdateRects(Dest, 1, &rect);
/* SDL_UpdateRect(Dest, x+SFont_TextWidth2(Font,text), y, SFont_TextWidth2(Font,"|"), Font->Surface->h); */
} else {
SDL_BlitSurface( Back, NULL, Dest, &rect);
SFont_PutString2(Dest, Font, x, y, text);
SDL_UpdateRects(Dest, 1, &rect);
/* SDL_UpdateRect(Dest, x-(Font->CharPos[ofs]-Font->CharPos[ofs-1])/2, y, PixelWidth, Font->Surface->h); */
}
}
SDL_Delay(1);
SDL_PollEvent(&event);
}
text[strlen(text)]='\0';
SDL_FreeSurface(Back);
SDL_EnableUNICODE(previous); /*restore the previous state */
}
void SFont_Input2( SDL_Surface *Dest, SFont_FontInfo *Font, int x, int y, int PixelWidth, char *text)
{
SFont_InternalInput( Dest, Font, x, y, PixelWidth, text);
}
void SFont_Input( SDL_Surface *Dest, int x, int y, int PixelWidth, char *text)
{
SFont_Input2( Dest, &InternalFont, x, y, PixelWidth, text);
}
MODULE = SDLx::SFont PACKAGE = SDLx::SFont PREFIX = st_
SDL_Surface *
st_new ( CLASS, filename )
char *CLASS
char *filename
CODE:
/* warn( "[xs] new" ); */
#ifdef HAVE_SDL_IMAGE
RETVAL = IMG_Load(filename);
#else
SDL_SetError("SDL_image not available for SFont. Using SDL_loadBMP instead of IMG_loadBMP.");
RETVAL = SDL_LoadBMP(filename);
#endif
SFont_InitFont(RETVAL);
OUTPUT:
RETVAL
void
st_use ( surface )
SDL_Surface *surface
CODE:
/*warn( "[xs] use" ); */
SFont_InitFont(surface);
void
st_print_string ( surface, x, y, text )
SDL_Surface *surface
int x
int y
char *text
CODE:
/* warn( "[xs] ps" ); */
SFont_PutString( surface, x, y, text );
int
st_TextWidth ( text )
char *text
CODE:
RETVAL = SFont_TextWidth(text);
OUTPUT:
RETVAL