Debian Codegraph @

src/fuente2.cc

raw symbols SCIP
1 /*
2 * Don Ceferino Hazaña - video game similary to Super Pang!
3 * Copyright (c) 2004, 2005 Hugo Ruscitti
4 * web site: http://www.loosersjuegos.com.ar
5 *
6 * This file is part of Don Ceferino Hazaña (ceferino).
7 * Written by Hugo Ruscitti <[email protected]>
8 *
9 * Don Ceferino Hazaña is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * Don Ceferino Hazaña 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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
25 #include "fuente2.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include "int.h"
30 #include "utils.h"
31
32
33
34 fuente2 :: fuente2 (int modo)
35 {
36 char ruta [1024] = DATADIR "/ima/fuente_2.png";
37
38 printf (_("+ Loading: %s\n"), ruta);
39 ima = IMG_Load (ruta);
40
41 if (ima == NULL)
42 printf ("error: %s\n", SDL_GetError ());
43
44 if (modo)
45 {
46 ima = reescalar_mitad(ima);
47
48 if(ima==NULL)
49 {
50 printf(_("error : error reshaping the image: '%s' "),\
51 ruta);
52 imprimir_error();
53 exit(1);
54 }
55 }
56
57 modo_reducido = modo;
58 letras = 0;
59
60 cargar_identificar_recs ();
61 }
62
63
64
65 void fuente2 :: guardar_rect (int i, int x, int y, int w, int h)
66 {
67 rects [i].x = x;
68 rects [i].y = y;
69 rects [i].w = w;
70 rects [i].h = h;
71 }
72
73
74 bool fuente2 :: es_pixel_opaco (SDL_Surface * ima, int fila, int col)
75 {
76 int bpp = ima->format->BytesPerPixel;
77 Uint8 r, g, b, a;
78 Uint8 * p;
79 Uint32 color;
80
81 p = (Uint8 *) ima->pixels;
82 p += col * bpp + fila * ima->pitch;
83
84 switch (bpp)
85 {
86 case 1:
87 color = * p;
88 break;
89
90 case 2:
91 color = * (Uint16 *) p;
92 break;
93
94 case 3:
95 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
96 color = p[0] << 16 | p[1] << 8 | p[2];
97 else
98 color = p[0] | p[1] << 8 | p[2] << 16;
99 break;
100
101 case 4:
102 color = * (Uint32 *) p;
103 break;
104
105 default:
106 printf (_("invalid bpp: %d\n"), bpp);
107 break;
108 }
109
110 SDL_GetRGBA (color, ima->format, &r, &g, &b, &a);
111
112 if (r == KEY_R && g == KEY_G && b == KEY_B && a == KEY_A)
113 return false;
114
115 return true;
116 }
117
118
119 bool fuente2 :: es_columna_vacia (SDL_Surface * ima, int col)
120 {
121 int fila;
122
123 for (fila = 0; fila < (ima->h / 2); fila ++)
124 {
125 if (es_pixel_opaco (ima, fila, col))
126 return false;
127 }
128
129 return true;
130 }
131
132
133 void fuente2 :: cargar_identificar_recs (void)
134 {
135 int i;
136 int hay_letra = 0;
137 int indice = 0;
138 int i_ant;
139
140 for (i = 0; i < ima->w; i++)
141 {
142 if (es_columna_vacia (ima, i))
143 {
144 if (hay_letra)
145 {
146 hay_letra = 0;
147 guardar_rect (indice, i_ant, 0, \
148 i - i_ant, ima->h / 2);
149 indice ++;
150 }
151 }
152 else
153 {
154 if (!hay_letra)
155 {
156 hay_letra = 1;
157 i_ant = i;
158 }
159 }
160 }
161
162 letras = indice;
163 printf (_("Found %d characters in bitmap\n"), indice);
164 }
165
166 fuente2 :: ~fuente2 ()
167 {
168 SDL_FreeSurface (ima);
169 }
170
171
172 SDL_Rect fuente2 :: obtener_referencia (char letra, bool transparente)
173 {
174 static SDL_Rect src;
175 unsigned char l = (unsigned char)letra;
176
177 if (letra == ' ' || letra == '\0' || letra == '\n')
178 {
179 src = rects [0];
180 return src;
181 }
182
183 /* latin1: 0x21-0x7E, 0xA1-0xFF */
184 if (l >= 33 && l <= 126) {
185 src = rects[l - 33];
186 } else if (l >= 161) {
187 src = rects[l - 67];
188 } else {
189 src = rects[0];
190 printf (_("Character %u not found in font tiles\n"), (unsigned)l);
191 }
192 if (transparente) {
193 src.y += src.h;
194 }
195 return src;
196 }
197
198
199 int fuente2 :: imprimir_letra (SDL_Surface * destino, char letra, int x, int y, SDL_Rect * rect, bool transparente)
200 {
201 SDL_Rect dst = {x, y, 0, 0};
202 SDL_Rect src;
203
204 if (modo_reducido)
205 {
206 dst.x /= 2;
207 dst.y /= 2;
208 }
209
210 src = obtener_referencia (letra, transparente);
211
212 if (letra == ' ')
213 {
214 if (rect)
215 (*rect) = dst;
216
217 return src.w;
218 }
219
220 src = obtener_referencia (letra, transparente);
221 SDL_BlitSurface (ima, & src, destino, & dst);
222
223 if (rect)
224 (*rect) = dst;
225
226 return get_w (letra);
227 }
228
229
230 void fuente2 :: myprintf (SDL_Surface * dst, int x, int y, SDL_Rect * rect, \
231 int * nrect, bool transparente, enum posiciones posicion, \
232 const char * format, ...)
233 {
234 char buffer [1024];
235 va_list va;
236 int i;
237
238 if (! format)
239 return;
240
241 va_start (va, format);
242 vsprintf (buffer, format, va);
243
244
245 switch (posicion)
246 {
247 case CONSALTO:
248 imprimir_con_salto (dst, buffer, x, y, rect, nrect, \
249 transparente);
250 break;
251
252 case NINGUNO:
253 imprimir (dst, buffer, x, y, rect, nrect, transparente);
254 break;
255
256 case CENTRADO:
257 x -= (longitud (buffer) / 2);
258 imprimir (dst, buffer, x, y, rect, nrect, transparente);
259 break;
260
261 }
262
263 va_end (va);
264 }
265
266
267 void fuente2 :: myprintf_inferior (SDL_Surface * dst, SDL_Rect * rect, \
268 int * nrect, bool transparente, const char * format, ...)
269 {
270 char buffer [1024];
271 va_list va;
272 int x1, y1;
273
274 if (! format)
275 return;
276
277 va_start (va, format);
278 vsprintf (buffer, format, va);
279
280 alinear_inferior (& x1, & y1, buffer);
281 myprintf (dst, x1, y1, rect, nrect, transparente, CONSALTO, buffer);
282
283 va_end (va);
284 }
285
286 int fuente2 :: get_h (void)
287 {
288 int altura = (int) (rects [0].h / 1.5);
289
290 if (modo_reducido)
291 return altura * 2;
292 else
293 return altura;
294 /*
295 (modo_reducido)? (rects [0].h * 2) / 1.5:
296 return altura;
297 */
298 }
299
300
301 /*!
302 * \brief busca los valor de x e y para que la cadena se pueda imprimr al pie
303 * de la página
304 */
305 void fuente2 :: alinear_inferior (int * x1, int * y1, char * cadena)
306 {
307 int f = 0;
308 int c = 0;
309 char * palabra = cadena;
310 int limite_w = 640 - 2 * 20 + get_w (' ');/*(modo_reducido)? 320: 640;*/
311 int limite_h = 480;/*(modo_reducido)? 240: 480;*/
312 int width;
313
314 while (palabra)
315 {
316 width = ancho_palabra (palabra) + get_w (' ');
317 if (c + width > limite_w || *palabra == '\n')
318 {
319 /* salta una linea */
320 c = width;
321 f += get_h ();
322 }
323 else
324 {
325 c += width;
326 }
327
328 palabra = saltar_palabra (palabra);
329
330 if (palabra)
331 {
332 palabra += 1;
333 }
334 }
335
336
337 /* se aplica la alineación inferior */
338 (* x1) = 20;
339 (* y1) = limite_h - (f + get_h () * 2);
340 }
341
342
343
344 int fuente2 :: ancho_palabra (char * palabra)
345 {
346 int i;
347 int ancho = 0;
348 SDL_Rect rect;
349
350 for (i = 0; palabra [i] != ' ' && palabra [i] != '\0' \
351 && palabra [i] != '\n'; i ++)
352 {
353 ancho += get_w (palabra [i]);
354 }
355
356 return ancho;
357 }
358
359
360 int fuente2 :: get_w (char letra)
361 {
362 SDL_Rect rect;
363
364 rect = obtener_referencia (letra, false);
365
366 if (modo_reducido)
367 return rect.w * 2;
368 else
369 return rect.w;
370 }
371
372
373 void fuente2 :: imprimir_con_salto (SDL_Surface * dst, char * cadena, int x, \
374 int y, SDL_Rect *rect, int * nrect, bool transparente)
375 {
376 int limite_w = 640 - 2 * 20 + get_w (' ');/*(modo_reducido)? 320: 640;*/
377 char * palabra = cadena;
378 int width;
379 x = 0;
380
381 while (palabra)
382 {
383 width = ancho_palabra (palabra) + get_w (' ');
384 if (x + width > limite_w)
385 {
386 x = 0;
387 y += get_h ();
388 }
389
390 imprimir_palabra (dst, palabra, 20 + x, y, rect, nrect,transparente);
391
392 x += width;
393
394 palabra = saltar_palabra (palabra);
395
396 if (palabra)
397 {
398 if (palabra [0] == '\n')
399 {
400 x = 0;
401 y += get_h ();
402 }
403
404 palabra += 1;
405 }
406 }
407 }
408
409
410 char * fuente2 :: saltar_palabra (char * cadena)
411 {
412 int i;
413
414 for (i = 0; cadena [i]; i ++)
415 {
416 if (cadena [i] == ' ' || cadena [i] == '\n')
417 return cadena + i;
418 }
419
420 if (cadena [i] == '\0')
421 return NULL;
422 }
423
424 void fuente2 :: imprimir (SDL_Surface * dst, char * cadena, int x, \
425 int y, SDL_Rect *rect, int * nrect, bool transparente)
426 {
427 int dx = 0;
428
429 for (int i = 0; cadena [i]; i++)
430 {
431 int w;
432
433 if (rect)
434 {
435 w = imprimir_letra (dst, cadena [i], x + dx, y, \
436 rect + * nrect, transparente);
437 }
438 else
439 {
440 w = imprimir_letra (dst, cadena [i], x + dx, y, NULL, \
441 transparente);
442 }
443
444 dx += w;
445
446 if (nrect)
447 {
448 (* nrect) ++;
449 }
450 }
451 }
452
453
454
455 void fuente2 :: imprimir_palabra (SDL_Surface * dst, char * cadena, int x, \
456 int y, SDL_Rect *rect, int * nrect, bool transparente)
457 {
458 int i = 0;
459 int dx = 0;
460
461 while (cadena [i] && cadena [i] != ' ' && cadena [i] != '\n')
462 {
463 int w;
464
465 if (rect)
466 {
467 w = imprimir_letra (dst, cadena [i], x + dx, y, \
468 rect + * nrect, transparente);
469 }
470 else
471 {
472 w = imprimir_letra (dst, cadena [i], x + dx, y, NULL, \
473 transparente);
474 }
475
476 dx += w;
477
478 if (nrect)
479 {
480 (* nrect) ++;
481 }
482
483 i ++;
484 }
485 }
486
487 int fuente2 :: longitud (char * cadena)
488 {
489 int w = 0;
490
491 for (int i = 0; cadena [i]; i ++)
492 {
493 w += get_w (cadena [i]) + 2;
494 }
495
496 return w;
497 }
498
499