Debian Codegraph @

src/gfx_sprites.cpp

raw
1 /**
2 * Graphics - sequences management
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2014, 2015 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * 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, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "gfx_sprites.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include "SDL_image.h"
33
34 #include "IOGfxPrimitives.h"
35 #include "gfx.h"
36 #include "ImageLoader.h"
37
38 #include "fastfile.h"
39 #include "io_util.h"
40 #include "log.h"
41 #include "paths.h"
42 #include "dinkini.h"
43
44 /*
45 External global variables in use:
46 seq[], GFX_k[], k[]
47 */
48
49 /* TODO: get rid of either k or GFX_k */
50 struct pic_info k[MAX_SPRITES]; // Sprite data
51 struct GFX_pic_info GFX_k[MAX_SPRITES]; // Sprite data (SDL)
52
53 struct sequence seq[MAX_SEQUENCES];
54
55
56 void(*gfx_sprites_loading_listener)() = NULL;
57
58 /**
59 * Free memory used by sprites. It's not much useful in itself, since
60 * it's only called when we're exiting the game, but it does avoid
61 * memory leak warnings when FreeDink is analyzed by Valgrind or other
62 * memory checkers. It also supports loading and unloading the
63 * subsystem several times.
64 */
65 void sprites_unload(void) {
66 int i = 0;
67 for (i = 0; i < MAX_SPRITES; i++) {
68 if (GFX_k[i].k != NULL)
69 delete GFX_k[i].k;
70 GFX_k[i].k = NULL;
71 }
72 for (i = 0; i < MAX_SEQUENCES; i++) {
73 if (seq[i].ini != NULL)
74 free(seq[i].ini);
75 seq[i].ini = NULL;
76 }
77 }
78
79
80 /**
81 * Return the next available graphic slot
82 */
83 static int next_slot()
84 {
85 /* Start index at 1 instead of 0. A few parts in the game rely on
86 this (e.g. if getpic(...) < 1). */
87 /* TODO: I noticed that sprite in slot 0 (by default, this would be
88 a small white square) would be displayed temporarily on the
89 screen in some situations.. */
90 int i = 1;
91 while (i < MAX_SPRITES && GFX_k[i].k != NULL)
92 i++;
93 return i;
94 /* Callee will need to check if i >= MAX_SPRITES and fail if
95 necessary */
96 }
97
98 /**
99 * Free all graphic slots used by given sequence
100 */
101 static void free_seq(int seq_no) {
102 int i = 1;
103 int slot_index = -1;
104 while (i < MAX_FRAMES_PER_ABUSED_SEQUENCE+1
105 && (slot_index = seq[seq_no].frame[i]) != 0) {
106 delete GFX_k[slot_index].k;
107 GFX_k[slot_index].k = NULL;
108 i++;
109 }
110 /* 0 means end-of-sequence, no more frames */
111 if (i == MAX_FRAMES_PER_ABUSED_SEQUENCE+1)
112 log_error("Invalid sequence %d, just avoided a buffer overflow\n", seq_no);
113 }
114
115
116 void load_sprite_pak(char seq_path_prefix[100], int seq_no, int delay, int xoffset, int yoffset,
117 rect hardbox, int flags, /*bool*/int samedir) {
118 char fname[20];
119 char crap[200];
120
121 int notanim = 0, black = 0, leftalign = 0;
122 if ((flags & DINKINI_NOTANIM) == DINKINI_NOTANIM)
123 notanim = 1;
124 if ((flags & DINKINI_BLACK) == DINKINI_BLACK)
125 black = 1;
126 if ((flags & DINKINI_LEFTALIGN) == DINKINI_LEFTALIGN)
127 leftalign = 1;
128
129 /* If the sequence already exists, free it first */
130 free_seq(seq_no);
131
132 if (gfx_sprites_loading_listener)
133 gfx_sprites_loading_listener();
134
135 char *seq_dirname = pdirname(seq_path_prefix);
136 int n = strlen(seq_path_prefix) - strlen(seq_dirname)-1;
137 char *fullpath = NULL;
138 strcpy(fname, &seq_path_prefix[strlen(seq_path_prefix) - n]);
139 sprintf(crap, "%s/dir.ff", seq_dirname);
140 if (samedir)
141 fullpath = paths_dmodfile(crap);
142 else
143 fullpath = paths_fallbackfile(crap);
144
145 if (!FastFileInit(fullpath, 5)) {
146 log_error("Could not load dir.ff art file %s", crap);
147 free(fullpath);
148 free(seq_dirname);
149 return;
150 }
151 free(seq_dirname);
152 /* keep 'fullpath' for error messages, free() it later */
153
154
155 int oo;
156 for (oo = 1; oo <= MAX_FRAMES_PER_SEQUENCE; oo++) {
157 int myslot = next_slot();
158 if (myslot >= MAX_SPRITES) {
159 log_error("No sprite slot available! Index %d out of %d.",
160 myslot, MAX_SPRITES);
161 break;
162 }
163
164 char *leading_zero = NULL;
165 //load sprite
166 if (oo < 10) leading_zero = "0"; else leading_zero = "";
167 sprintf(crap, "%s%s%d.bmp", fname, leading_zero, oo);
168
169 FF_Handle* pfile = FastFileOpen(crap);
170
171 if (pfile == NULL)
172 /* File not present in this fastfile - either missing file or
173 end of sequence */
174 break;
175
176 // GFX
177 SDL_Surface* surf = NULL;
178 SDL_RWops *rw = FastFileLock(pfile);
179 if (rw == NULL) {
180 /* rwops error? */
181 log_error("Failed to open '%s' in fastfile '%s'", crap, fullpath);
182 } else {
183 /* We use IMG_Load_RW instead of SDL_LoadBMP because there
184 is no _RW access in plain SDL. However there is no
185 intent to support anything else than 8bit BMPs. */
186 surf = IMG_Load_RW(rw, 1); // auto free()
187 if (surf == NULL)
188 log_error("Failed to load %s from fastfile %s: %s", crap, fullpath, SDL_GetError());
189 }
190 if (surf == NULL) {
191 log_error("Failed to load %s from fastfile %s (see error above)", crap, fullpath);
192 FastFileClose(pfile);
193 break;
194 }
195 if (surf->format->BitsPerPixel != 8) {
196 log_error("Failed to load %s from fastfile %s:"
197 " only 8bit paletted bitmaps are supported in dir.ff archives.",
198 crap, fullpath);
199 SDL_FreeSurface(surf);
200 continue;
201 }
202
203 // Palettes and transparency
204
205 /* Note: in the original engine, for efficiency, no palette
206 conversion was done for sprite paks - they needed to use
207 the Dink Palette (otherwise weird colors would appear!)) */
208 /* The engine suffered a DX limitation: palette indexes 0 and
209 255 fixed fixed to black and white respectively. This is
210 also the opposite of the Dink BMP palette indexes. This
211 causes troubles when skipping palette conversion (here),
212 during fade_down()/fade_up() (255/white pixels can't be
213 darkened) and in DinkC's fillcolor(index). This is why this
214 function replaced black with brighter black and white with
215 darker white. */
216 /* In FreeDink palette conversion is done in
217 load_bmp_internal, so we mainly care about avoiding white
218 pixels during fade_down(), and only because we reproduced
219 the palette limitation so as to support dynamic
220 palette-changing tricks (cf. Lyna's Story) as well as
221 having readable white text during fade_down(). Maintaining
222 compatibility with fillcolor() is also important (although
223 forcing 0 and 255 indexes could be done in that function
224 only). But we might consider getting rid of it entirely. We
225 just have to make sure a dir.ff LEFTALIGN has no
226 transparency, otherwise the experience counter digits in
227 the status bar will become transparent. */
228 /* Using bitmap's own palette in truecolor mode, because at
229 least one D-Mod (The Blacksmith's Trail) ships with a
230 24bit, hence palette-less, tiles/ts01.bmp, and graphics
231 stay clean in v1.08 -truecolor !-dinkpal. */
232
233 /* Disable palette conversion in future blits */
234 if (!truecolor)
235 SDL_SetPaletteColors(surf->format->palette,
236 ImageLoader::blitFormat->format->palette->colors, 0, 256);
237
238 Uint8 *p = (Uint8 *)surf->pixels;
239 Uint8 *last = p + surf->h * surf->pitch;
240
241 if (leftalign)
242 {
243 // brighten black and darken white
244 while (p < last)
245 {
246 if (*p == 0) // white
247 *p = 30; // darker white
248 else if (*p == 255) // black
249 *p = 249; // brighter black
250 p++;
251 }
252 }
253 else if (black)
254 {
255 // darken white and set black as transparent
256 while (p < last)
257 {
258 if (*p == 0) // white
259 *p = 30; // darker white
260 p++;
261 }
262 SDL_SetColorKey(surf, SDL_TRUE, 255);
263 } else {
264 // brighten black and set white as transparent
265 while (p < last) {
266 if (*p == 255) // black in Dink palette
267 *p = 249; // brighter black
268 p++;
269 }
270 SDL_SetColorKey(surf, SDL_TRUE, 0);
271 }
272
273 k[myslot].box.top = 0;
274 k[myslot].box.left = 0;
275 k[myslot].box.right = surf->w;
276 k[myslot].box.bottom = surf->h;
277
278 GFX_k[myslot].k = g_display->upload(surf);
279 surf = NULL;
280
281 /* Define the offsets / center of the image */
282 if (yoffset > 0) {
283 // explicitely set center
284 k[myslot].yoffset = yoffset;
285 } else {
286 if (oo > 1 && notanim)
287 // copy first frame info
288 k[myslot].yoffset = k[seq[seq_no].frame[1]].yoffset;
289 else
290 // compute default center
291 k[myslot].yoffset = (k[myslot].box.bottom - (k[myslot].box.bottom / 4))
292 - (k[myslot].box.bottom / 30);
293 }
294
295 if (xoffset > 0) {
296 // explicitely set center
297 k[myslot].xoffset = xoffset;
298 } else {
299 if (oo > 1 && notanim)
300 // copy first frame info
301 k[myslot].xoffset = k[seq[seq_no].frame[1]].xoffset;
302 else
303 // compute default center
304 k[myslot].xoffset = (k[myslot].box.right - (k[myslot].box.right / 2))
305 + (k[myslot].box.right / 6);
306 }
307 //ok, setup main offsets, lets build the hard block
308
309 if (hardbox.right > 0) {
310 //forced setting
311 k[myslot].hardbox.left = hardbox.left;
312 k[myslot].hardbox.right = hardbox.right;
313 } else {
314 //guess setting
315 int work = k[myslot].box.right / 4;
316 k[myslot].hardbox.left -= work;
317 k[myslot].hardbox.right += work;
318 }
319
320 if (hardbox.bottom > 0) {
321 k[myslot].hardbox.top = hardbox.top;
322 k[myslot].hardbox.bottom = hardbox.bottom;
323 } else {
324 int work = k[myslot].box.bottom / 10;
325 k[myslot].hardbox.top -= work;
326 k[myslot].hardbox.bottom += work;
327 }
328 FastFileClose(pfile);
329
330 seq[seq_no].frame[oo] = myslot;
331 seq[seq_no].delay[oo] = delay;
332 }
333 FastFileFini();
334
335 /* Mark end-of-sequence */
336 seq[seq_no].frame[oo] = 0;
337 /* Length: inaccurate if 'set_frame_frame' is used */
338 seq[seq_no].len = oo - 1;
339
340 if (oo == 1)
341 log_error("Sprite_load_pak error: Couldn't load %s in %s.", crap, fullpath);
342 free(fullpath);
343 }
344
345
346 /* Load sprite, either from a dir.ff pack (delegated to
347 load_sprite_pak), either from a BMP file */
348 /* - seq_path_prefix: path to the file, relative to the current game (dink or dmod) */
349 /* - not_anim: reuse xoffset and yoffset from the first frame of the animation (misnomer) */
350 void load_sprites(char seq_path_prefix[100], int seq_no, int delay, int xoffset, int yoffset,
351 rect hardbox, int flags) {
352 char crap[200];
353 char *fullpath = NULL;
354 int use_fallback = 0;
355
356 int notanim = 0, black = 0;
357 if ((flags & DINKINI_NOTANIM) == DINKINI_NOTANIM)
358 notanim = 1;
359 if ((flags & DINKINI_BLACK) == DINKINI_BLACK)
360 black = 1;
361
362 if (gfx_sprites_loading_listener)
363 gfx_sprites_loading_listener();
364
365
366 /* Order: */
367 /* - dmod/.../dir.ff */
368 /* - dmod/.../...01.BMP */
369 /* - ../dink/.../dir.ff */
370 /* - ../dink/.../...01.BMP */
371 char *seq_dirname = pdirname(seq_path_prefix);
372 int exists = 0;
373
374 if (!exists) {
375 sprintf(crap, "%s/dir.ff", seq_dirname);
376 fullpath = paths_dmodfile(crap);
377 exists = exist(fullpath);
378 free(fullpath);
379 if (exists) {
380 free(seq_dirname);
381 load_sprite_pak(seq_path_prefix, seq_no, delay, xoffset, yoffset,
382 hardbox, flags, /*true*/1);
383 return;
384 }
385 }
386
387 if (!exists) {
388 sprintf(crap, "%s01.BMP",seq_path_prefix);
389 fullpath = paths_dmodfile(crap);
390 exists = exist(fullpath);
391 free(fullpath);
392 }
393
394 if (!exists) {
395 sprintf(crap, "%s/dir.ff", seq_dirname);
396 fullpath = paths_fallbackfile(crap);
397 exists = exist(fullpath);
398 free(fullpath);
399 if (exists) {
400 free(seq_dirname);
401 load_sprite_pak(seq_path_prefix, seq_no, delay, xoffset, yoffset,
402 hardbox, flags, /*false*/0);
403 return;
404 }
405 }
406
407 if (!exists) {
408 /* Let's look for the BMP below */
409 use_fallback = 1;
410 }
411
412 free(seq_dirname);
413
414
415 /* If the sequence already exists, free it first */
416 free_seq(seq_no);
417
418 /* Load the whole sequence (prefix-01.bmp, prefix-02.bmp, ...) */
419 int oo;
420 for (oo = 1; oo <= MAX_FRAMES_PER_ABUSED_SEQUENCE; oo++) {
421 int myslot = next_slot();
422 if (myslot >= MAX_SPRITES) {
423 log_error("No sprite slot available! Index %d out of %d.",
424 myslot, MAX_SPRITES);
425 break;
426 }
427
428 FILE *in = NULL;
429 char *leading_zero = NULL;
430 if (oo < 10) leading_zero = "0"; else leading_zero = "";
431 sprintf(crap, "%s%s%d.bmp", seq_path_prefix, leading_zero, oo);
432
433 /* Set the pixel data */
434 if (use_fallback)
435 in = paths_fallbackfile_fopen(crap, "rb");
436 else
437 in = paths_dmodfile_fopen(crap, "rb");
438
439 SDL_Surface* surf = ImageLoader::loadToBlitFormat(in);
440 if (surf == NULL) {
441 // end of sequence
442 break;
443 }
444
445 /** Configure current frame **/
446 /* Set transparent color: either black or white */
447 if (black)
448 SDL_SetColorKey(surf, SDL_TRUE,
449 SDL_MapRGB(surf->format, 0, 0, 0));
450 else
451 SDL_SetColorKey(surf, SDL_TRUE,
452 SDL_MapRGB(surf->format, 255, 255, 255));
453
454 /* Fill in .box; this was previously done in DDSethLoad; in
455 the future we could get rid of the .box field and rely
456 directly on SDL_Surface's .w and .h fields instead: */
457 k[myslot].box.top = 0;
458 k[myslot].box.left = 0;
459 k[myslot].box.right = surf->w;
460 k[myslot].box.bottom = surf->h;
461
462 GFX_k[myslot].k = g_display->upload(surf);
463
464 /* Define the offsets / center of the image */
465 if (yoffset > 0) {
466 // explicitely set center
467 k[myslot].yoffset = yoffset;
468 } else {
469 if (oo > 1 && notanim)
470 // copy first frame info
471 k[myslot].yoffset = k[seq[seq_no].frame[1]].yoffset;
472 else
473 // compute default center
474 k[myslot].yoffset = (k[myslot].box.bottom - (k[myslot].box.bottom / 4))
475 - (k[myslot].box.bottom / 30);
476 }
477
478 if (xoffset > 0) {
479 // explicitely set center
480 k[myslot].xoffset = xoffset;
481 } else {
482 if (oo > 1 && notanim)
483 // copy first frame info
484 k[myslot].xoffset = k[seq[seq_no].frame[1]].xoffset;
485 else
486 // compute default center
487 k[myslot].xoffset = (k[myslot].box.right - (k[myslot].box.right / 2))
488 + (k[myslot].box.right / 6);
489 }
490 //ok, setup main offsets, lets build the hard block
491
492 if (hardbox.right > 0) {
493 //forced setting
494 k[myslot].hardbox.left = hardbox.left;
495 k[myslot].hardbox.right = hardbox.right;
496 } else {
497 //default setting
498 int work = k[myslot].box.right / 4;
499 k[myslot].hardbox.left -= work;
500 k[myslot].hardbox.right += work;
501 }
502
503 if (hardbox.bottom > 0) {
504 //forced setting
505 k[myslot].hardbox.top = hardbox.top;
506 k[myslot].hardbox.bottom = hardbox.bottom;
507 } else {
508 //default setting
509 /* eg: graphics\dink\push\ds-p2- and
510 graphics\effects\comets\sm-comt2\fbal2- */
511 int work = k[myslot].box.bottom / 10;
512 k[myslot].hardbox.top -= work;
513 k[myslot].hardbox.bottom += work;
514 }
515
516 seq[seq_no].frame[oo] = myslot;
517 seq[seq_no].delay[oo] = delay;
518 }
519
520 /* Mark end-of-sequence */
521 seq[seq_no].frame[oo] = 0;
522 /* Length: inaccurate if 'set_frame_frame' is used */
523 seq[seq_no].len = oo - 1;
524
525 /* oo == 1 => not even one sprite was loaded, error */
526 /* oo > 1 => the sequence ends */
527
528 if (oo == 1) {
529 /* First frame didn't load! */
530 log_error("load_sprites: anim '%s' not found: couldn't open '%s'", seq_path_prefix, crap);
531 }
532 }
533
534 /**
535 * Set the dink.ini / init() line for this sequence.
536 */
537 void seq_set_ini(int seq_no, char *line)
538 {
539 /* Check if we are not attempting to replace a line by itself
540 (e.g. when a sequence is lazy-loaded) and free previous line. */
541 if (seq[seq_no].ini != line)
542 {
543 if (seq[seq_no].ini != NULL)
544 free(seq[seq_no].ini);
545 seq[seq_no].ini = strdup(line);
546 }
547 }
548
549
550 // ********* CHECK TO SEE IF THIS CORD IS ON A HARD SPOT *********
551 /*bool*/int not_in_this_base(int seq, int base)
552 {
553
554 int realbase = (seq / 10) * 10;
555
556
557 if (realbase != base)
558 {
559
560
561 return(/*true*/1);
562 }
563 else
564 {
565 return(/*false*/0);
566 }
567 }
568
569 /*bool*/int in_this_base(int seq, int base)
570 {
571
572 int realbase = (seq / 10) * 10;
573 if (realbase == base)
574 {
575
576 // Msg("TRUE - Ok, realbase is %d, compared to the base, which is %d.", realbase, base);
577 return(/*true*/1);
578 }
579 else
580 {
581 // Msg("FALSE - Ok, realbase is %d, compared to the base, which is %d.", realbase, base);
582
583 return(/*false*/0);
584 }
585 }