+
+static Bitmap *get_tile_bitmap_c64(GdCave *cave, SDL_Surface *surface)
+{
+ static Bitmap *tile_bitmap_c64 = NULL;
+
+ if (surface == NULL)
+ return NULL;
+
+ if (tile_bitmap_c64 != NULL)
+ FreeBitmap(tile_bitmap_c64);
+
+ // set surface color palette to cave colors
+ set_surface_palette_color(surface, 0, 0);
+ set_surface_palette_color(surface, 1, gd_color_get_rgb(cave->color0));
+ set_surface_palette_color(surface, 2, gd_color_get_rgb(cave->color1));
+ set_surface_palette_color(surface, 3, gd_color_get_rgb(cave->color2));
+ set_surface_palette_color(surface, 4, gd_color_get_rgb(cave->color3));
+ set_surface_palette_color(surface, 5, gd_color_get_rgb(cave->color4));
+ set_surface_palette_color(surface, 6, gd_color_get_rgb(cave->color5));
+ set_surface_palette_color(surface, 7, 0);
+ set_surface_palette_color(surface, 8, 0);
+
+ // create bitmap from C64 surface
+ tile_bitmap_c64 = SDLGetBitmapFromSurface(surface);
+
+ return tile_bitmap_c64;
+}
+
+void gd_prepare_tile_bitmap(GdCave *cave, Bitmap *bitmap, int scale_down_factor)
+{
+ static SDL_Surface *tile_surface_c64 = NULL;
+ static Bitmap *gd_tile_bitmap_original = NULL;
+ static int scale_down_factor_last = -1;
+
+ if (program.headless)
+ return;
+
+ // check if tile bitmap has changed (different artwork or tile size selected)
+ if (bitmap != gd_tile_bitmap_original || scale_down_factor != scale_down_factor_last)
+ {
+ // check if tile bitmap has limited C64 style colors
+ tile_surface_c64 = get_tile_surface_c64(bitmap->surface, scale_down_factor);
+
+ // store original tile bitmap from current artwork set and scaling factor
+ gd_tile_bitmap_original = bitmap;
+ scale_down_factor_last = scale_down_factor;
+
+ // store reference tile bitmap from current artwork set (may be changed later)
+ gd_tile_bitmap_reference = bitmap;
+ }
+
+ // check if tile bitmap should be colored for next game
+ if (tile_surface_c64 != NULL)
+ {
+ // set tile bitmap to bitmap with level-specific colors
+ gd_tile_bitmap = get_tile_bitmap_c64(cave, tile_surface_c64);
+ }
+ else
+ {
+ // no special tile bitmap available for this artwork set
+ gd_tile_bitmap = NULL;
+ }
+}
+
+void gd_set_tile_bitmap_reference(Bitmap *bitmap)
+{
+ gd_tile_bitmap_reference = bitmap;
+}
+
+Bitmap *gd_get_tile_bitmap(Bitmap *bitmap)
+{
+ // if no special tile bitmap available, keep using default tile bitmap
+ if (gd_tile_bitmap == NULL)
+ return bitmap;
+
+ // if default bitmap refers to tile bitmap, use special tile bitmap
+ if (bitmap == gd_tile_bitmap_reference)
+ return gd_tile_bitmap;
+
+ return bitmap;
+}
+
+// returns true if the element is a player
+static inline boolean el_player(const int element)
+{
+ return (gd_elements[element & O_MASK].properties & P_PLAYER) != 0;
+}
+
+// returns true if the element is diggable
+static inline boolean el_diggable(const int element)
+{
+ return (gd_elements[element & O_MASK].properties & P_DIGGABLE) != 0;
+}
+
+// returns true if the element is collectible
+static inline boolean el_collectible(const int element)
+{
+ return (gd_elements[element & O_MASK].properties & P_COLLECTIBLE) != 0;
+}
+
+// returns true if the element is pushable
+static inline boolean el_pushable(const int element)
+{
+ return (gd_elements[element & O_MASK].properties & P_PUSHABLE) != 0;
+}
+
+// returns true if the element can move
+static inline boolean el_can_move(const int element)
+{
+ return (gd_elements[element & O_MASK].properties & P_CAN_MOVE) != 0;
+}
+
+// returns true if the element can fall
+static inline boolean el_falling(const int element)
+{
+ return (gd_elements[element & O_MASK].properties & P_FALLING) != 0;
+}
+
+// returns true if the element is exploding
+static inline boolean el_explosion(const int element)
+{
+ return (gd_elements[element & O_MASK].properties & P_EXPLOSION) != 0;
+}
+
+static void gd_drawcave_tile(Bitmap *dest, GdGame *game, int x, int y, boolean draw_masked)
+{
+ void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
+ (draw_masked ? BlitBitmapMasked : BlitBitmap);
+ GdCave *cave = game->cave;
+ int sx = x * cell_size - scroll_x;
+ int sy = y * cell_size - scroll_y;
+ int dir = game->dir_buffer[y][x];
+ int tile = game->element_buffer[y][x];
+ int frame = game->animcycle;
+ struct GraphicInfo_BD *g = &graphic_info_bd_object[tile][frame];
+ Bitmap *tile_bitmap = gd_get_tile_bitmap(g->bitmap);
+ boolean is_movable = (el_can_move(tile) || el_falling(tile) || el_pushable(tile) ||
+ el_player(tile));
+ boolean is_movable_or_diggable = (is_movable || el_diggable(game->last_element_buffer[y][x]));
+ boolean is_moving = (is_movable_or_diggable && dir != GD_MV_STILL);
+ boolean use_smooth_movements = use_bd_smooth_movements();
+
+ // do not use smooth movement animation for exploding game elements (like player)
+ if (el_explosion(tile) && dir != GD_MV_STILL)
+ use_smooth_movements = FALSE;
+
+ // do not use smooth movement animation for player entering exit (engine stopped)
+ if (cave->player_state == GD_PL_EXITED)
+ use_smooth_movements = FALSE;
+
+#if DO_GFX_SANITY_CHECK
+ if (use_native_bd_graphics_engine() && !setup.small_game_graphics && !program.headless)
+ {
+ int old_x = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) % GD_NUM_OF_CELLS_X;
+ int old_y = (game->gfx_buffer[y][x] % GD_NUM_OF_CELLS) / GD_NUM_OF_CELLS_X;
+ int new_x = g->src_x / g->width;
+ int new_y = g->src_y / g->height;
+
+ if (new_x != old_x || new_y != old_y)
+ {
+ printf("::: BAD ANIMATION FOR TILE %d, FRAME %d [NEW(%d, %d) != OLD(%d, %d)] ['%s']\n",
+ tile, frame,
+ new_x, new_y,
+ old_x, old_y,
+ gd_elements[tile].name);
+ }
+ }