+
+static SDL_Surface *get_surface_from_raw_data(const unsigned char *data, int size)
+{
+ SDL_RWops *rwop = SDL_RWFromConstMem(data, size);
+ SDL_Surface *surface = IMG_Load_RW(rwop, 1); // 1 = automatically closes rwop
+
+ return surface;
+}
+
+static SDL_Surface *get_surface_from_base64(const char *base64_data)
+{
+ int decoded_data_size = base64_decoded_size(base64_data);
+ unsigned char *decoded_data = checked_malloc(decoded_data_size);
+
+ base64_decode(decoded_data, base64_data);
+
+ SDL_Surface *surface = get_surface_from_raw_data(decoded_data, decoded_data_size);
+
+ checked_free(decoded_data);
+
+ return surface;
+}
+
+static SDL_Surface *get_title_screen_background_surface(SDL_Surface *tile)
+{
+ if (tile == NULL)
+ return NULL;
+
+ SDL_Surface *foreground_surface = gd_title_screen_bitmaps[0]->surface_masked;
+
+ // if foreground image has no transparency, no background image needed
+ if (foreground_surface->format->Amask == 0)
+ return NULL;
+
+ // use foreground image size for background image size
+ int w = foreground_surface->w;
+ int h = foreground_surface->h + tile->h; // background is one scrolling tile higher
+
+ // create background surface
+ SDL_Surface *back = SDL_CreateRGBSurface(0, w, h, 32, 0, 0, 0, 0);
+ int x, y;
+
+ // fill background surface with tile
+ for (y = 0; y < h; y += tile->h)
+ for (x = 0; x < w; x += tile->w)
+ SDLBlitSurface(tile, back, 0, 0, tile->w, tile->h, x, y);
+
+ // background tile surface not needed anymore
+ SDL_FreeSurface(tile);
+
+ return back;
+}
+
+static SDL_Surface *get_title_screen_surface(int nr)
+{
+ if (gd_caveset_data == NULL)
+ return NULL;
+
+ // do not use title screen background without foreground image
+ if (nr == 1 && gd_title_screen_bitmaps[0] == NULL)
+ return NULL;
+
+ char *data = (nr == 0 ? gd_caveset_data->title_screen : gd_caveset_data->title_screen_scroll);
+
+ if (data == NULL)
+ return NULL;
+
+ SDL_Surface *surface = get_surface_from_base64(data);
+
+ if (surface == NULL)
+ return NULL;
+
+ return (nr == 0 ? surface : get_title_screen_background_surface(surface));
+}
+
+static void set_title_screen_bitmap(int nr)
+{
+ if (gd_title_screen_bitmaps[nr] != NULL)
+ FreeBitmap(gd_title_screen_bitmaps[nr]);
+
+ gd_title_screen_bitmaps[nr] = NULL;
+
+ SDL_Surface *surface = get_title_screen_surface(nr);
+
+ if (surface == NULL)
+ return;
+
+ int width_scaled = surface->w * 2;
+ int height_scaled = surface->h * 2;
+ SDL_Surface *surface_scaled = SDLZoomSurface(surface, width_scaled, height_scaled);
+
+ gd_title_screen_bitmaps[nr] = SDLGetBitmapFromSurface(surface_scaled);
+
+ SDL_FreeSurface(surface);
+ SDL_FreeSurface(surface_scaled);
+}
+
+static void set_title_screen_bitmaps(void)
+{
+ int i;
+
+ for (i = 0; i < 2; i++)
+ set_title_screen_bitmap(i);
+}
+
+Bitmap **gd_get_title_screen_bitmaps(void)
+{
+ static char *levelset_subdir_last = NULL;
+
+ if (gd_caveset_data == NULL || gd_caveset_data->title_screen == NULL)
+ return NULL;
+
+ // check if stored cave set is used as current level set (may be outdated)
+ if (!strEqual(gd_caveset_data->levelset_subdir, leveldir_current->subdir))
+ return NULL;
+
+ // check if stored cave set has changed
+ if (!strEqual(gd_caveset_data->levelset_subdir, levelset_subdir_last))
+ set_title_screen_bitmaps();
+
+ setString(&levelset_subdir_last, gd_caveset_data->levelset_subdir);
+
+ return gd_title_screen_bitmaps;
+}