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