+// takes a c64_gfx.png-coded 32-bit surface, and creates a paletted surface in our internal format.
+static SDL_Surface *get_tile_surface_c64(SDL_Surface *surface, int scale_down_factor)
+{
+ static SDL_Surface *tile_surface_c64 = NULL;
+ static unsigned char *pixels = NULL;
+ int width = surface->w;
+ int height = surface->h;
+ int out = 0;
+ int x, y;
+
+ if (!surface_has_c64_colors(surface))
+ return NULL;
+
+ if (surface->format->BytesPerPixel != 4)
+ Fail("C64 style surface has wrong color depth -- should not happen");
+
+ if (tile_surface_c64 != NULL)
+ SDL_FreeSurface(tile_surface_c64);
+
+ checked_free(pixels);
+
+ pixels = checked_malloc(width * height);
+
+ SDL_LockSurface(surface);
+
+ for (y = 0; y < height; y++)
+ {
+ unsigned int *p = (unsigned int *)((char *)surface->pixels + y * surface->pitch);
+
+ for (x = 0; x < width; x++)
+ {
+ int r = (p[x] & surface->format->Rmask) >> surface->format->Rshift << surface->format->Rloss;
+ int g = (p[x] & surface->format->Gmask) >> surface->format->Gshift << surface->format->Gloss;
+ int b = (p[x] & surface->format->Bmask) >> surface->format->Bshift << surface->format->Bloss;
+ // should be:
+ // a = (p[x]&surface->format->Amask) >> surface->format->Ashift << surface->format->Aloss;
+ // but we do not use the alpha channel in sdash, so we just use 255 (max alpha)
+
+ pixels[out++] = c64_png_colors(r, g, b, 255);
+ }
+ }
+
+ SDL_UnlockSurface(surface);
+
+ // create new surface from pixel data
+ tile_surface_c64 =
+ SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 8, width, 0, 0, 0, 0);
+
+ if (tile_surface_c64 == NULL)
+ Fail("SDL_CreateRGBSurfaceFrom() failed: %s", SDL_GetError());
+
+ if (scale_down_factor > 1)
+ {
+ SDL_Surface *surface_old = tile_surface_c64;
+ int width_scaled = width / scale_down_factor;
+ int height_scaled = height / scale_down_factor;
+
+ // replace surface with scaled-down variant
+ tile_surface_c64 = SDLZoomSurface(surface_old, width_scaled, height_scaled);
+
+ // free previous (non-scaled) surface
+ SDL_FreeSurface(surface_old);
+ }
+
+ return tile_surface_c64;
+}
+
+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);
+
+ // set background color to be transparent for masked tile bitmap
+ int bg_color = gd_color_get_rgb(cave->color0);
+ int bg_r = gd_color_get_r(bg_color);
+ int bg_g = gd_color_get_g(bg_color);
+ int bg_b = gd_color_get_b(bg_color);
+
+ // create bitmap from C64 surface
+ tile_bitmap_c64 = SDLGetBitmapFromSurface_WithMaskedColor(surface, bg_r, bg_g, bg_b);
+
+ 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)