author | andyq <andyq> | 2003-01-20 23:11:56 (UTC) |
---|---|---|
committer | andyq <andyq> | 2003-01-20 23:11:56 (UTC) |
commit | 92419b025aa5d82bb7592281af5505ff931c2e58 (patch) (unidiff) | |
tree | 374ee9ca86ef716ce388c8a28e38261f6a04ce7a /noncore/games/sfcave-sdl/bfont.cpp | |
parent | 02090d2e63ad8398c0a8a9f1fb895a9c6e42514b (diff) | |
download | opie-92419b025aa5d82bb7592281af5505ff931c2e58.zip opie-92419b025aa5d82bb7592281af5505ff931c2e58.tar.gz opie-92419b025aa5d82bb7592281af5505ff931c2e58.tar.bz2 |
Initial Revision
Diffstat (limited to 'noncore/games/sfcave-sdl/bfont.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/games/sfcave-sdl/bfont.cpp | 443 |
1 files changed, 443 insertions, 0 deletions
diff --git a/noncore/games/sfcave-sdl/bfont.cpp b/noncore/games/sfcave-sdl/bfont.cpp new file mode 100644 index 0000000..0f29104 --- a/dev/null +++ b/noncore/games/sfcave-sdl/bfont.cpp | |||
@@ -0,0 +1,443 @@ | |||
1 | /***********************************************************/ | ||
2 | /* */ | ||
3 | /* BFONT.c v. 1.0.2 - Billi Font Library by Diego Billi */ | ||
4 | /* BFONT++ C++ port by Gianluigi Davassi */ | ||
5 | /***********************************************************/ | ||
6 | #include "iostream" | ||
7 | using namespace std; | ||
8 | #include "string.h" | ||
9 | #include "stdlib.h" | ||
10 | #include "stdarg.h" | ||
11 | |||
12 | #include "SDL_image.h" | ||
13 | #include "bfont.h" | ||
14 | |||
15 | void BFont::InitFont() | ||
16 | { | ||
17 | int x = 0, i = '!'; | ||
18 | Uint32 sentry = GetPixel(0,0); | ||
19 | |||
20 | if (SDL_MUSTLOCK(Surface)) | ||
21 | SDL_LockSurface(Surface); | ||
22 | |||
23 | while ( x < (Surface->w-1)) | ||
24 | { | ||
25 | if(GetPixel(x,0) != sentry) | ||
26 | { | ||
27 | Chars[i].x = x; | ||
28 | Chars[i].y = 1; | ||
29 | Chars[i].h = Surface->h; | ||
30 | for (; GetPixel(x, 0) != sentry && x < (Surface->w); ++x) ; | ||
31 | Chars[i].w = (x - Chars[i].x); | ||
32 | i++; | ||
33 | } else | ||
34 | { x++; } | ||
35 | } | ||
36 | Chars[' '].x = 0; | ||
37 | Chars[' '].y = 0; | ||
38 | Chars[' '].h = Surface->h; | ||
39 | Chars[' '].w = Chars['!'].w; | ||
40 | |||
41 | if (SDL_MUSTLOCK(Surface)) | ||
42 | SDL_UnlockSurface(Surface); | ||
43 | |||
44 | h = Surface->h; | ||
45 | |||
46 | SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, GetPixel(0, Surface->h-1)); | ||
47 | } | ||
48 | |||
49 | |||
50 | /* Load the font and stores it in the BFont_Info structure */ | ||
51 | void BFont::LoadFont (const char *filename) | ||
52 | { | ||
53 | SDL_Surface *surface(NULL); | ||
54 | int x; | ||
55 | // tutta roba inutile in C++.... :-) | ||
56 | /* BFont_Info *Font=NULL; | ||
57 | Font = (BFont_Info *) malloc(sizeof(BFont_Info));*/ | ||
58 | |||
59 | if ((filename != NULL ) && (this != NULL)) | ||
60 | { | ||
61 | surface = IMG_Load( filename ); | ||
62 | |||
63 | if (surface != NULL) | ||
64 | { | ||
65 | Surface = surface; | ||
66 | for (x=0; x<256; x++) | ||
67 | { | ||
68 | Chars[x].x = 0; | ||
69 | Chars[x].y = 0; | ||
70 | Chars[x].h = 0; | ||
71 | Chars[x].w = 0; | ||
72 | } | ||
73 | InitFont(); // Init the font | ||
74 | } | ||
75 | } | ||
76 | else | ||
77 | { | ||
78 | cerr << "Error! The font has not been loaded!" << endl; | ||
79 | } | ||
80 | } | ||
81 | |||
82 | BFont * BFont :: SetFontColor(Uint8 r, Uint8 g, Uint8 b) | ||
83 | { | ||
84 | int x,y; | ||
85 | |||
86 | BFont *newfont; | ||
87 | SDL_Surface *surface = NULL; | ||
88 | |||
89 | Uint32 pixel; | ||
90 | Uint8 old_r, old_g, old_b; | ||
91 | Uint8 new_r, new_g, new_b; | ||
92 | Uint32 color_key; | ||
93 | |||
94 | newfont = new BFont(NULL); | ||
95 | |||
96 | if (newfont != NULL) { | ||
97 | |||
98 | newfont->h = h; | ||
99 | |||
100 | for (x=0; x<256; x++) { | ||
101 | newfont->Chars[x].x = Chars[x].x; | ||
102 | newfont->Chars[x].y = Chars[x].y; | ||
103 | newfont->Chars[x].h = Chars[x].h; | ||
104 | newfont->Chars[x].w = Chars[x].w; | ||
105 | } | ||
106 | |||
107 | surface = SDL_CreateRGBSurface(SDL_SWSURFACE, Surface->w, Surface->h, 32, | ||
108 | 0x000000ff,0x0000ff00, 0x00ff0000, 0xff000000); | ||
109 | if (surface != NULL) { | ||
110 | |||
111 | if (SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); | ||
112 | if (SDL_MUSTLOCK(Surface)) SDL_LockSurface(Surface); | ||
113 | |||
114 | color_key = xGetPixel(Surface, 0, Surface->h-1); | ||
115 | |||
116 | printf("looking...\n"); | ||
117 | for( x=0; x < Surface->w; x++) { | ||
118 | for( y=0; y < Surface->h; y++) { | ||
119 | old_r = old_g = old_b = 0; | ||
120 | pixel = xGetPixel(Surface,x,y); | ||
121 | |||
122 | if (pixel != color_key) { | ||
123 | SDL_GetRGB(pixel, Surface->format, &old_r,&old_g,&old_b); | ||
124 | |||
125 | new_r = (Uint8) ((old_r * r) / 255); | ||
126 | new_g = (Uint8) ((old_g * g) / 255); | ||
127 | new_b = (Uint8) ((old_b * b) / 255); | ||
128 | |||
129 | pixel = SDL_MapRGB(surface->format,new_r,new_g,new_b); | ||
130 | } | ||
131 | PutPixel(surface,x,y,pixel); | ||
132 | } | ||
133 | } | ||
134 | printf("unlooking...\n"); | ||
135 | if (SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); | ||
136 | if (SDL_MUSTLOCK(Surface)) SDL_UnlockSurface(Surface); | ||
137 | |||
138 | SDL_SetColorKey(surface, SDL_SRCCOLORKEY, color_key); | ||
139 | } | ||
140 | |||
141 | newfont->Surface = surface; | ||
142 | } | ||
143 | return newfont; | ||
144 | } | ||
145 | |||
146 | |||
147 | /* Puts a single char on the surface with the specified font */ | ||
148 | int BFont::PutChar(SDL_Surface *screen, int x, int y, char c) | ||
149 | { | ||
150 | int r=0; | ||
151 | SDL_Rect dest; | ||
152 | |||
153 | dest.w = CharWidth(' '); | ||
154 | dest.h = FontHeight(); | ||
155 | dest.x = x; | ||
156 | dest.y = y; | ||
157 | |||
158 | if (c != ' ') | ||
159 | SDL_BlitSurface( Surface, &Chars[c], screen, &dest); | ||
160 | |||
161 | r = dest.w; | ||
162 | return r; | ||
163 | } | ||
164 | |||
165 | |||
166 | void BFont::PutString(SDL_Surface *screen, int x, int y, const char *text) | ||
167 | { | ||
168 | int i(0); | ||
169 | while (text[i]!='\0') | ||
170 | { | ||
171 | x += PutChar(screen,x,y,text[i]); | ||
172 | i++; | ||
173 | } | ||
174 | } | ||
175 | |||
176 | int BFont::TextWidth(const char *text) | ||
177 | { | ||
178 | int i(0),x(0); | ||
179 | |||
180 | while (text[i]!='\0') | ||
181 | { | ||
182 | x += CharWidth(text[i]); | ||
183 | i++; | ||
184 | } | ||
185 | return x; | ||
186 | } | ||
187 | |||
188 | |||
189 | /* counts the spaces of the strings */ | ||
190 | int BFont::count (const char *text) | ||
191 | { | ||
192 | char *p(NULL); | ||
193 | int pos(-1),i(0); | ||
194 | |||
195 | /* Calculate the space occupied by the text without spaces */ | ||
196 | while ((p=strchr(&text[pos+1],' ')) != NULL) | ||
197 | { | ||
198 | i++; | ||
199 | pos = p - text; | ||
200 | } | ||
201 | return i; | ||
202 | } | ||
203 | |||
204 | |||
205 | void BFont::JustifiedPutString( SDL_Surface *screen, int y, const char *text) | ||
206 | { | ||
207 | int spaces(0),gap,single_gap,dif; | ||
208 | char *strtmp,*p; | ||
209 | int pos(-1),xpos(0); | ||
210 | |||
211 | |||
212 | if (strchr(text,' ') == NULL) | ||
213 | { | ||
214 | PutString(screen, 0, y, text); | ||
215 | } | ||
216 | else { | ||
217 | gap = (screen->w-1) - TextWidth(text); | ||
218 | |||
219 | if (gap <= 0) { | ||
220 | PutString(screen, 0,y,text); | ||
221 | } else { | ||
222 | spaces = count(text); | ||
223 | dif = gap % spaces; | ||
224 | single_gap = (gap - dif) / spaces; | ||
225 | xpos=0; | ||
226 | pos = -1; | ||
227 | while ( spaces > 0 ) | ||
228 | { | ||
229 | p = strstr(&text[pos+1]," "); | ||
230 | strtmp = NULL; | ||
231 | strtmp = (char *) calloc ( (p - &text[pos+1]) + 1,sizeof(char)); | ||
232 | if (strtmp != NULL) | ||
233 | { | ||
234 | strncpy (strtmp, &text[pos+1], (p - &text[pos+1])); | ||
235 | PutString(screen, xpos, y, strtmp); | ||
236 | xpos = xpos + TextWidth(strtmp) + single_gap + CharWidth(' '); | ||
237 | if (dif >= 0) | ||
238 | { | ||
239 | xpos ++; | ||
240 | dif--; | ||
241 | } | ||
242 | pos = p - text; | ||
243 | spaces--; | ||
244 | free(strtmp); | ||
245 | } | ||
246 | } | ||
247 | strtmp = NULL; | ||
248 | strtmp = (char *) calloc ( strlen( &text[pos+1]) + 1,sizeof(char)); | ||
249 | |||
250 | if (strtmp != NULL) { | ||
251 | strncpy (strtmp, &text[pos+1], strlen( &text[pos+1])); | ||
252 | PutString(screen, xpos, y, strtmp); | ||
253 | free(strtmp); | ||
254 | } | ||
255 | } | ||
256 | } | ||
257 | } | ||
258 | |||
259 | |||
260 | void BFont::CenteredPutString(SDL_Surface *screen, int y, const char *text) | ||
261 | { | ||
262 | printf( "xpos - %d, %d <%s>\n", screen->w/2-TextWidth(text)/2, TextWidth(text), text ); | ||
263 | PutString( screen, screen->w/2-TextWidth(text)/2, y, text); | ||
264 | } | ||
265 | |||
266 | |||
267 | void BFont::RightPutString(SDL_Surface *screen, int y, const char *text) | ||
268 | { | ||
269 | PutString( screen, screen->w - TextWidth(text) - 1, y, text); | ||
270 | } | ||
271 | |||
272 | void BFont::LeftPutString(SDL_Surface *screen, int y, const char *text) | ||
273 | { | ||
274 | PutString( screen, 0, y, text); | ||
275 | } | ||
276 | |||
277 | /******/ | ||
278 | |||
279 | void BFont::PrintString (SDL_Surface *screen, int x, int y, char *fmt, ...) | ||
280 | { | ||
281 | va_list args; | ||
282 | char *temp; | ||
283 | va_start (args,fmt); | ||
284 | |||
285 | if ( (temp = (char *) malloc(1000+1)) != NULL) { | ||
286 | vsprintf(temp,fmt,args); | ||
287 | PutString(screen, x, y, temp); | ||
288 | free (temp); | ||
289 | } | ||
290 | va_end(args); | ||
291 | } | ||
292 | |||
293 | void BFont::CenteredPrintString(SDL_Surface *screen, int y, char *fmt, ...) | ||
294 | { | ||
295 | va_list args; | ||
296 | char *temp; | ||
297 | va_start (args,fmt); | ||
298 | |||
299 | if ( (temp = (char *) malloc(1000+1)) != NULL) { | ||
300 | vsprintf(temp,fmt,args); | ||
301 | CenteredPutString(screen, y, temp); | ||
302 | free (temp); | ||
303 | } | ||
304 | va_end(args); | ||
305 | } | ||
306 | |||
307 | void BFont::RightPrintString(SDL_Surface *screen, int y, char *fmt, ...) | ||
308 | { | ||
309 | va_list args; | ||
310 | char *temp; | ||
311 | va_start (args,fmt); | ||
312 | |||
313 | if ( (temp = (char *) malloc(1000+1)) != NULL) { | ||
314 | vsprintf(temp,fmt,args); | ||
315 | RightPutString(screen, y, temp); | ||
316 | free (temp); | ||
317 | } | ||
318 | va_end(args); | ||
319 | } | ||
320 | |||
321 | void BFont::LeftPrintString( SDL_Surface *screen, int y, char *fmt, ...) | ||
322 | { | ||
323 | va_list args; | ||
324 | char *temp; | ||
325 | va_start (args,fmt); | ||
326 | |||
327 | if ( (temp = (char *) malloc(1000+1)) != NULL) { | ||
328 | vsprintf(temp,fmt,args); | ||
329 | LeftPutString(screen, y, temp); | ||
330 | free (temp); | ||
331 | } | ||
332 | va_end(args); | ||
333 | } | ||
334 | |||
335 | void BFont::JustifiedPrintString( SDL_Surface *screen, int y, char *fmt, ...) | ||
336 | { | ||
337 | va_list args; | ||
338 | char *temp; | ||
339 | va_start (args,fmt); | ||
340 | |||
341 | if ( (temp = (char *) malloc(1000+1)) != NULL) { | ||
342 | vsprintf(temp,fmt,args); | ||
343 | JustifiedPutString( screen, y,temp); | ||
344 | free (temp); | ||
345 | } | ||
346 | va_end(args); | ||
347 | } | ||
348 | |||
349 | |||
350 | void BFont::PutPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) | ||
351 | { | ||
352 | int bpp = surface->format->BytesPerPixel; | ||
353 | /* Here p is the address to the pixel we want to set */ | ||
354 | Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; | ||
355 | |||
356 | switch(bpp) { | ||
357 | case 1: | ||
358 | *p = pixel; | ||
359 | break; | ||
360 | |||
361 | case 2: | ||
362 | *(Uint16 *)p = pixel; | ||
363 | break; | ||
364 | |||
365 | case 3: | ||
366 | if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { | ||
367 | p[0] = (pixel >> 16) & 0xff; | ||
368 | p[1] = (pixel >> 8) & 0xff; | ||
369 | p[2] = pixel & 0xff; | ||
370 | } else { | ||
371 | p[0] = pixel & 0xff; | ||
372 | p[1] = (pixel >> 8) & 0xff; | ||
373 | p[2] = (pixel >> 16) & 0xff; | ||
374 | } | ||
375 | break; | ||
376 | |||
377 | case 4: | ||
378 | *(Uint32 *)p = pixel; | ||
379 | break; | ||
380 | } | ||
381 | } | ||
382 | |||
383 | Uint32 BFont :: xGetPixel(SDL_Surface *surface, int x, int y) | ||
384 | { | ||
385 | int bpp = surface->format->BytesPerPixel; | ||
386 | /* Here p is the address to the pixel we want to retrieve */ | ||
387 | Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; | ||
388 | |||
389 | switch(bpp) { | ||
390 | case 1: | ||
391 | return *p; | ||
392 | |||
393 | case 2: | ||
394 | return *(Uint16 *)p; | ||
395 | |||
396 | case 3: | ||
397 | if(SDL_BYTEORDER == SDL_BIG_ENDIAN) | ||
398 | return p[0] << 16 | p[1] << 8 | p[2]; | ||
399 | else | ||
400 | return p[0] | p[1] << 8 | p[2] << 16; | ||
401 | |||
402 | case 4: | ||
403 | return *(Uint32 *)p; | ||
404 | |||
405 | default: | ||
406 | return 0; /* shouldn't happen, but avoids warnings */ | ||
407 | } | ||
408 | } | ||
409 | |||
410 | Uint32 BFont::GetPixel( Sint32 X, Sint32 Y) | ||
411 | { | ||
412 | |||
413 | Uint8 *bits; | ||
414 | Uint32 Bpp; | ||
415 | |||
416 | if (X<0) puts("x too small in GetPixel!"); | ||
417 | if (X>=Surface->w) puts("x too big in GetPixel!"); | ||
418 | |||
419 | Bpp = Surface->format->BytesPerPixel; | ||
420 | |||
421 | bits = ((Uint8 *)Surface->pixels)+Y*Surface->pitch+X*Bpp; | ||
422 | |||
423 | // Get the pixel | ||
424 | switch(Bpp) { | ||
425 | case 1: | ||
426 | return *((Uint8 *)Surface->pixels + Y * Surface->pitch + X); | ||
427 | break; | ||
428 | case 2: | ||
429 | return *((Uint16 *)Surface->pixels + Y * Surface->pitch/2 + X); | ||
430 | break; | ||
431 | case 3: { // Format/endian independent | ||
432 | Uint8 r, g, b; | ||
433 | r = *((bits)+Surface->format->Rshift/8); | ||
434 | g = *((bits)+Surface->format->Gshift/8); | ||
435 | b = *((bits)+Surface->format->Bshift/8); | ||
436 | return SDL_MapRGB(Surface->format, r, g, b); | ||
437 | } | ||
438 | break; | ||
439 | case 4: | ||
440 | return *((Uint32 *)Surface->pixels + Y * Surface->pitch/4 + X); | ||
441 | break; | ||
442 | } | ||
443 | } | ||