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