rnd-20030107-1-src
[rocksndiamonds.git] / src / tools.c
index 493213367b73499b7da2075f98603ee8dde095e4..16cb3984075fd88b48bec40c43a3f39ead5e8d48 100644 (file)
@@ -32,7 +32,6 @@
 #define NUM_TOOL_BUTTONS       7
 
 /* forward declaration for internal use */
-static int getGraphicAnimationPhase(int, int, int);
 static void UnmapToolButtons();
 static void HandleToolButtons(struct GadgetInfo *);
 
@@ -280,7 +279,7 @@ void BackToFront()
       info1[0] = '\0';
 
     sprintf(text, "%.1f fps%s", global.frames_per_second, info1);
-    DrawTextExt(window, SX, SY, text, FS_SMALL, FC_YELLOW);
+    DrawTextExt(window, SX, SY, text, FS_SMALL, FC_YELLOW, FONT_OPAQUE);
   }
 
   FlushDisplay();
@@ -363,9 +362,32 @@ void FadeToFront()
   BackToFront();
 }
 
+void SetMainBackgroundImage(int graphic)
+{
+  SetMainBackgroundBitmap(graphic == IMG_NONE ? NULL :
+                         new_graphic_info[graphic].bitmap ?
+                         new_graphic_info[graphic].bitmap :
+                         new_graphic_info[IMG_BACKGROUND_DEFAULT].bitmap);
+}
+
+void SetDoorBackgroundImage(int graphic)
+{
+  SetDoorBackgroundBitmap(graphic == IMG_NONE ? NULL :
+                         new_graphic_info[graphic].bitmap ?
+                         new_graphic_info[graphic].bitmap :
+                         new_graphic_info[IMG_BACKGROUND_DEFAULT].bitmap);
+}
+
+void DrawBackground(int dest_x, int dest_y, int width, int height)
+{
+  ClearRectangleOnBackground(backbuffer, dest_x, dest_y, width, height);
+
+  redraw_mask |= REDRAW_FIELD;
+}
+
 void ClearWindow()
 {
-  ClearRectangle(backbuffer, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
+  DrawBackground(REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
 
   if (setup.soft_scrolling && game_status == PLAYING)
   {
@@ -380,8 +402,39 @@ void ClearWindow()
     ClearRectangle(window, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
     SetDrawtoField(DRAW_DIRECT);
   }
+}
 
-  redraw_mask |= REDRAW_FIELD;
+static int getGraphicAnimationPhase(int frames, int delay, int mode)
+{
+  int phase;
+
+  if (mode & ANIM_PINGPONG)
+  {
+    int max_anim_frames = 2 * frames - 2;
+
+    phase = (FrameCounter % (delay * max_anim_frames)) / delay;
+    phase = (phase < frames ? phase : max_anim_frames - phase);
+  }
+  else
+    phase = (FrameCounter % (delay * frames)) / delay;
+
+  if (mode & ANIM_REVERSE)
+    phase = -phase;
+
+  return phase;
+}
+
+inline int getGraphicAnimationFrame(int graphic, int sync_frame)
+{
+  /* animation synchronized with global frame counter, not move position */
+  if (new_graphic_info[graphic].anim_global_sync || sync_frame < 0)
+    sync_frame = FrameCounter;
+
+  return getAnimationFrame(new_graphic_info[graphic].anim_frames,
+                          new_graphic_info[graphic].anim_delay,
+                          new_graphic_info[graphic].anim_mode,
+                          new_graphic_info[graphic].anim_start_frame,
+                          sync_frame);
 }
 
 void MarkTileDirty(int x, int y)
@@ -625,9 +678,10 @@ void DrawPlayer(struct PlayerInfo *player)
   {
     int px = SCREENX(next_jx), py = SCREENY(next_jy);
 
-    if (element == EL_SOKOBAN_FIELD_EMPTY ||
-       Feld[next_jx][next_jy] == EL_SOKOBAN_FIELD_FULL)
-      DrawGraphicShiftedThruMask(px, py, sxx, syy, GFX_SOKOBAN_OBJEKT, 0,
+    if ((sxx || syy) &&
+       (element == EL_SOKOBAN_FIELD_EMPTY ||
+        Feld[next_jx][next_jy] == EL_SOKOBAN_FIELD_FULL))
+      DrawGraphicShiftedThruMask(px, py, sxx, syy, IMG_SOKOBAN_OBJECT, 0,
                                 NO_CUTTING);
     else
     {
@@ -637,9 +691,7 @@ void DrawPlayer(struct PlayerInfo *player)
       int frame = 0;
 #endif
 
-      if ((element == EL_ROCK ||
-          element == EL_BD_ROCK ||
-          element == EL_SP_ZONK) && sxx)
+      if ((sxx || syy) && IS_PUSHABLE(element))
       {
        graphic = el_dir_act2img(element, player->MovDir, GFX_ACTION_MOVING);
 #if 1
@@ -693,24 +745,22 @@ void DrawPlayer(struct PlayerInfo *player)
 #endif
 
     if (game.emulation == EMU_SUPAPLEX)
-      DrawGraphic(sx, sy, GFX_SP_DISK_RED, 0);
+      DrawGraphic(sx, sy, IMG_SP_DISK_RED, 0);
     else
       DrawGraphicThruMask(sx, sy, graphic, frame);
   }
 
   if (player_is_moving && last_element == EL_EXPLOSION)
   {
-    int graphic = IMG_EXPLOSION;
-    int phase = Frame[last_jx][last_jy] - 1;
+    int stored = Store[last_jx][last_jy];
+    int graphic = (game.emulation != EMU_SUPAPLEX ? IMG_EXPLOSION :
+                  stored == EL_SP_INFOTRON ? IMG_SP_EXPLOSION_INFOTRON :
+                  IMG_SP_EXPLOSION);
     int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
-    int frame = (phase / delay - 1);
-
-    if (game.emulation == EMU_SUPAPLEX)
-      graphic = (Store[last_jx][last_jy] == EL_SP_INFOTRON ?
-                IMG_SP_EXPLOSION_INFOTRON :
-                IMG_SP_EXPLOSION);
+    int phase = Frame[last_jx][last_jy] - 1;
+    int frame = getGraphicAnimationFrame(graphic, phase - delay);
 
-    if (frame >= 0)
+    if (phase >= delay)
       DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), graphic, frame);
   }
 
@@ -738,104 +788,29 @@ void DrawPlayer(struct PlayerInfo *player)
   MarkTileDirty(sx,sy);
 }
 
-static int getGraphicAnimationPhase(int frames, int delay, int mode)
+void DrawGraphicAnimationExt(DrawBuffer *dst_bitmap, int x, int y,
+                            int graphic, int mask_mode)
 {
-  int phase;
-
-  if (mode & ANIM_PINGPONG)
-  {
-    int max_anim_frames = 2 * frames - 2;
+  int frame = getGraphicAnimationFrame(graphic, -1);
 
-    phase = (FrameCounter % (delay * max_anim_frames)) / delay;
-    phase = (phase < frames ? phase : max_anim_frames - phase);
-  }
+  if (mask_mode == USE_MASKING)
+    DrawGraphicThruMaskExt(dst_bitmap, x, y, graphic, frame);
   else
-    phase = (FrameCounter % (delay * frames)) / delay;
-
-  if (mode & ANIM_REVERSE)
-    phase = -phase;
-
-  return phase;
-}
-
-int getGraphicAnimationFrame(int graphic, int sync_frame)
-{
-  int num_frames = new_graphic_info[graphic].anim_frames;
-  int delay = new_graphic_info[graphic].anim_delay;
-  int mode = new_graphic_info[graphic].anim_mode;
-  int frame = 0;
-
-  /* animation synchronized with global frame counter, not move position */
-  if (new_graphic_info[graphic].anim_global_sync || sync_frame < 0)
-    sync_frame = FrameCounter;
-
-  sync_frame += new_graphic_info[graphic].anim_start_frame * delay;
-
-  if (mode & ANIM_LOOP)                        /* normal, looping animation */
-  {
-    frame = (sync_frame % (delay * num_frames)) / delay;
-  }
-  else if (mode & ANIM_LINEAR)         /* normal, non-looping animation */
-  {
-    frame = sync_frame / delay;
-
-    if (frame > num_frames - 1)
-      frame = num_frames - 1;
-  }
-  else if (mode & ANIM_PINGPONG)       /* use border frames once */
-  {
-    int max_anim_frames = 2 * num_frames - 2;
-
-    frame = (sync_frame % (delay * max_anim_frames)) / delay;
-    frame = (frame < num_frames ? frame : max_anim_frames - frame);
-  }
-  else if (mode & ANIM_PINGPONG2)      /* use border frames twice */
-  {
-    int max_anim_frames = 2 * num_frames;
-
-    frame = (sync_frame % (delay * max_anim_frames)) / delay;
-    frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
-  }
-
-  if (mode & ANIM_REVERSE)             /* use reverse animation direction */
-    frame = num_frames - frame - 1;
-
-  return frame;
+    DrawGraphicExt(dst_bitmap, x, y, graphic, frame);
 }
 
-void DrawGraphicAnimationExt(int x, int y, int graphic, int mask_mode)
+void DrawGraphicAnimation(int x, int y, int graphic)
 {
-#if 0
-  int delay = new_graphic_info[graphic].anim_delay;
-
-  if (!(FrameCounter % delay) && IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
-#else
-  if (IN_SCR_FIELD(SCREENX(x), SCREENY(y)))
-#endif
-  {
-    int frame = getGraphicAnimationFrame(graphic, -1);
+  if (!IN_SCR_FIELD(x, y) ||
+      (FrameCounter % new_graphic_info[graphic].anim_delay) != 0)
+    return;
 
-    if (mask_mode == USE_MASKING)
-      DrawGraphicThruMask(SCREENX(x), SCREENY(y), graphic, frame);
-    else
-      DrawGraphic(SCREENX(x), SCREENY(y), graphic, frame);
-  }
+  DrawGraphicAnimationExt(drawto_field, FX + x * TILEX, FY + y * TILEY,
+                         graphic, NO_MASKING);
+  MarkTileDirty(x, y);
 }
 
 #if 0
-void DrawOldGraphicAnimation(int x, int y, int graphic,
-                            int frames, int delay, int mode)
-{
-  DrawGraphicAnimationExt(x, y, graphic, frames, delay, mode, NO_MASKING);
-}
-#endif
-
-void DrawGraphicAnimation(int x, int y, int graphic)
-{
-  DrawGraphicAnimationExt(x, y, graphic, NO_MASKING);
-}
-
-#if 1
 void getOldGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
 {
   if (graphic >= 0 && graphic_info[graphic].bitmap != NULL)
@@ -848,54 +823,67 @@ void getOldGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
           graphic <= GFX_END_ROCKSELEMENTS)
   {
     graphic -= GFX_START_ROCKSELEMENTS;
-    *bitmap = pix[PIX_ELEMENTS];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_ELEMENTS].bitmap;
     *x = (graphic % GFX_PER_LINE) * TILEX;
     *y = (graphic / GFX_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSHEROES && graphic <= GFX_END_ROCKSHEROES)
   {
     graphic -= GFX_START_ROCKSHEROES;
-    *bitmap = pix[PIX_HEROES];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_HEROES].bitmap;
     *x = (graphic % HEROES_PER_LINE) * TILEX;
     *y = (graphic / HEROES_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSSP && graphic <= GFX_END_ROCKSSP)
   {
     graphic -= GFX_START_ROCKSSP;
-    *bitmap = pix[PIX_SP];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_SP].bitmap;
     *x = (graphic % SP_PER_LINE) * TILEX;
     *y = (graphic / SP_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSDC && graphic <= GFX_END_ROCKSDC)
   {
     graphic -= GFX_START_ROCKSDC;
-    *bitmap = pix[PIX_DC];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_DC].bitmap;
     *x = (graphic % DC_PER_LINE) * TILEX;
     *y = (graphic / DC_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSMORE && graphic <= GFX_END_ROCKSMORE)
   {
     graphic -= GFX_START_ROCKSMORE;
-    *bitmap = pix[PIX_MORE];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_MORE].bitmap;
     *x = (graphic % MORE_PER_LINE) * TILEX;
     *y = (graphic / MORE_PER_LINE) * TILEY;
   }
   else if (graphic >= GFX_START_ROCKSFONT && graphic <= GFX_END_ROCKSFONT)
   {
     graphic -= GFX_START_ROCKSFONT;
-    *bitmap = pix[PIX_FONT_EM];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_FONT_EM].bitmap;
     *x = (graphic % FONT_CHARS_PER_LINE) * TILEX;
     *y = (graphic / FONT_CHARS_PER_LINE) * TILEY;
   }
   else
   {
-    *bitmap = pix[PIX_SP];
+    *bitmap = new_graphic_info[IMG_OLD_PIX_SP].bitmap;
     *x = 0;
     *y = 0;
   }
 }
 #endif
 
+void getGraphicSource(int graphic, int frame, Bitmap **bitmap, int *x, int *y)
+{
+  Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
+  int offset_x = new_graphic_info[graphic].offset_x;
+  int offset_y = new_graphic_info[graphic].offset_y;
+  int src_x = new_graphic_info[graphic].src_x + frame * offset_x;
+  int src_y = new_graphic_info[graphic].src_y + frame * offset_y;
+
+  *bitmap = src_bitmap;
+  *x = src_x;
+  *y = src_y;
+}
+
 void DrawGraphic(int x, int y, int graphic, int frame)
 {
 #if DEBUG
@@ -911,6 +899,7 @@ void DrawGraphic(int x, int y, int graphic, int frame)
   MarkTileDirty(x, y);
 }
 
+#if 0
 void DrawOldGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic)
 {
   Bitmap *src_bitmap;
@@ -919,10 +908,17 @@ void DrawOldGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic)
   getOldGraphicSource(graphic, &src_bitmap, &src_x, &src_y);
   BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, TILEX, TILEY, x, y);
 }
+#endif
 
 void DrawGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic,
                    int frame)
 {
+#if 1
+  Bitmap *src_bitmap;
+  int src_x, src_y;
+
+  getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
+#else
   Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
   int src_x = new_graphic_info[graphic].src_x;
   int src_y = new_graphic_info[graphic].src_y;
@@ -931,6 +927,7 @@ void DrawGraphicExt(DrawBuffer *dst_bitmap, int x, int y, int graphic,
 
   src_x += frame * offset_x;
   src_y += frame * offset_y;
+#endif
 
   BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, TILEX, TILEY, x, y);
 }
@@ -954,8 +951,16 @@ void DrawGraphicThruMask(int x, int y, int graphic, int frame)
 void DrawGraphicThruMaskExt(DrawBuffer *d, int dest_x, int dest_y, int graphic,
                            int frame)
 {
-  Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
+#if 1
+  Bitmap *src_bitmap;
+  int src_x, src_y;
+  GC drawing_gc;
+
+  getGraphicSource(graphic, frame, &src_bitmap, &src_x, &src_y);
+  drawing_gc = src_bitmap->stored_clip_gc;
+#else
   GC drawing_gc = src_bitmap->stored_clip_gc;
+  Bitmap *src_bitmap = new_graphic_info[graphic].bitmap;
   int src_x = new_graphic_info[graphic].src_x;
   int src_y = new_graphic_info[graphic].src_y;
   int offset_x = new_graphic_info[graphic].offset_x;
@@ -964,6 +969,8 @@ void DrawGraphicThruMaskExt(DrawBuffer *d, int dest_x, int dest_y, int graphic,
   src_x += frame * offset_x;
   src_y += frame * offset_y;
 
+#endif
+
   SetClipOrigin(src_bitmap, drawing_gc, dest_x - src_x, dest_y - src_y);
   BlitBitmapMasked(src_bitmap, d, src_x, src_y, TILEX, TILEY, dest_x, dest_y);
 }
@@ -982,6 +989,17 @@ void getMiniGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
   int src_x = mini_startx + new_graphic_info[graphic].src_x / 2;
   int src_y = mini_starty + new_graphic_info[graphic].src_y / 2;
 
+  if (src_x + MINI_TILEX > src_bitmap->width ||
+      src_y + MINI_TILEY > src_bitmap->height)
+  {
+    /* graphic of desired size seems not to be contained in this image;
+       dirty workaround: get it from the middle of the normal sized image */
+
+    getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
+    src_x += (TILEX / 2 - MINI_TILEX / 2);
+    src_y += (TILEY / 2 - MINI_TILEY / 2);
+  }
+
   *bitmap = src_bitmap;
   *x = src_x;
   *y = src_y;
@@ -1308,6 +1326,12 @@ inline static int getGfxAction(int x, int y)
   gfx_action = GfxAction[x][y];
 #endif
 
+#if DEBUG
+  if (gfx_action < 0)
+    printf("getGfxAction: THIS SHOULD NEVER HAPPEN: GfxAction[%d][%d] == %d\n",
+          x, y, gfx_action);
+#endif
+
   return gfx_action;
 }
 
@@ -1315,11 +1339,23 @@ void DrawScreenElementExt(int x, int y, int dx, int dy, int element,
                          int cut_mode, int mask_mode)
 {
   int ux = LEVELX(x), uy = LEVELY(y);
-  int move_dir = MovDir[ux][uy];
-  int move_pos = getFramePosition(ux, uy);
-  int gfx_action = getGfxAction(ux, uy);
-  int graphic = el_dir_act2img(element, move_dir, gfx_action);
-  int frame = getGraphicAnimationFrame(graphic, move_pos);
+  int graphic;
+  int frame;
+
+  if (IN_LEV_FIELD(ux, uy))
+  {
+    int move_dir = MovDir[ux][uy];
+    int move_pos = getFramePosition(ux, uy);
+    int gfx_action = getGfxAction(ux, uy);
+
+    graphic = el_dir_act2img(element, move_dir, gfx_action);
+    frame = getGraphicAnimationFrame(graphic, move_pos);
+  }
+  else
+  {
+    graphic = el2img(element);
+    frame = getGraphicAnimationFrame(graphic, 0);
+  }
 
   if (element == EL_WALL_GROWING)
   {
@@ -1699,6 +1735,17 @@ void getMicroGraphicSource(int graphic, Bitmap **bitmap, int *x, int *y)
   int src_x = mini_startx + new_graphic_info[graphic].src_x / 8;
   int src_y = mini_starty + new_graphic_info[graphic].src_y / 8;
 
+  if (src_x + MICRO_TILEX > src_bitmap->width ||
+      src_y + MICRO_TILEY > src_bitmap->height)
+  {
+    /* graphic of desired size seems not to be contained in this image;
+       dirty workaround: get it from the middle of the normal sized image */
+
+    getGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
+    src_x += (TILEX / 2 - MICRO_TILEX / 2);
+    src_y += (TILEY / 2 - MICRO_TILEY / 2);
+  }
+
   *bitmap = src_bitmap;
   *x = src_x;
   *y = src_y;
@@ -1708,12 +1755,7 @@ void DrawMicroElement(int xpos, int ypos, int element)
 {
   Bitmap *src_bitmap;
   int src_x, src_y;
-  int graphic;
-
-  if (element == EL_EMPTY)
-    return;
-
-  graphic = el2img(element);
+  int graphic = el2img(element);
 
   getMicroGraphicSource(graphic, &src_bitmap, &src_x, &src_y);
   BlitBitmap(src_bitmap, drawto, src_x, src_y, MICRO_TILEX, MICRO_TILEY,
@@ -1724,6 +1766,7 @@ void DrawLevel()
 {
   int x,y;
 
+  SetDrawBackgroundMask(REDRAW_NONE);
   ClearWindow();
 
   for(x=BX1; x<=BX2; x++)
@@ -1748,7 +1791,7 @@ static void DrawMicroLevelExt(int xpos, int ypos, int from_x, int from_y)
 {
   int x, y;
 
-  ClearRectangle(drawto, xpos, ypos, MICROLEV_XSIZE, MICROLEV_YSIZE);
+  DrawBackground(xpos, ypos, MICROLEV_XSIZE, MICROLEV_YSIZE);
 
   if (lev_fieldx < STD_LEV_FIELDX)
     xpos += (STD_LEV_FIELDX - lev_fieldx) / 2 * MICRO_TILEX;
@@ -1767,7 +1810,8 @@ static void DrawMicroLevelExt(int xpos, int ypos, int from_x, int from_y)
       if (lx >= 0 && lx < lev_fieldx && ly >= 0 && ly < lev_fieldy)
        DrawMicroElement(xpos + x * MICRO_TILEX, ypos + y * MICRO_TILEY,
                         Ur[lx][ly]);
-      else if (lx >= -1 && lx < lev_fieldx+1 && ly >= -1 && ly < lev_fieldy+1)
+      else if (lx >= -1 && lx < lev_fieldx+1 && ly >= -1 && ly < lev_fieldy+1
+              && BorderElement != EL_EMPTY)
        DrawMicroElement(xpos + x * MICRO_TILEX, ypos + y * MICRO_TILEY,
                         BorderElement);
     }
@@ -1789,7 +1833,7 @@ static void DrawMicroLevelLabelExt(int mode)
 {
   char label_text[MAX_MICROLABEL_SIZE + 1];
 
-  ClearRectangle(drawto, SX, MICROLABEL_YPOS, SXSIZE, FONT4_YSIZE);
+  DrawBackground(SX, MICROLABEL_YPOS, SXSIZE, FONT4_YSIZE);
 
   strncpy(label_text, (mode == MICROLABEL_LEVEL_NAME ? level.name :
                       mode == MICROLABEL_CREATED_BY ? "created by" :
@@ -1937,12 +1981,14 @@ boolean Request(char *text, unsigned int req_state)
   CloseDoor(DOOR_CLOSE_1);
 
   /* save old door content */
-  BlitBitmap(pix[PIX_DB_DOOR], pix[PIX_DB_DOOR],
+  BlitBitmap(bitmap_db_door, bitmap_db_door,
             DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE,
             DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1);
 
+  SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
+
   /* clear door drawing field */
-  ClearRectangle(drawto, DX, DY, DXSIZE, DYSIZE);
+  DrawBackground(DX, DY, DXSIZE, DYSIZE);
 
   /* write text for request */
   for(ty=0; ty < MAX_REQUEST_LINES; ty++)
@@ -1970,8 +2016,8 @@ boolean Request(char *text, unsigned int req_state)
     strncpy(text_line, text, tl);
     text_line[tl] = 0;
 
-    DrawTextExt(drawto, DX + 50 - (tl * 14)/2, DY + 8 + ty * 16,
-               text_line, FS_SMALL, FC_YELLOW);
+    DrawText(DX + 50 - (tl * 14)/2, DY + 8 + ty * 16,
+            text_line, FS_SMALL, FC_YELLOW);
 
     text += tl + (tc == ' ' ? 1 : 0);
   }
@@ -1994,7 +2040,7 @@ boolean Request(char *text, unsigned int req_state)
   }
 
   /* copy request gadgets to door backbuffer */
-  BlitBitmap(drawto, pix[PIX_DB_DOOR],
+  BlitBitmap(drawto, bitmap_db_door,
             DX, DY, DXSIZE, DYSIZE,
             DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
 
@@ -2005,7 +2051,11 @@ boolean Request(char *text, unsigned int req_state)
 #endif
 
   if (!(req_state & REQUEST_WAIT_FOR))
-    return(FALSE);
+  {
+    SetDrawBackgroundMask(REDRAW_FIELD);
+
+    return FALSE;
+  }
 
   if (game_status != MAINMENU)
     InitAnimation();
@@ -2014,6 +2064,8 @@ boolean Request(char *text, unsigned int req_state)
 
   request_gadget_id = -1;
 
+  SetDrawBackgroundMask(REDRAW_FIELD | REDRAW_DOOR_1);
+
   while(result < 0)
   {
     if (PendingEvent())
@@ -2140,7 +2192,7 @@ boolean Request(char *text, unsigned int req_state)
 
     if (!(req_state & REQ_STAY_CLOSED) && (old_door_state & DOOR_OPEN_1))
     {
-      BlitBitmap(pix[PIX_DB_DOOR], pix[PIX_DB_DOOR],
+      BlitBitmap(bitmap_db_door, bitmap_db_door,
                 DOOR_GFX_PAGEX2,DOOR_GFX_PAGEY1, DXSIZE,DYSIZE,
                 DOOR_GFX_PAGEX1,DOOR_GFX_PAGEY1);
       OpenDoor(DOOR_OPEN_1);
@@ -2149,6 +2201,8 @@ boolean Request(char *text, unsigned int req_state)
 
   RemapAllGadgets();
 
+  SetDrawBackgroundMask(REDRAW_FIELD);
+
 #if defined(PLATFORM_UNIX)
   /* continue network game after request */
   if (options.network &&
@@ -2157,7 +2211,7 @@ boolean Request(char *text, unsigned int req_state)
     SendToServer_ContinuePlaying();
 #endif
 
-  return(result);
+  return result;
 }
 
 unsigned int OpenDoor(unsigned int door_state)
@@ -2166,7 +2220,7 @@ unsigned int OpenDoor(unsigned int door_state)
 
   if (door_state & DOOR_COPY_BACK)
   {
-    BlitBitmap(pix[PIX_DB_DOOR], pix[PIX_DB_DOOR],
+    BlitBitmap(bitmap_db_door, bitmap_db_door,
               DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE + VYSIZE,
               DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
     door_state &= ~DOOR_COPY_BACK;
@@ -2181,9 +2235,9 @@ unsigned int CloseDoor(unsigned int door_state)
 {
   unsigned int new_door_state;
 
-  BlitBitmap(backbuffer, pix[PIX_DB_DOOR],
+  BlitBitmap(backbuffer, bitmap_db_door,
             DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
-  BlitBitmap(backbuffer, pix[PIX_DB_DOOR],
+  BlitBitmap(backbuffer, bitmap_db_door,
             VX, VY, VXSIZE, VYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
 
   new_door_state = MoveDoor(door_state);
@@ -2235,10 +2289,17 @@ unsigned int MoveDoor(unsigned int door_state)
   {
     stepsize = 20;
     door_delay_value = 0;
+
     StopSound(SND_MENU_DOOR_OPENING);
     StopSound(SND_MENU_DOOR_CLOSING);
   }
 
+  if (global.autoplay_leveldir)
+  {
+    door_state |= DOOR_NO_DELAY;
+    door_state &= ~DOOR_CLOSE_ALL;
+  }
+
   if (door_state & DOOR_ACTION)
   {
     if (!(door_state & DOOR_NO_DELAY))
@@ -2254,17 +2315,18 @@ unsigned int MoveDoor(unsigned int door_state)
 
     for(x=start; x<=DXSIZE; x+=stepsize)
     {
-      Bitmap *bitmap = pix[PIX_DOOR];
+      Bitmap *bitmap = new_graphic_info[IMG_GLOBAL_DOOR].bitmap;
       GC gc = bitmap->stored_clip_gc;
 
-      WaitUntilDelayReached(&door_delay, door_delay_value);
+      if (!(door_state & DOOR_NO_DELAY))
+       WaitUntilDelayReached(&door_delay, door_delay_value);
 
       if (door_state & DOOR_ACTION_1)
       {
        int i = (door_state & DOOR_OPEN_1 ? DXSIZE-x : x);
        int j = (DXSIZE - i) / 3;
 
-       BlitBitmap(pix[PIX_DB_DOOR], drawto,
+       BlitBitmap(bitmap_db_door, drawto,
                   DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1 + i/2,
                   DXSIZE,DYSIZE - i/2, DX, DY);
 
@@ -2307,7 +2369,7 @@ unsigned int MoveDoor(unsigned int door_state)
        int i = (door_state & DOOR_OPEN_2 ? VXSIZE - x : x);
        int j = (VXSIZE - i) / 3;
 
-       BlitBitmap(pix[PIX_DB_DOOR], drawto,
+       BlitBitmap(bitmap_db_door, drawto,
                   DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2 + i/2,
                   VXSIZE, VYSIZE - i/2, VX, VY);
 
@@ -2359,8 +2421,12 @@ unsigned int MoveDoor(unsigned int door_state)
 void DrawSpecialEditorDoor()
 {
   /* draw bigger toolbox window */
-  BlitBitmap(pix[PIX_DOOR], drawto,
-            DOOR_GFX_PAGEX7, 0, 108, 56, EX - 4, EY - 12);
+  BlitBitmap(new_graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
+            DOOR_GFX_PAGEX7, 0, EXSIZE + 8, 8,
+            EX - 4, EY - 12);
+  BlitBitmap(new_graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
+            EX - 4, VY - 4, EXSIZE + 8, EYSIZE - VYSIZE + 4,
+            EX - 4, EY - 4);
 
   redraw_mask |= REDRAW_ALL;
 }
@@ -2368,8 +2434,9 @@ void DrawSpecialEditorDoor()
 void UndrawSpecialEditorDoor()
 {
   /* draw normal tape recorder window */
-  BlitBitmap(pix[PIX_BACK], drawto,
-            562, 344, 108, 56, EX - 4, EY - 12);
+  BlitBitmap(new_graphic_info[IMG_GLOBAL_BORDER].bitmap, drawto,
+            EX - 4, EY - 12, EXSIZE + 8, EYSIZE - VYSIZE + 12,
+            EX - 4, EY - 12);
 
   redraw_mask |= REDRAW_ALL;
 }
@@ -2497,7 +2564,7 @@ void CreateToolButtons()
 
   for (i=0; i<NUM_TOOL_BUTTONS; i++)
   {
-    Bitmap *gd_bitmap = pix[PIX_DOOR];
+    Bitmap *gd_bitmap = new_graphic_info[IMG_GLOBAL_DOOR].bitmap;
     Bitmap *deco_bitmap = None;
     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
     struct GadgetInfo *gi;
@@ -2549,6 +2616,14 @@ void CreateToolButtons()
   }
 }
 
+void FreeToolButtons()
+{
+  int i;
+
+  for (i=0; i<NUM_TOOL_BUTTONS; i++)
+    FreeGadget(tool_gadget[i]);
+}
+
 static void UnmapToolButtons()
 {
   int i;
@@ -2904,30 +2979,15 @@ int el2gfx(int element)
 
 int el2img(int element)
 {
-#if 1
-  int graphic_NEW = element_info[element].graphic[GFX_ACTION_DEFAULT];
+  int graphic = element_info[element].graphic[GFX_ACTION_DEFAULT];
 
 #if DEBUG
-  if (graphic_NEW < 0)
+  if (graphic < 0)
     Error(ERR_WARN, "element %d -> graphic %d -- probably crashing now...",
-         element, graphic_NEW);
+         element, graphic);
 #endif
 
-  return graphic_NEW;
-#else
-
-  switch(element)
-  {
-    case EL_BD_BUTTERFLY:      return IMG_BD_BUTTERFLY;
-    case EL_BD_FIREFLY:                return IMG_BD_FIREFLY;
-    case EL_SP_ELECTRON:       return IMG_SP_ELECTRON;
-
-    default:
-      break;
-  }
-
-  return IMG_EMPTY;
-#endif
+  return graphic;
 }
 
 int el_dir2img(int element, int direction)
@@ -2937,6 +2997,24 @@ int el_dir2img(int element, int direction)
 
 int el_dir_act2img(int element, int direction, int action)
 {
+#if DEBUG
+  if (element < 0)
+  {    
+    printf("el_dir_act2img: THIS SHOULD NEVER HAPPEN: element == %d\n",
+          element);
+
+    return IMG_EMPTY;
+  }
+
+  if (action < 0)
+  {    
+    printf("el_dir_act2img: THIS SHOULD NEVER HAPPEN: action == %d\n",
+          action);
+
+    return IMG_EMPTY;
+  }
+#endif
+
   action = graphics_action_mapping[action];
   direction = MV_DIR_BIT(direction);