improved smooth movement animation for BD engine (complete rewrite)
[rocksndiamonds.git] / src / game_bd / bd_graphics.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "main_bd.h"
18
19
20 // !!! (can be removed later) !!!
21 #define DO_GFX_SANITY_CHECK     TRUE
22
23 // distance to screen edge in cells when scrolling the screen
24 #define SCROLL_EDGE_DISTANCE    4
25
26 // these can't be larger than 31, or they mess up utf8 coding or are the same as some ascii letter
27 #define GD_DOWN_CHAR            1
28 #define GD_LEFT_CHAR            2
29 #define GD_UP_CHAR              3
30 #define GD_RIGHT_CHAR           4
31
32 #define GD_BALL_CHAR            5
33 #define GD_UNCHECKED_BOX_CHAR   6
34 #define GD_CHECKED_BOX_CHAR     7
35
36 #define GD_PLAYER_CHAR          8
37 #define GD_DIAMOND_CHAR         9
38 #define GD_SKELETON_CHAR        11
39 #define GD_KEY_CHAR             12
40 #define GD_COMMENT_CHAR         13
41
42 // pointer to tile bitmap (which may be prepared with level-specific colors)
43 static Bitmap *gd_tile_bitmap = NULL;
44 // pointer to reference tile bitmap (without level-specific colors)
45 static Bitmap *gd_tile_bitmap_reference = NULL;
46
47 // optional title screen bitmaps (foreground and background)
48 static Bitmap *gd_title_screen_bitmaps[2] = { NULL, NULL };
49
50 // screen area
51 Bitmap *gd_screen_bitmap = NULL;
52
53 static int play_area_w = 0;
54 static int play_area_h = 0;
55
56 static int scroll_x, scroll_y;
57
58 // quit, global variable which is set to true if the application should quit
59 boolean gd_quit = FALSE;
60
61 static int cell_size = 0;
62
63 // graphic info for game objects/frames and players/actions/frames
64 struct GraphicInfo_BD graphic_info_bd_object[O_MAX_ALL][8];
65 // graphic info for game graphics template for level-specific colors
66 struct GraphicInfo_BD graphic_info_bd_color_template;
67
68 static inline int c64_png_colors(int r, int g, int b, int a)
69 {
70   static const int c64_png_cols[] =
71   {
72     // ABGR
73
74     /* 0000 */ 0,       // transparent
75     /* 0001 */ 0,
76     /* 0010 */ 0,
77     /* 0011 */ 0,
78     /* 0100 */ 0,
79     /* 0101 */ 0,
80     /* 0110 */ 0,
81     /* 0111 */ 0,
82     /* 1000 */ 1,       // black - background
83     /* 1001 */ 2,       // red - foreg1
84     /* 1010 */ 5,       // green - amoeba
85     /* 1011 */ 4,       // yellow - foreg3
86     /* 1100 */ 6,       // blue - slime
87     /* 1101 */ 3,       // purple - foreg2
88     /* 1110 */ 7,       // black around arrows (used in editor) is coded as cyan
89     /* 1111 */ 8,       // white is the arrow
90   };
91
92   int c = ((a >> 7) * 8 +
93            (b >> 7) * 4 +
94            (g >> 7) * 2 +
95            (r >> 7) * 1);
96
97   return c64_png_cols[c];
98 }
99
100 void set_cell_size(int s)
101 {
102   cell_size = s;
103 }
104
105 void set_play_area(int w, int h)
106 {
107   play_area_w = w;
108   play_area_h = h;
109 }
110
111 void gd_init_play_area(void)
112 {
113   set_play_area(SXSIZE, SYSIZE);
114 }
115
116 /*
117   logical_size: logical pixel size of playfield, usually larger than the screen.
118   physical_size: visible part. (remember: player_x-x1!)
119
120   center: the coordinates to scroll to.
121   exact: scroll exactly
122   start: start scrolling if difference is larger than
123   to: scroll to, if started, until difference is smaller than
124   current
125
126   desired: the function stores its data here
127   speed: the function stores its data here
128
129   cell_size: size of one cell. used to determine if the play field is only a
130   slightly larger than the screen, in that case no scrolling is desirable
131 */
132 static boolean cave_scroll(int logical_size, int physical_size, int center, boolean exact,
133                            int *current, int *desired, int speed)
134 {
135   int max = MAX(0, logical_size - physical_size);
136   int edge_distance = SCROLL_EDGE_DISTANCE;
137   int cell_size = TILESIZE_VAR;
138   boolean changed = FALSE;
139
140   // start scrolling when player reaches certain distance to screen edge
141   int start = physical_size / 2 - cell_size * edge_distance;
142
143   // scroll so that the player is at the center; the allowed difference is this
144   int to = cell_size;
145
146   // if cave size smaller than the screen, no scrolling req'd
147   if (logical_size < physical_size)
148   {
149     *desired = 0;
150
151     if (*current != 0)
152     {
153       *current = 0;
154       changed = TRUE;
155     }
156
157     return changed;
158   }
159
160   if (logical_size <= physical_size + cell_size)
161   {
162     // if cave size is only a slightly larger than the screen, also no scrolling
163     // scroll to the middle of the cell
164     *desired = max / 2;
165   }
166   else
167   {
168     if (exact)
169     {
170       // if exact scrolling, just go exactly to the center.
171       *desired = center;
172     }
173     else
174     {
175       // hystheresis function.
176       // when scrolling left, always go a bit less left than player being at the middle.
177       // when scrolling right, always go a bit less to the right.
178       if (*current < center - start)
179         *desired = center - to;
180       if (*current > center + start)
181         *desired = center + to;
182     }
183   }
184
185   *desired = MIN(MAX(0, *desired), max);
186
187   if (*current < *desired)
188   {
189     *current = MIN(*current + speed, *desired);
190
191     changed = TRUE;
192   }
193
194   if (*current > *desired)
195   {
196     *current = MAX(*current - speed, *desired);
197
198     changed = TRUE;
199   }
200
201   return changed;
202 }
203
204 // just set current viewport to upper left.
205 void gd_scroll_to_origin(void)
206 {
207   scroll_x = 0;
208   scroll_y = 0;
209 }
210
211 int get_scroll_x(void)
212 {
213   return scroll_x / cell_size;
214 }
215
216 int get_scroll_y(void)
217 {
218   return scroll_y / cell_size;
219 }
220
221 int get_play_area_w(void)
222 {
223   return play_area_w / cell_size;
224 }
225
226 int get_play_area_h(void)
227 {
228   return play_area_h / cell_size;
229 }
230
231 static boolean player_out_of_window(GdGame *game, int player_x, int player_y)
232 {
233   // if not yet born, we treat as visible. so cave will run.
234   // the user is unable to control an unborn player, so this is the right behaviour.
235   if (game->cave->player_state == GD_PL_NOT_YET)
236     return FALSE;
237
238   // check if active player is outside drawing area. if yes, we should wait for scrolling
239   if ((player_x * cell_size) < scroll_x ||
240       (player_x * cell_size + cell_size - 1) > scroll_x + play_area_w)
241   {
242     // but only do the wait, if the player SHOULD BE visible, ie. he is inside
243     // the defined visible area of the cave
244     if (game->cave->player_x >= game->cave->x1 &&
245         game->cave->player_x <= game->cave->x2)
246       return TRUE;
247   }
248
249   if ((player_y * cell_size) < scroll_y ||
250       (player_y * cell_size + cell_size - 1) > scroll_y + play_area_h)
251   {
252     // but only do the wait, if the player SHOULD BE visible, ie. he is inside
253     // the defined visible area of the cave
254     if (game->cave->player_y >= game->cave->y1 &&
255         game->cave->player_y <= game->cave->y2)
256       return TRUE;
257   }
258
259   // player is inside visible window
260   return FALSE;
261 }
262
263 /*
264   SCROLLING
265   
266   scrolls to the player during game play.
267   called by drawcave
268   returns true, if player is not visible-ie it is out of the visible size in the drawing area.
269 */
270 boolean gd_scroll(GdGame *game, boolean exact_scroll, boolean immediate)
271 {
272   static int scroll_desired_x = 0, scroll_desired_y = 0;
273   static int scroll_speed_last = -1;
274   int player_x, player_y, visible_x, visible_y;
275   boolean changed;
276
277   // max scrolling speed depends on the speed of the cave.
278   // game moves cell_size_game * 1s / cave time pixels in a second.
279   // scrolling moves scroll speed * 1s / scroll_time in a second.
280   // these should be almost equal; scrolling speed a little slower.
281   // that way, the player might reach the border with a small probability,
282   // but the scrolling will not "oscillate", ie. turn on for little intervals as it has
283   // caught up with the desired position. smaller is better.
284   int scroll_speed = cell_size * 20 / game->cave->speed;
285
286   if (!setup.bd_scroll_delay)
287     exact_scroll = TRUE;
288
289   if (immediate)
290     scroll_speed = cell_size * MAX(game->cave->w, game->cave->h);
291
292   player_x = game->cave->player_x - game->cave->x1; // cell coordinates of player
293   player_y = game->cave->player_y - game->cave->y1;
294
295   // if player is outside visible playfield area, use faster scrolling
296   // (might happen when wrapping around the playfield, teleporting etc.)
297   if (player_out_of_window(game, player_x, player_y))
298     scroll_speed *= 4;
299
300   // if scrolling started with player outside visible playfield area, keep faster scrolling
301   if (scroll_speed_last > scroll_speed)
302     scroll_speed = scroll_speed_last;
303
304   // store current (potentially faster) scrolling speed for next time
305   scroll_speed_last = scroll_speed;
306
307   // pixel size of visible part of the cave (may be smaller in intermissions)
308   visible_x = (game->cave->x2 - game->cave->x1 + 1) * cell_size;
309   visible_y = (game->cave->y2 - game->cave->y1 + 1) * cell_size;
310
311   // cell_size contains the scaled size, but we need the original.
312   changed = FALSE;
313
314   if (cave_scroll(visible_x, play_area_w, player_x * cell_size + cell_size / 2 - play_area_w / 2,
315                   exact_scroll, &scroll_x, &scroll_desired_x, scroll_speed))
316     changed = TRUE;
317
318   if (cave_scroll(visible_y, play_area_h, player_y * cell_size + cell_size / 2 - play_area_h / 2,
319                   exact_scroll, &scroll_y, &scroll_desired_y, scroll_speed))
320     changed = TRUE;
321
322   // if scrolling, we should update entire screen.
323   if (changed)
324   {
325     int x, y;
326
327     for (y = 0; y < game->cave->h; y++)
328       for (x = 0; x < game->cave->w; x++)
329         game->gfx_buffer[y][x] |= GD_REDRAW;
330   }
331
332   // if no scrolling required, reset last (potentially faster) scrolling speed
333   if (!changed)
334     scroll_speed_last = -1;
335
336   // check if active player is visible at the moment.
337   return player_out_of_window(game, player_x, player_y);
338 }
339
340 // returns true, if the given surface seems to be a c64 imported image.
341 static boolean surface_has_c64_colors(SDL_Surface *surface)
342 {
343   boolean has_c64_colors = TRUE;        // default: assume C64 colors
344   const unsigned char *p;
345   int x, y;
346
347   if (surface->format->BytesPerPixel != 4)
348     return FALSE;
349
350   SDL_LockSurface(surface);
351
352   for (y = 0; y < surface->h && has_c64_colors; y++)
353   {
354     p = ((unsigned char *)surface->pixels) + y * surface->pitch;
355
356     for (x = 0; x < surface->w * surface->format->BytesPerPixel && has_c64_colors; x++)
357       if (p[x] != 0 && p[x] != 255)
358         has_c64_colors = FALSE;
359   }
360
361   SDL_UnlockSurface(surface);
362
363   return has_c64_colors;
364 }
365
366 boolean gd_bitmap_has_c64_colors(Bitmap *bitmap)
367 {
368   return surface_has_c64_colors(bitmap->surface);
369 }
370
371 // sets one of the colors in the indexed palette of an sdl surface to a GdColor.
372 static void set_surface_palette_color(SDL_Surface *surface, int index, GdColor col)
373 {
374   if (surface->format->BytesPerPixel != 1)
375     return;
376
377   SDL_Color c;
378
379   c.r = gd_color_get_r(col);
380   c.g = gd_color_get_g(col);
381   c.b = gd_color_get_b(col);
382
383   SDL_SetPaletteColors(surface->format->palette, &c, index, 1);
384 }
385
386 // takes a c64_gfx.png-coded 32-bit surface, and creates a paletted surface in our internal format.
387 static SDL_Surface *get_tile_surface_c64(SDL_Surface *surface, int scale_down_factor)
388 {
389   static SDL_Surface *tile_surface_c64 = NULL;
390   static unsigned char *pixels = NULL;
391   int width  = surface->w;
392   int height = surface->h;
393   int out = 0;
394   int x, y;
395
396   if (!surface_has_c64_colors(surface))
397     return NULL;
398
399   if (surface->format->BytesPerPixel != 4)
400     Fail("C64 style surface has wrong color depth -- should not happen");
401
402   if (tile_surface_c64 != NULL)
403     SDL_FreeSurface(tile_surface_c64);
404
405   checked_free(pixels);
406
407   pixels = checked_malloc(width * height);
408
409   SDL_LockSurface(surface);
410
411   for (y = 0; y < height; y++)
412   {
413     unsigned int *p = (unsigned int *)((char *)surface->pixels + y * surface->pitch);
414
415     for (x = 0; x < width; x++)
416     {
417       int r = (p[x] & surface->format->Rmask) >> surface->format->Rshift << surface->format->Rloss;
418       int g = (p[x] & surface->format->Gmask) >> surface->format->Gshift << surface->format->Gloss;
419       int b = (p[x] & surface->format->Bmask) >> surface->format->Bshift << surface->format->Bloss;
420       // should be:
421       // a = (p[x]&surface->format->Amask) >> surface->format->Ashift << surface->format->Aloss;
422       // but we do not use the alpha channel in sdash, so we just use 255 (max alpha)
423
424       pixels[out++] = c64_png_colors(r, g, b, 255);
425     }
426   }
427
428   SDL_UnlockSurface(surface);
429
430   // create new surface from pixel data
431   tile_surface_c64 =
432     SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 8, width, 0, 0, 0, 0);
433
434   if (tile_surface_c64 == NULL)
435     Fail("SDL_CreateRGBSurfaceFrom() failed: %s", SDL_GetError());
436
437   if (scale_down_factor > 1)
438   {
439     SDL_Surface *surface_old = tile_surface_c64;
440     int width_scaled  = width  / scale_down_factor;
441     int height_scaled = height / scale_down_factor;
442
443     // replace surface with scaled-down variant
444     tile_surface_c64 = SDLZoomSurface(surface_old, width_scaled, height_scaled);
445
446     // free previous (non-scaled) surface
447     SDL_FreeSurface(surface_old);
448   }
449
450   return tile_surface_c64;
451 }
452
453 static Bitmap *get_tile_bitmap_c64(GdCave *cave, SDL_Surface *surface)
454 {
455   static Bitmap *tile_bitmap_c64 = NULL;
456
457   if (surface == NULL)
458     return NULL;
459
460   if (tile_bitmap_c64 != NULL)
461     FreeBitmap(tile_bitmap_c64);
462
463   // set surface color palette to cave colors
464   set_surface_palette_color(surface, 0, 0);
465   set_surface_palette_color(surface, 1, gd_color_get_rgb(cave->color0));
466   set_surface_palette_color(surface, 2, gd_color_get_rgb(cave->color1));
467   set_surface_palette_color(surface, 3, gd_color_get_rgb(cave->color2));
468   set_surface_palette_color(surface, 4, gd_color_get_rgb(cave->color3));
469   set_surface_palette_color(surface, 5, gd_color_get_rgb(cave->color4));
470   set_surface_palette_color(surface, 6, gd_color_get_rgb(cave->color5));
471   set_surface_palette_color(surface, 7, 0);
472   set_surface_palette_color(surface, 8, 0);
473
474   // set background color to be transparent for masked tile bitmap
475   int bg_color = gd_color_get_rgb(cave->color0);
476   int bg_r = gd_color_get_r(bg_color);
477   int bg_g = gd_color_get_g(bg_color);
478   int bg_b = gd_color_get_b(bg_color);
479
480   // create bitmap from C64 surface
481   tile_bitmap_c64 = SDLGetBitmapFromSurface_WithMaskedColor(surface, bg_r, bg_g, bg_b);
482
483   return tile_bitmap_c64;
484 }
485
486 void gd_prepare_tile_bitmap(GdCave *cave, Bitmap *bitmap, int scale_down_factor)
487 {
488   static SDL_Surface *tile_surface_c64 = NULL;
489   static Bitmap *gd_tile_bitmap_original = NULL;
490   static int scale_down_factor_last = -1;
491
492   if (program.headless)
493     return;
494
495   // check if tile bitmap has changed (different artwork or tile size selected)
496   if (bitmap != gd_tile_bitmap_original || scale_down_factor != scale_down_factor_last)
497   {
498     // check if tile bitmap has limited C64 style colors
499     tile_surface_c64 = get_tile_surface_c64(bitmap->surface, scale_down_factor);
500
501     // store original tile bitmap from current artwork set and scaling factor
502     gd_tile_bitmap_original = bitmap;
503     scale_down_factor_last = scale_down_factor;
504
505     // store reference tile bitmap from current artwork set (may be changed later)
506     gd_tile_bitmap_reference = bitmap;
507   }
508
509   // check if tile bitmap should be colored for next game
510   if (tile_surface_c64 != NULL)
511   {
512     // set tile bitmap to bitmap with level-specific colors
513     gd_tile_bitmap = get_tile_bitmap_c64(cave, tile_surface_c64);
514   }
515   else
516   {
517     // no special tile bitmap available for this artwork set
518     gd_tile_bitmap = NULL;
519   }
520 }
521
522 void gd_set_tile_bitmap_reference(Bitmap *bitmap)
523 {
524   gd_tile_bitmap_reference = bitmap;
525 }
526
527 Bitmap *gd_get_tile_bitmap(Bitmap *bitmap)
528 {
529   // if no special tile bitmap available, keep using default tile bitmap
530   if (gd_tile_bitmap == NULL)
531     return bitmap;
532
533   // if default bitmap refers to tile bitmap, use special tile bitmap
534   if (bitmap == gd_tile_bitmap_reference)
535     return gd_tile_bitmap;
536
537   return bitmap;
538 }
539
540 // returns true if the element is a player
541 static inline boolean el_player(const int element)
542 {
543   return (gd_elements[element & O_MASK].properties & P_PLAYER) != 0;
544 }
545
546 // returns true if the element is walkable
547 static inline boolean el_walkable(const int element)
548 {
549   return (gd_elements[element & O_MASK].properties & P_WALKABLE) != 0;
550 }
551
552 // returns true if the element is diggable
553 static inline boolean el_diggable(const int element)
554 {
555   return (gd_elements[element & O_MASK].properties & P_DIGGABLE) != 0;
556 }
557
558 #if 0
559 // returns true if the element is collectible
560 static inline boolean el_collectible(const int element)
561 {
562   return (gd_elements[element & O_MASK].properties & P_COLLECTIBLE) != 0;
563 }
564
565 // returns true if the element is pushable
566 static inline boolean el_pushable(const int element)
567 {
568   return (gd_elements[element & O_MASK].properties & P_PUSHABLE) != 0;
569 }
570
571 // returns true if the element can move
572 static inline boolean el_can_move(const int element)
573 {
574   return (gd_elements[element & O_MASK].properties & P_CAN_MOVE) != 0;
575 }
576
577 // returns true if the element can fall
578 static inline boolean el_falling(const int element)
579 {
580   return (gd_elements[element & O_MASK].properties & P_FALLING) != 0;
581 }
582 #endif
583
584 // returns true if the element is growing
585 static inline boolean el_growing(const int element)
586 {
587   return (gd_elements[element & O_MASK].properties & P_GROWING) != 0;
588 }
589
590 // returns true if the element is exploding
591 static inline boolean el_explosion(const int element)
592 {
593   return (gd_elements[element & O_MASK].properties & P_EXPLOSION) != 0;
594 }
595
596 static void gd_drawcave_tile(Bitmap *dest, GdGame *game, int x, int y, boolean draw_masked)
597 {
598   void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
599     (draw_masked ? BlitBitmapMasked : BlitBitmap);
600   GdCave *cave = game->cave;
601   int sx = x * cell_size - scroll_x;
602   int sy = y * cell_size - scroll_y;
603   int dir_from = game->dir_buffer_from[y][x];
604   int dir_to = game->dir_buffer_to[y][x];
605   int tile = game->element_buffer[y][x];
606   int tile_last = game->last_element_buffer[y][x];
607   int tile_from = O_NONE;       // source element if element is moving (will be set later)
608   int tile_to = tile;           // target element if element is moving
609   int frame = game->animcycle;
610   boolean is_moving_from = (dir_from != GD_MV_STILL);
611   boolean is_moving_to   = (dir_to   != GD_MV_STILL);
612   boolean is_moving = (is_moving_from || is_moving_to);
613   boolean use_smooth_movements = use_bd_smooth_movements();
614
615   // if element is moving away from this tile, determine element that is moving
616   if (is_moving_from)
617   {
618     int dx = (dir_from == GD_MV_LEFT ? -1 : dir_from == GD_MV_RIGHT ? +1 : 0);
619     int dy = (dir_from == GD_MV_UP   ? -1 : dir_from == GD_MV_DOWN  ? +1 : 0);
620     int new_x = cave->getx(cave, x + dx, y + dy);
621     int new_y = cave->gety(cave, x + dx, y + dy);
622
623     tile_from = game->element_buffer[new_y][new_x];
624
625     // handle special case of player running into enemy/explosion from top or left side
626     if ((el_growing(tile_from) || el_explosion(tile_from)) && el_player(tile_last))
627       tile_from = tile_last;
628   }
629
630   // --------------------------------------------------------------------------------
631   // check if we should use smooth movement animations or not
632   // --------------------------------------------------------------------------------
633
634   // do not use smooth movement animation for player entering exit (engine stopped)
635   if (cave->player_state == GD_PL_EXITED)
636     use_smooth_movements = FALSE;
637
638   // never treat empty space as "moving" (source tile if player is snapping)
639   if (tile_from == O_SPACE)
640     use_smooth_movements = FALSE;
641
642   // do not use smooth movement animation for player stirring the pot
643   if (tile_from == O_PLAYER_STIRRING || tile_to == O_PLAYER_STIRRING)
644     use_smooth_movements = FALSE;
645
646   // do not use smooth movement animation for growing or exploding game elements
647   if (el_growing(tile) || el_explosion(tile))
648     use_smooth_movements = FALSE;
649
650 #if DO_GFX_SANITY_CHECK
651   if (use_native_bd_graphics_engine() && !setup.small_game_graphics && !program.headless)
652   {
653     struct GraphicInfo_BD *g = &graphic_info_bd_object[tile][frame];
654     int old_x = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) % GD_NUM_OF_CELLS_X;
655     int old_y = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) / GD_NUM_OF_CELLS_X;
656     int new_x = g->src_x / g->width;
657     int new_y = g->src_y / g->height;
658
659     if (new_x != old_x || new_y != old_y)
660     {
661       printf("::: BAD ANIMATION FOR TILE %d, FRAME %d [NEW(%d, %d) != OLD(%d, %d)] ['%s']\n",
662              tile, frame,
663              new_x, new_y,
664              old_x, old_y,
665              gd_elements[tile].name);
666     }
667   }
668 #endif
669
670   // if game element not moving (or no smooth movements requested), simply draw tile
671   if (!is_moving || !use_smooth_movements)
672   {
673     struct GraphicInfo_BD *g = &graphic_info_bd_object[tile][frame];
674     Bitmap *tile_bitmap = gd_get_tile_bitmap(g->bitmap);
675
676     blit_bitmap(tile_bitmap, dest, g->src_x, g->src_y, cell_size, cell_size, sx, sy);
677
678     return;
679   }
680
681   // --------------------------------------------------------------------------------
682   // draw smooth animation for game element moving between two cave tiles
683   // --------------------------------------------------------------------------------
684
685   // ---------- 1st step: draw background element for this tile ----------
686   {
687     int tile_back = (!is_moving_to ? tile : el_diggable(tile_last) ? tile_last : O_SPACE);
688     struct GraphicInfo_BD *g = &graphic_info_bd_object[tile_back][frame];
689     Bitmap *tile_bitmap = gd_get_tile_bitmap(g->bitmap);
690
691     blit_bitmap(tile_bitmap, dest, g->src_x, g->src_y, cell_size, cell_size, sx, sy);
692   }
693
694   // get shifted position between cave fields the game element is moving from/to
695   int itercycle = MIN(MAX(0, game->itermax - game->itercycle - 1), game->itermax);
696   int shift = cell_size * itercycle / game->itermax;
697
698   // ---------- 2nd step: draw element that is moving away from this tile  ----------
699
700   if (is_moving_from)
701   {
702     struct GraphicInfo_BD *g = &graphic_info_bd_object[tile_from][frame];
703     Bitmap *tile_bitmap = gd_get_tile_bitmap(g->bitmap);
704     int dx = (dir_from == GD_MV_LEFT ? -1 : dir_from == GD_MV_RIGHT ? +1 : 0);
705     int dy = (dir_from == GD_MV_UP   ? -1 : dir_from == GD_MV_DOWN  ? +1 : 0);
706     int xsize = (dx != 0 ? shift : cell_size);
707     int ysize = (dy != 0 ? shift : cell_size);
708     int gx = g->src_x + (dx < 0 ? cell_size - shift : 0);
709     int gy = g->src_y + (dy < 0 ? cell_size - shift : 0);
710     int tx = sx + (dx < 0 ? 0 : dx > 0 ? cell_size - shift : 0);
711     int ty = sy + (dy < 0 ? 0 : dy > 0 ? cell_size - shift : 0);
712
713     if (el_walkable(tile))
714       blit_bitmap = BlitBitmapMasked;
715
716     blit_bitmap(tile_bitmap, dest, gx, gy, xsize, ysize, tx, ty);
717
718     // when using dynamic scheduling (mainly BD1 levels), redraw tile in next frame
719     game->gfx_buffer[y][x] |= GD_REDRAW;
720   }
721
722   // ---------- 3rd step: draw element that is moving towards this tile  ----------
723
724   if (is_moving_to)
725   {
726     struct GraphicInfo_BD *g = &graphic_info_bd_object[tile_to][frame];
727     Bitmap *tile_bitmap = gd_get_tile_bitmap(g->bitmap);
728     int dx = (dir_to == GD_MV_LEFT ? +1 : dir_to == GD_MV_RIGHT ? -1 : 0);
729     int dy = (dir_to == GD_MV_UP   ? +1 : dir_to == GD_MV_DOWN  ? -1 : 0);
730     int xsize = (dx != 0 ? cell_size - shift : cell_size);
731     int ysize = (dy != 0 ? cell_size - shift : cell_size);
732     int gx = g->src_x + (dx < 0 ? shift : 0);
733     int gy = g->src_y + (dy < 0 ? shift : 0);
734     int tx = sx + (dx < 0 ? 0 : dx > 0 ? shift : 0);
735     int ty = sy + (dy < 0 ? 0 : dy > 0 ? shift : 0);
736
737     if (is_moving_from)
738       blit_bitmap = BlitBitmapMasked;
739
740     blit_bitmap(tile_bitmap, dest, gx, gy, xsize, ysize, tx, ty);
741
742     // when using dynamic scheduling (mainly BD1 levels), redraw tile in next frame
743     game->gfx_buffer[y][x] |= GD_REDRAW;
744   }
745 }
746
747 int gd_drawcave(Bitmap *dest, GdGame *game, boolean force_redraw)
748 {
749   GdCave *cave = game->cave;
750   static int show_flash_count = 0;
751   boolean show_flash = FALSE;
752   boolean draw_masked = FALSE;
753   boolean redraw_all = force_redraw;
754   int x, y;
755
756   // force redraw if maximum number of cycles has changed (to redraw moving elements)
757   if (game->itermax != game->itermax_last)
758     redraw_all = TRUE;
759
760   if (!cave->gate_open_flash)
761   {
762     show_flash_count = 0;
763   }
764   else
765   {
766     if (show_flash_count++ < 4)
767       show_flash = TRUE;
768
769     redraw_all = TRUE;
770   }
771
772   if (show_flash)
773   {
774     FillRectangle(dest, 0, 0, SXSIZE, SYSIZE, WHITE_PIXEL);
775
776     draw_masked = TRUE;
777     redraw_all = TRUE;
778   }
779
780   // here we draw all cells to be redrawn. we do not take scrolling area into
781   // consideration - sdl will do the clipping.
782   for (y = cave->y1; y <= cave->y2; y++)
783   {
784     for (x = cave->x1; x <= cave->x2; x++)
785     {
786       if (redraw_all ||
787           game->gfx_buffer[y][x] & GD_REDRAW ||
788           game->dir_buffer_from[y][x] != GD_MV_STILL ||
789           game->dir_buffer_to[y][x]   != GD_MV_STILL)
790       {
791         // now we have drawn it
792         game->gfx_buffer[y][x] = game->gfx_buffer[y][x] & ~GD_REDRAW;
793
794         gd_drawcave_tile(dest, game, x, y, draw_masked);
795       }
796     }
797   }
798
799   return 0;
800 }
801
802 static SDL_Surface *get_surface_from_raw_data(const unsigned char *data, int size)
803 {
804   SDL_RWops *rwop = SDL_RWFromConstMem(data, size);
805   SDL_Surface *surface = IMG_Load_RW(rwop, 1);  // 1 = automatically closes rwop
806
807   return surface;
808 }
809
810 static SDL_Surface *get_surface_from_base64(const char *base64_data)
811 {
812   int decoded_data_size = base64_decoded_size(base64_data);
813   unsigned char *decoded_data = checked_malloc(decoded_data_size);
814
815   base64_decode(decoded_data, base64_data);
816
817   SDL_Surface *surface = get_surface_from_raw_data(decoded_data, decoded_data_size);
818
819   checked_free(decoded_data);
820
821   return surface;
822 }
823
824 static SDL_Surface *get_title_screen_background_surface(SDL_Surface *tile)
825 {
826   if (tile == NULL)
827     return NULL;
828
829   SDL_Surface *foreground_surface = gd_title_screen_bitmaps[0]->surface_masked;
830
831   // if foreground image has no transparency, no background image needed
832   if (foreground_surface->format->Amask == 0)
833     return NULL;
834
835   // use foreground image size for background image size
836   int w = foreground_surface->w;
837   int h = foreground_surface->h + tile->h;      // background is one scrolling tile higher
838
839   // create background surface
840   SDL_Surface *back = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0);
841   int x, y;
842
843   // fill background surface with tile
844   for (y = 0; y < h; y += tile->h)
845     for (x = 0; x < w; x += tile->w)
846       SDLBlitSurface(tile, back, 0, 0, tile->w, tile->h, x, y);
847
848   // background tile surface not needed anymore
849   SDL_FreeSurface(tile);
850
851   return back;
852 }
853
854 static SDL_Surface *get_title_screen_surface(int nr)
855 {
856   if (gd_caveset_data == NULL)
857     return NULL;
858
859   // do not use title screen background without foreground image
860   if (nr == 1 && gd_title_screen_bitmaps[0] == NULL)
861     return NULL;
862
863   char *data = (nr == 0 ? gd_caveset_data->title_screen : gd_caveset_data->title_screen_scroll);
864
865   if (data == NULL)
866     return NULL;
867
868   SDL_Surface *surface = get_surface_from_base64(data);
869
870   if (surface == NULL)
871     return NULL;
872
873   return (nr == 0 ? surface : get_title_screen_background_surface(surface));
874 }
875
876 static void set_title_screen_bitmap(int nr)
877 {
878   if (gd_title_screen_bitmaps[nr] != NULL)
879     FreeBitmap(gd_title_screen_bitmaps[nr]);
880
881   gd_title_screen_bitmaps[nr] = NULL;
882
883   SDL_Surface *surface = get_title_screen_surface(nr);
884
885   if (surface == NULL)
886     return;
887
888   int width_scaled  = surface->w * 2;
889   int height_scaled = surface->h * 2;
890   SDL_Surface *surface_scaled = SDLZoomSurface(surface, width_scaled, height_scaled);
891
892   gd_title_screen_bitmaps[nr] = SDLGetBitmapFromSurface(surface_scaled);
893
894   SDL_FreeSurface(surface);
895   SDL_FreeSurface(surface_scaled);
896 }
897
898 static void set_title_screen_bitmaps(void)
899 {
900   int i;
901
902   for (i = 0; i < 2; i++)
903     set_title_screen_bitmap(i);
904 }
905
906 Bitmap **gd_get_title_screen_bitmaps(void)
907 {
908   static char *levelset_subdir_last = NULL;
909
910   if (gd_caveset_data == NULL || gd_caveset_data->title_screen == NULL)
911     return NULL;
912
913   // check if stored cave set is used as current level set (may be outdated)
914   if (!strEqual(gd_caveset_data->levelset_subdir, leveldir_current->subdir))
915     return NULL;
916
917   // check if stored cave set has changed
918   if (!strEqual(gd_caveset_data->levelset_subdir, levelset_subdir_last))
919     set_title_screen_bitmaps();
920
921   setString(&levelset_subdir_last, gd_caveset_data->levelset_subdir);
922
923   return gd_title_screen_bitmaps;
924 }