+void SetDrawBackgroundMask(int draw_background_mask)
+{
+ gfx.draw_background_mask = draw_background_mask;
+}
+
+static void DrawBitmapFromTile(Bitmap *bitmap, Bitmap *tile,
+ int dest_x, int dest_y, int width, int height)
+{
+ int bitmap_xsize = width;
+ int bitmap_ysize = height;
+ int tile_xsize = tile->width;
+ int tile_ysize = tile->height;
+ int tile_xsteps = (bitmap_xsize + tile_xsize - 1) / tile_xsize;
+ int tile_ysteps = (bitmap_ysize + tile_ysize - 1) / tile_ysize;
+ int x, y;
+
+ for (y=0; y < tile_ysteps; y++)
+ {
+ for (x=0; x < tile_xsteps; x++)
+ {
+ int draw_x = dest_x + x * tile_xsize;
+ int draw_y = dest_y + y * tile_ysize;
+ int draw_xsize = MIN(tile_xsize, bitmap_xsize - x * tile_xsize);
+ int draw_ysize = MIN(tile_ysize, bitmap_ysize - y * tile_ysize);
+
+ BlitBitmap(tile, bitmap, 0, 0, draw_xsize, draw_ysize, draw_x, draw_y);
+ }
+ }
+}
+
+void SetBackgroundBitmap(Bitmap *background_bitmap_tile, int mask)
+{
+ static Bitmap *main_bitmap_tile = NULL;
+ static Bitmap *door_bitmap_tile = NULL;
+
+ if (mask == REDRAW_FIELD)
+ {
+ if (background_bitmap_tile == main_bitmap_tile)
+ return; /* main background tile has not changed */
+
+ main_bitmap_tile = background_bitmap_tile;
+ }
+ else if (mask == REDRAW_DOOR_1)
+ {
+ if (background_bitmap_tile == door_bitmap_tile)
+ return; /* main background tile has not changed */
+
+ door_bitmap_tile = background_bitmap_tile;
+ }
+ else /* should not happen */
+ return;
+
+ if (background_bitmap_tile)
+ gfx.background_bitmap_mask |= mask;
+ else
+ gfx.background_bitmap_mask &= ~mask;
+
+ if (gfx.background_bitmap == NULL)
+ gfx.background_bitmap = CreateBitmap(video.width, video.height,
+ DEFAULT_DEPTH);
+
+ if (background_bitmap_tile == NULL) /* empty background requested */
+ return;
+
+ if (mask == REDRAW_FIELD)
+ DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
+ gfx.real_sx, gfx.real_sy,
+ gfx.full_sxsize, gfx.full_sysize);
+ else
+ DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
+ gfx.dx, gfx.dy,
+ gfx.dxsize, gfx.dysize);
+}
+
+void SetMainBackgroundBitmap(Bitmap *background_bitmap_tile)
+{
+ SetBackgroundBitmap(background_bitmap_tile, REDRAW_FIELD);
+}
+
+void SetDoorBackgroundBitmap(Bitmap *background_bitmap_tile)
+{
+ SetBackgroundBitmap(background_bitmap_tile, REDRAW_DOOR_1);
+}
+