added functions to load title screen from native BD caveset
[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 bitmap
48 static Bitmap *gd_title_screen_bitmap = 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 /*
236   SCROLLING
237   
238   scrolls to the player during game play.
239   called by drawcave
240   returns true, if player is not visible-ie it is out of the visible size in the drawing area.
241 */
242 boolean gd_scroll(GdGame *game, boolean exact_scroll, boolean immediate)
243 {
244   static int scroll_desired_x = 0, scroll_desired_y = 0;
245   boolean out_of_window;
246   int player_x, player_y, visible_x, visible_y;
247   boolean changed;
248
249   // max scrolling speed depends on the speed of the cave.
250   // game moves cell_size_game * 1s / cave time pixels in a second.
251   // scrolling moves scroll speed * 1s / scroll_time in a second.
252   // these should be almost equal; scrolling speed a little slower.
253   // that way, the player might reach the border with a small probability,
254   // but the scrolling will not "oscillate", ie. turn on for little intervals as it has
255   // caught up with the desired position. smaller is better.
256   int scroll_speed = cell_size * 20 / game->cave->speed;
257
258   if (!setup.bd_scroll_delay)
259     exact_scroll = TRUE;
260
261   if (immediate)
262     scroll_speed = cell_size * MAX(game->cave->w, game->cave->h);
263
264   player_x = game->cave->player_x - game->cave->x1; // cell coordinates of player
265   player_y = game->cave->player_y - game->cave->y1;
266
267   // when wrapping around to opposite level border, use faster scrolling
268   if (game->cave->player_x == game->cave->x1 ||
269       game->cave->player_x == game->cave->x2 ||
270       game->cave->player_y == game->cave->y1 ||
271       game->cave->player_y == game->cave->y2)
272     scroll_speed *= 4;
273
274   // pixel size of visible part of the cave (may be smaller in intermissions)
275   visible_x = (game->cave->x2 - game->cave->x1 + 1) * cell_size;
276   visible_y = (game->cave->y2 - game->cave->y1 + 1) * cell_size;
277
278   // cell_size contains the scaled size, but we need the original.
279   changed = FALSE;
280
281   if (cave_scroll(visible_x, play_area_w, player_x * cell_size + cell_size / 2 - play_area_w / 2,
282                   exact_scroll, &scroll_x, &scroll_desired_x, scroll_speed))
283     changed = TRUE;
284
285   if (cave_scroll(visible_y, play_area_h, player_y * cell_size + cell_size / 2 - play_area_h / 2,
286                   exact_scroll, &scroll_y, &scroll_desired_y, scroll_speed))
287     changed = TRUE;
288
289   // if scrolling, we should update entire screen.
290   if (changed)
291   {
292     int x, y;
293
294     for (y = 0; y < game->cave->h; y++)
295       for (x = 0; x < game->cave->w; x++)
296         game->gfx_buffer[y][x] |= GD_REDRAW;
297   }
298
299   // check if active player is visible at the moment.
300   out_of_window = FALSE;
301
302   // check if active player is outside drawing area. if yes, we should wait for scrolling
303   if ((player_x * cell_size) < scroll_x ||
304       (player_x * cell_size + cell_size - 1) > scroll_x + play_area_w)
305   {
306     // but only do the wait, if the player SHOULD BE visible, ie. he is inside
307     // the defined visible area of the cave
308     if (game->cave->player_x >= game->cave->x1 &&
309         game->cave->player_x <= game->cave->x2)
310       out_of_window = TRUE;
311   }
312
313   if ((player_y * cell_size) < scroll_y ||
314       (player_y * cell_size + cell_size - 1) > scroll_y + play_area_h)
315     // but only do the wait, if the player SHOULD BE visible, ie. he is inside
316     // the defined visible area of the cave
317     if (game->cave->player_y >= game->cave->y1 &&
318         game->cave->player_y <= game->cave->y2)
319       out_of_window = TRUE;
320
321   // if not yet born, we treat as visible. so cave will run.
322   // the user is unable to control an unborn player, so this is the right behaviour.
323   if (game->cave->player_state == GD_PL_NOT_YET)
324     return FALSE;
325
326   return out_of_window;
327 }
328
329 // returns true, if the given surface seems to be a c64 imported image.
330 static boolean surface_has_c64_colors(SDL_Surface *surface)
331 {
332   boolean has_c64_colors = TRUE;        // default: assume C64 colors
333   const unsigned char *p;
334   int x, y;
335
336   if (surface->format->BytesPerPixel != 4)
337     return FALSE;
338
339   SDL_LockSurface(surface);
340
341   for (y = 0; y < surface->h && has_c64_colors; y++)
342   {
343     p = ((unsigned char *)surface->pixels) + y * surface->pitch;
344
345     for (x = 0; x < surface->w * surface->format->BytesPerPixel && has_c64_colors; x++)
346       if (p[x] != 0 && p[x] != 255)
347         has_c64_colors = FALSE;
348   }
349
350   SDL_UnlockSurface(surface);
351
352   return has_c64_colors;
353 }
354
355 // sets one of the colors in the indexed palette of an sdl surface to a GdColor.
356 static void set_surface_palette_color(SDL_Surface *surface, int index, GdColor col)
357 {
358   if (surface->format->BytesPerPixel != 1)
359     return;
360
361   SDL_Color c;
362
363   c.r = gd_color_get_r(col);
364   c.g = gd_color_get_g(col);
365   c.b = gd_color_get_b(col);
366
367   SDL_SetPaletteColors(surface->format->palette, &c, index, 1);
368 }
369
370 // takes a c64_gfx.png-coded 32-bit surface, and creates a paletted surface in our internal format.
371 static SDL_Surface *get_tile_surface_c64(SDL_Surface *surface, int scale_down_factor)
372 {
373   static SDL_Surface *tile_surface_c64 = NULL;
374   static unsigned char *pixels = NULL;
375   int width  = surface->w;
376   int height = surface->h;
377   int out = 0;
378   int x, y;
379
380   if (!surface_has_c64_colors(surface))
381     return NULL;
382
383   if (surface->format->BytesPerPixel != 4)
384     Fail("C64 style surface has wrong color depth -- should not happen");
385
386   if (tile_surface_c64 != NULL)
387     SDL_FreeSurface(tile_surface_c64);
388
389   checked_free(pixels);
390
391   pixels = checked_malloc(width * height);
392
393   SDL_LockSurface(surface);
394
395   for (y = 0; y < height; y++)
396   {
397     unsigned int *p = (unsigned int *)((char *)surface->pixels + y * surface->pitch);
398
399     for (x = 0; x < width; x++)
400     {
401       int r = (p[x] & surface->format->Rmask) >> surface->format->Rshift << surface->format->Rloss;
402       int g = (p[x] & surface->format->Gmask) >> surface->format->Gshift << surface->format->Gloss;
403       int b = (p[x] & surface->format->Bmask) >> surface->format->Bshift << surface->format->Bloss;
404       // should be:
405       // a = (p[x]&surface->format->Amask) >> surface->format->Ashift << surface->format->Aloss;
406       // but we do not use the alpha channel in sdash, so we just use 255 (max alpha)
407
408       pixels[out++] = c64_png_colors(r, g, b, 255);
409     }
410   }
411
412   SDL_UnlockSurface(surface);
413
414   // create new surface from pixel data
415   tile_surface_c64 =
416     SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 8, width, 0, 0, 0, 0);
417
418   if (tile_surface_c64 == NULL)
419     Fail("SDL_CreateRGBSurfaceFrom() failed: %s", SDL_GetError());
420
421   if (scale_down_factor > 1)
422   {
423     SDL_Surface *surface_old = tile_surface_c64;
424     int width_scaled  = width  / scale_down_factor;
425     int height_scaled = height / scale_down_factor;
426
427     // replace surface with scaled-down variant
428     tile_surface_c64 = SDLZoomSurface(surface_old, width_scaled, height_scaled);
429
430     // free previous (non-scaled) surface
431     SDL_FreeSurface(surface_old);
432   }
433
434   return tile_surface_c64;
435 }
436
437 static Bitmap *get_tile_bitmap_c64(GdCave *cave, SDL_Surface *surface)
438 {
439   static Bitmap *tile_bitmap_c64 = NULL;
440
441   if (surface == NULL)
442     return NULL;
443
444   if (tile_bitmap_c64 != NULL)
445     FreeBitmap(tile_bitmap_c64);
446
447   // set surface color palette to cave colors
448   set_surface_palette_color(surface, 0, 0);
449   set_surface_palette_color(surface, 1, gd_color_get_rgb(cave->color0));
450   set_surface_palette_color(surface, 2, gd_color_get_rgb(cave->color1));
451   set_surface_palette_color(surface, 3, gd_color_get_rgb(cave->color2));
452   set_surface_palette_color(surface, 4, gd_color_get_rgb(cave->color3));
453   set_surface_palette_color(surface, 5, gd_color_get_rgb(cave->color4));
454   set_surface_palette_color(surface, 6, gd_color_get_rgb(cave->color5));
455   set_surface_palette_color(surface, 7, 0);
456   set_surface_palette_color(surface, 8, 0);
457
458   // create bitmap from C64 surface
459   tile_bitmap_c64 = SDLGetBitmapFromSurface(surface);
460
461   return tile_bitmap_c64;
462 }
463
464 void gd_prepare_tile_bitmap(GdCave *cave, Bitmap *bitmap, int scale_down_factor)
465 {
466   static SDL_Surface *tile_surface_c64 = NULL;
467   static Bitmap *gd_tile_bitmap_original = NULL;
468   static int scale_down_factor_last = -1;
469
470   if (program.headless)
471     return;
472
473   // check if tile bitmap has changed (different artwork or tile size selected)
474   if (bitmap != gd_tile_bitmap_original || scale_down_factor != scale_down_factor_last)
475   {
476     // check if tile bitmap has limited C64 style colors
477     tile_surface_c64 = get_tile_surface_c64(bitmap->surface, scale_down_factor);
478
479     // store original tile bitmap from current artwork set and scaling factor
480     gd_tile_bitmap_original = bitmap;
481     scale_down_factor_last = scale_down_factor;
482
483     // store reference tile bitmap from current artwork set (may be changed later)
484     gd_tile_bitmap_reference = bitmap;
485   }
486
487   // check if tile bitmap should be colored for next game
488   if (tile_surface_c64 != NULL)
489   {
490     // set tile bitmap to bitmap with level-specific colors
491     gd_tile_bitmap = get_tile_bitmap_c64(cave, tile_surface_c64);
492   }
493   else
494   {
495     // no special tile bitmap available for this artwork set
496     gd_tile_bitmap = NULL;
497   }
498 }
499
500 void gd_set_tile_bitmap_reference(Bitmap *bitmap)
501 {
502   gd_tile_bitmap_reference = bitmap;
503 }
504
505 Bitmap *gd_get_tile_bitmap(Bitmap *bitmap)
506 {
507   // if no special tile bitmap available, keep using default tile bitmap
508   if (gd_tile_bitmap == NULL)
509     return bitmap;
510
511   // if default bitmap refers to tile bitmap, use special tile bitmap
512   if (bitmap == gd_tile_bitmap_reference)
513     return gd_tile_bitmap;
514
515   return bitmap;
516 }
517
518 #if DO_GFX_SANITY_CHECK
519 // workaround to prevent variable name scope problem
520 static boolean use_native_bd_graphics_engine(void)
521 {
522   return game.use_native_bd_graphics_engine;
523 }
524 #endif
525
526 // returns true if the element is a player
527 static inline boolean is_player(const int element)
528 {
529   return (gd_elements[element & O_MASK].properties & P_PLAYER) != 0;
530 }
531
532 // returns true if the element is diggable
533 static inline boolean is_diggable(const int element)
534 {
535   return (gd_elements[element & O_MASK].properties & P_DIGGABLE) != 0;
536 }
537
538 // returns true if the element is collectible
539 static inline boolean is_collectible(const int element)
540 {
541   return (gd_elements[element & O_MASK].properties & P_COLLECTIBLE) != 0;
542 }
543
544 // returns true if the element is pushable
545 static inline boolean is_pushable(const int element)
546 {
547   return (gd_elements[element & O_MASK].properties & P_PUSHABLE) != 0;
548 }
549
550 // returns true if the element can move
551 static inline boolean can_move(const int element)
552 {
553   return (gd_elements[element & O_MASK].properties & P_CAN_MOVE) != 0;
554 }
555
556 // returns true if the element can fall
557 static inline boolean can_fall(const int element)
558 {
559   return (gd_elements[element & O_MASK].properties & P_CAN_FALL) != 0;
560 }
561
562 // returns true if the element is exploding
563 static inline boolean is_explosion(const int element)
564 {
565   return (gd_elements[element & O_MASK].properties & P_EXPLOSION) != 0;
566 }
567
568 static void gd_drawcave_tile(Bitmap *dest, GdGame *game, int x, int y, boolean draw_masked)
569 {
570   void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
571     (draw_masked ? BlitBitmapMasked : BlitBitmap);
572   GdCave *cave = game->cave;
573   int sx = x * cell_size - scroll_x;
574   int sy = y * cell_size - scroll_y;
575   int dir = game->dir_buffer[y][x];
576   int tile = game->element_buffer[y][x];
577   int frame = game->animcycle;
578   struct GraphicInfo_BD *g = &graphic_info_bd_object[tile][frame];
579   Bitmap *tile_bitmap = gd_get_tile_bitmap(g->bitmap);
580   boolean is_movable = (can_move(tile) || can_fall(tile) || is_pushable(tile) || is_player(tile));
581   boolean is_movable_or_diggable = (is_movable || is_diggable(game->last_element_buffer[y][x]));
582   boolean is_moving = (is_movable_or_diggable && dir != GD_MV_STILL);
583   boolean use_smooth_movements =
584     ((setup.bd_smooth_movements == TRUE) ||
585      (setup.bd_smooth_movements == AUTO && !use_native_bd_graphics_engine()));
586
587   // do not use smooth movement animation for exploding game elements (like player)
588   if (is_explosion(tile) && dir != GD_MV_STILL)
589     use_smooth_movements = FALSE;
590
591   // do not use smooth movement animation for player entering exit (engine stopped)
592   if (cave->player_state == GD_PL_EXITED)
593     use_smooth_movements = FALSE;
594
595 #if DO_GFX_SANITY_CHECK
596   if (use_native_bd_graphics_engine() && !setup.small_game_graphics && !program.headless)
597   {
598     int old_x = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) % GD_NUM_OF_CELLS_X;
599     int old_y = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) / GD_NUM_OF_CELLS_X;
600     int new_x = g->src_x / g->width;
601     int new_y = g->src_y / g->height;
602
603     if (new_x != old_x || new_y != old_y)
604     {
605       printf("::: BAD ANIMATION FOR TILE %d, FRAME %d [NEW(%d, %d) != OLD(%d, %d)] ['%s']\n",
606              tile, frame,
607              new_x, new_y,
608              old_x, old_y,
609              gd_elements[tile].name);
610     }
611   }
612 #endif
613
614   // if game element not moving (or no smooth movements requested), simply draw tile
615   if (!is_moving || !use_smooth_movements)
616   {
617     blit_bitmap(tile_bitmap, dest, g->src_x, g->src_y, cell_size, cell_size, sx, sy);
618
619     return;
620   }
621
622   // draw smooth animation for game element moving between two cave tiles
623
624   if (!(game->last_element_buffer[y][x] & SKIPPED))
625   {
626     // redraw previous game element on the cave field the new element is moving to
627     int tile_last = game->last_element_buffer[y][x];
628
629     // only redraw previous game element if it is diggable (like dirt etc.)
630     if (!is_diggable(tile_last))
631       tile_last = O_SPACE;
632
633     struct GraphicInfo_BD *g_old = &graphic_info_bd_object[tile_last][frame];
634     Bitmap *tile_bitmap_old = gd_get_tile_bitmap(g_old->bitmap);
635
636     blit_bitmap(tile_bitmap_old, dest, g_old->src_x, g_old->src_y, cell_size, cell_size, sx, sy);
637   }
638
639   // get cave field position the game element is moving from
640   int dx = (dir == GD_MV_LEFT ? +1 : dir == GD_MV_RIGHT ? -1 : 0);
641   int dy = (dir == GD_MV_UP   ? +1 : dir == GD_MV_DOWN  ? -1 : 0);
642   int old_x = cave->getx(cave, x + dx, y + dy);
643   int old_y = cave->gety(cave, x + dx, y + dy);
644   int tile_from = game->element_buffer[old_y][old_x] & ~SKIPPED;   // should never be skipped
645   struct GraphicInfo_BD *g_from = &graphic_info_bd_object[tile_from][frame];
646   Bitmap *tile_bitmap_from = gd_get_tile_bitmap(g_from->bitmap);
647   boolean old_is_player = is_player(tile_from);
648   boolean old_is_moving = (game->dir_buffer[old_y][old_x] != GD_MV_STILL);
649   boolean old_is_visible = (old_x >= cave->x1 &&
650                             old_x <= cave->x2 &&
651                             old_y >= cave->y1 &&
652                             old_y <= cave->y2);
653   if (old_is_visible)
654   {
655     if (!old_is_moving && !old_is_player)
656     {
657       // redraw game element on the cave field the element is moving from
658       blit_bitmap(tile_bitmap_from, dest, g_from->src_x, g_from->src_y, cell_size, cell_size,
659                   sx + dx * cell_size, sy + dy * cell_size);
660
661       game->element_buffer[old_y][old_x] |= SKIPPED;
662     }
663     else
664     {
665       // if old tile also moving (like pushing player), do not redraw tile background
666       // (but redraw if tile and old tile are moving/falling into different directions)
667       if (game->dir_buffer[old_y][old_x] == game->dir_buffer[y][x])
668         game->last_element_buffer[old_y][old_x] |= SKIPPED;
669     }
670   }
671
672   // get shifted position between cave fields the game element is moving from/to
673   int itercycle = MIN(MAX(0, game->itermax - game->itercycle - 1), game->itermax);
674   int shift = cell_size * itercycle / game->itermax;
675
676   blit_bitmap(tile_bitmap, dest, g->src_x, g->src_y, cell_size, cell_size,
677               sx + dx * shift, sy + dy * shift);
678
679   // special case: redraw player snapping a game element
680   if (old_is_visible && old_is_player && !old_is_moving)
681   {
682     // redraw game element on the cave field the element is moving from
683     blit_bitmap(tile_bitmap_from, dest, g_from->src_x, g_from->src_y, cell_size, cell_size,
684                 sx + dx * cell_size, sy + dy * cell_size);
685   }
686 }
687
688 int gd_drawcave(Bitmap *dest, GdGame *game, boolean force_redraw)
689 {
690   GdCave *cave = game->cave;
691   static int show_flash_count = 0;
692   boolean show_flash = FALSE;
693   boolean draw_masked = FALSE;
694   boolean redraw_all = force_redraw;
695   int x, y;
696
697   // force redraw if maximum number of cycles has changed (to redraw moving elements)
698   if (game->itermax != game->itermax_last)
699     redraw_all = TRUE;
700
701   if (!cave->gate_open_flash)
702   {
703     show_flash_count = 0;
704   }
705   else
706   {
707     if (show_flash_count++ < 4)
708       show_flash = TRUE;
709
710     redraw_all = TRUE;
711   }
712
713   if (show_flash)
714   {
715     FillRectangle(dest, 0, 0, SXSIZE, SYSIZE, WHITE_PIXEL);
716
717     draw_masked = TRUE;
718     redraw_all = TRUE;
719   }
720
721   // here we draw all cells to be redrawn. we do not take scrolling area into
722   // consideration - sdl will do the clipping.
723   for (y = cave->y1; y <= cave->y2; y++)
724   {
725     for (x = cave->x1; x <= cave->x2; x++)
726     {
727       if (redraw_all ||
728           game->gfx_buffer[y][x] & GD_REDRAW ||
729           game->dir_buffer[y][x] != GD_MV_STILL)
730       {
731         // skip redrawing already drawn element with movement
732         if (game->element_buffer[y][x] & SKIPPED)
733           continue;
734
735         // now we have drawn it
736         game->gfx_buffer[y][x] = game->gfx_buffer[y][x] & ~GD_REDRAW;
737
738         gd_drawcave_tile(dest, game, x, y, draw_masked);
739       }
740     }
741   }
742
743   return 0;
744 }
745
746 static SDL_Surface *get_surface_from_raw_data(const unsigned char *data, int size)
747 {
748   SDL_RWops *rwop = SDL_RWFromConstMem(data, size);
749   SDL_Surface *surface = IMG_Load_RW(rwop, 1);  // 1 = automatically closes rwop
750
751   return surface;
752 }
753
754 static SDL_Surface *get_surface_from_base64(const char *base64_data)
755 {
756   int decoded_data_size = base64_decoded_size(base64_data);
757   unsigned char *decoded_data = checked_malloc(decoded_data_size);
758
759   base64_decode(decoded_data, base64_data);
760
761   SDL_Surface *surface = get_surface_from_raw_data(decoded_data, decoded_data_size);
762
763   checked_free(decoded_data);
764
765   return surface;
766 }
767
768 static SDL_Surface *get_title_screen_surface(void)
769 {
770   if (gd_caveset_data == NULL || gd_caveset_data->title_screen == NULL)
771     return NULL;
772
773   SDL_Surface *surface = get_surface_from_base64(gd_caveset_data->title_screen);
774
775   if (surface == NULL)
776     return NULL;
777
778   // create target surface
779   SDL_Surface *target = SDL_CreateRGBSurface(0, surface->w, surface->h, 32, 0, 0, 0, 0);
780
781   // check for transparency and background tile
782   if (surface->format->Amask != 0 && gd_caveset_data->title_screen_scroll != NULL)
783   {
784     SDL_Surface *tile = get_surface_from_base64(gd_caveset_data->title_screen_scroll);
785
786     if (tile != NULL)
787     {
788       int x, y;
789
790       // create background surface
791       SDL_Surface *back = SDL_CreateRGBSurface(0, surface->w, surface->h, 32, 0, 0, 0, 0);
792
793       // fill background surface with tile
794       for (y = 0; y < surface->h; y += tile->h)
795         for (x = 0; x < surface->w; x += tile->w)
796           SDLBlitSurface(tile, back, 0, 0, tile->w, tile->h, x, y);
797
798       // copy masked screen over background surface
799       SDLBlitSurface(back, target, 0, 0, surface->w, surface->h, 0, 0);
800
801       // free temporary surfaces
802       SDL_FreeSurface(tile);
803       SDL_FreeSurface(back);
804     }
805   }
806
807   SDLBlitSurface(surface, target, 0, 0, surface->w, surface->h, 0, 0);
808
809   SDL_FreeSurface(surface);
810
811   return target;
812 }
813
814 static void set_title_screen_bitmap(void)
815 {
816   if (gd_title_screen_bitmap != NULL)
817     FreeBitmap(gd_title_screen_bitmap);
818
819   gd_title_screen_bitmap = NULL;
820
821   SDL_Surface *surface = get_title_screen_surface();
822
823   if (surface == NULL)
824     return;
825
826   int width_scaled  = surface->w * 2;
827   int height_scaled = surface->h * 2;
828   SDL_Surface *surface_scaled = SDLZoomSurface(surface, width_scaled, height_scaled);
829
830   gd_title_screen_bitmap = SDLGetBitmapFromSurface(surface_scaled);
831
832   SDL_FreeSurface(surface);
833   SDL_FreeSurface(surface_scaled);
834 }
835
836 Bitmap *gd_get_title_screen_bitmap(void)
837 {
838   static char *levelset_subdir_last = NULL;
839
840   if (gd_caveset_data == NULL || gd_caveset_data->title_screen == NULL)
841     return NULL;
842
843   // check if stored cave set is used as current level set (may be outdated)
844   if (!strEqual(gd_caveset_data->levelset_subdir, leveldir_current->subdir))
845     return NULL;
846
847   // check if stored cave set has changed
848   if (!strEqual(gd_caveset_data->levelset_subdir, levelset_subdir_last))
849     set_title_screen_bitmap();
850
851   setString(&levelset_subdir_last, gd_caveset_data->levelset_subdir);
852
853   return gd_title_screen_bitmap;
854 }