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