fixed bug with even sized playfield screen and levels without border element
[rocksndiamonds.git] / src / tools.c
index b2668d026ae34c8e02ce56f9d5785ac0fe3aaec1..c2b1ec15c80b9e31719171cb40b7d587f4f33478 100644 (file)
@@ -217,9 +217,10 @@ static int getFieldbufferOffsetX_RND(void)
 
   if (EVEN(SCR_FIELDX))
   {
+    int sbx_right = SBX_Right + (BorderElement != EL_EMPTY ? 1 : 0);
     int ffx = (scroll_x - SBX_Left)  * TILEX_VAR + dx_var;
 
-    if (ffx < SBX_Right * TILEX_VAR + TILEX_VAR / 2 + TILEX_VAR)
+    if (ffx < sbx_right * TILEX_VAR + TILEX_VAR / 2)
       fx += dx_var - MIN(ffx, TILEX_VAR / 2) + TILEX_VAR;
     else
       fx += (dx_var > 0 ? TILEX_VAR : 0);
@@ -249,9 +250,10 @@ static int getFieldbufferOffsetY_RND(void)
 
   if (EVEN(SCR_FIELDY))
   {
+    int sby_lower = SBY_Lower + (BorderElement != EL_EMPTY ? 1 : 0);
     int ffy = (scroll_y - SBY_Upper) * TILEY_VAR + dy_var;
 
-    if (ffy < SBY_Lower * TILEY_VAR + TILEY_VAR / 2 + TILEY_VAR)
+    if (ffy < sby_lower * TILEY_VAR + TILEY_VAR / 2)
       fy += dy_var - MIN(ffy, TILEY_VAR / 2) + TILEY_VAR;
     else
       fy += (dy_var > 0 ? TILEY_VAR : 0);
@@ -1042,8 +1044,9 @@ void FadeIn(int fade_mask)
   FADE_SXSIZE = FULL_SXSIZE;
   FADE_SYSIZE = FULL_SYSIZE;
 
-  if (game_status == GAME_MODE_PLAYING &&
-      strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
+  // activate virtual buttons depending on upcoming game status
+  if (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS) &&
+      game_status == GAME_MODE_PLAYING && !tape.playing)
     SetOverlayActive(TRUE);
 
   SetScreenStates_AfterFadingIn();
@@ -1149,10 +1152,12 @@ void FadeSkipNextFadeOut(void)
 
 static Bitmap *getBitmapFromGraphicOrDefault(int graphic, int default_graphic)
 {
+  if (graphic == IMG_UNDEFINED)
+    return NULL;
+
   boolean redefined = getImageListEntryFromImageID(graphic)->redefined;
 
-  return (graphic == IMG_UNDEFINED ? NULL :
-         graphic_info[graphic].bitmap != NULL || redefined ?
+  return (graphic_info[graphic].bitmap != NULL || redefined ?
          graphic_info[graphic].bitmap :
          graphic_info[default_graphic].bitmap);
 }
@@ -2273,7 +2278,9 @@ static void DrawLevelFieldCrumbledExt(int x, int y, int graphic, int frame)
          !IN_SCR_FIELD(sxx, syy))
        continue;
 
-      if (Feld[xx][yy] == EL_ELEMENT_SNAPPING)
+      // do not crumble fields that are being digged or snapped
+      if (Feld[xx][yy] == EL_EMPTY ||
+         Feld[xx][yy] == EL_ELEMENT_SNAPPING)
        continue;
 
       element = TILE_GFX_ELEMENT(xx, yy);
@@ -3513,12 +3520,13 @@ static void DrawPreviewLevelExt(boolean restart)
   }
 }
 
-static void DrawPreviewPlayers(void)
+void DrawPreviewPlayers(void)
 {
   if (game_status != GAME_MODE_MAIN)
     return;
 
-  if (!network.enabled && !setup.team_mode)
+  // do not draw preview players if level preview redefined, but players aren't
+  if (preview.redefined && !menu.main.preview_players.redefined)
     return;
 
   boolean player_found[MAX_PLAYERS];
@@ -3569,6 +3577,9 @@ static void DrawPreviewPlayers(void)
   ClearRectangleOnBackground(drawto, max_xpos, max_ypos,
                             max_players_width, max_players_height);
 
+  if (!network.enabled && !setup.team_mode)
+    return;
+
   // only draw players if level is suited for team mode
   if (num_players < 2)
     return;
@@ -3623,6 +3634,10 @@ static void DrawNetworkPlayersExt(boolean force)
   if (!network.connected && !force)
     return;
 
+  // do not draw network players if level preview redefined, but players aren't
+  if (preview.redefined && !menu.main.network_players.redefined)
+    return;
+
   int num_players = 0;
   int i;
 
@@ -3829,87 +3844,122 @@ static boolean equalGraphics(int graphic1, int graphic2)
          g1->anim_mode   == g2->anim_mode);
 }
 
-void DrawAllPlayers(void)
+#define DRAW_PLAYER_OVER_PUSHED_ELEMENT        1
+
+enum
 {
-  int i;
+  DRAW_PLAYER_STAGE_INIT = 0,
+  DRAW_PLAYER_STAGE_LAST_FIELD,
+  DRAW_PLAYER_STAGE_FIELD_UNDER_PLAYER,
+#if DRAW_PLAYER_OVER_PUSHED_ELEMENT
+  DRAW_PLAYER_STAGE_ELEMENT_PUSHED,
+  DRAW_PLAYER_STAGE_PLAYER,
+#else
+  DRAW_PLAYER_STAGE_PLAYER,
+  DRAW_PLAYER_STAGE_ELEMENT_PUSHED,
+#endif
+  DRAW_PLAYER_STAGE_ELEMENT_OVER_PLAYER,
+  DRAW_PLAYER_STAGE_FIELD_OVER_PLAYER,
 
-  for (i = 0; i < MAX_PLAYERS; i++)
-    if (stored_player[i].active)
-      DrawPlayer(&stored_player[i]);
-}
+  NUM_DRAW_PLAYER_STAGES
+};
 
-void DrawPlayerField(int x, int y)
+static void DrawPlayerExt(struct PlayerInfo *player, int drawing_stage)
 {
-  if (!IS_PLAYER(x, y))
+  static int static_last_player_graphic[MAX_PLAYERS];
+  static int static_last_player_frame[MAX_PLAYERS];
+  static boolean static_player_is_opaque[MAX_PLAYERS];
+  static boolean draw_player[MAX_PLAYERS];
+  int pnr = player->index_nr;
+
+  if (drawing_stage == DRAW_PLAYER_STAGE_INIT)
+  {
+    static_last_player_graphic[pnr] = getPlayerGraphic(player, player->MovDir);
+    static_last_player_frame[pnr] = player->Frame;
+    static_player_is_opaque[pnr] = FALSE;
+
+    draw_player[pnr] = TRUE;
+  }
+
+  if (!draw_player[pnr])
     return;
 
-  DrawPlayer(PLAYERINFO(x, y));
-}
+#if DEBUG
+  if (!IN_LEV_FIELD(player->jx, player->jy))
+  {
+    printf("DrawPlayerField(): x = %d, y = %d\n", player->jx, player->jy);
+    printf("DrawPlayerField(): This should never happen!\n");
 
-#define DRAW_PLAYER_OVER_PUSHED_ELEMENT        1
+    draw_player[pnr] = FALSE;
+
+    return;
+  }
+#endif
+
+  int last_player_graphic  = static_last_player_graphic[pnr];
+  int last_player_frame    = static_last_player_frame[pnr];
+  boolean player_is_opaque = static_player_is_opaque[pnr];
 
-void DrawPlayer(struct PlayerInfo *player)
-{
   int jx = player->jx;
   int jy = player->jy;
-  int move_dir = player->MovDir;
+  int move_dir = (player->is_waiting ? player->dir_waiting : player->MovDir);
   int dx = (move_dir == MV_LEFT ? -1 : move_dir == MV_RIGHT ? +1 : 0);
   int dy = (move_dir == MV_UP   ? -1 : move_dir == MV_DOWN  ? +1 : 0);
   int last_jx = (player->is_moving ? jx - dx : jx);
   int last_jy = (player->is_moving ? jy - dy : jy);
   int next_jx = jx + dx;
   int next_jy = jy + dy;
-  boolean player_is_moving = (player->MovPos ? TRUE : FALSE);
-  boolean player_is_opaque = FALSE;
-  int sx = SCREENX(jx), sy = SCREENY(jy);
-  int sxx = 0, syy = 0;
-  int element = Feld[jx][jy], last_element = Feld[last_jx][last_jy];
-  int graphic;
-  int action = ACTION_DEFAULT;
-  int last_player_graphic = getPlayerGraphic(player, move_dir);
-  int last_player_frame = player->Frame;
-  int frame = 0;
+  boolean player_is_moving = (player->MovPos != 0 ? TRUE : FALSE);
+  int sx = SCREENX(jx);
+  int sy = SCREENY(jy);
+  int sxx = (move_dir == MV_LEFT || move_dir == MV_RIGHT ? player->GfxPos : 0);
+  int syy = (move_dir == MV_UP   || move_dir == MV_DOWN  ? player->GfxPos : 0);
+  int element = Feld[jx][jy];
+  int last_element = Feld[last_jx][last_jy];
+  int action = (player->is_pushing    ? ACTION_PUSHING         :
+               player->is_digging    ? ACTION_DIGGING         :
+               player->is_collecting ? ACTION_COLLECTING      :
+               player->is_moving     ? ACTION_MOVING          :
+               player->is_snapping   ? ACTION_SNAPPING        :
+               player->is_dropping   ? ACTION_DROPPING        :
+               player->is_waiting    ? player->action_waiting :
+               ACTION_DEFAULT);
+
+  if (drawing_stage == DRAW_PLAYER_STAGE_INIT)
+  {
+    // ------------------------------------------------------------------------
+    // initialize drawing the player
+    // ------------------------------------------------------------------------
 
-  // GfxElement[][] is set to the element the player is digging or collecting;
-  // remove also for off-screen player if the player is not moving anymore
-  if (IN_LEV_FIELD(jx, jy) && !player_is_moving)
-    GfxElement[jx][jy] = EL_UNDEFINED;
+    draw_player[pnr] = FALSE;
 
-  if (!player->active || !IN_SCR_FIELD(SCREENX(last_jx), SCREENY(last_jy)))
-    return;
+    // GfxElement[][] is set to the element the player is digging or collecting;
+    // remove also for off-screen player if the player is not moving anymore
+    if (IN_LEV_FIELD(jx, jy) && !player_is_moving)
+      GfxElement[jx][jy] = EL_UNDEFINED;
 
-#if DEBUG
-  if (!IN_LEV_FIELD(jx, jy))
-  {
-    printf("DrawPlayerField(): x = %d, y = %d\n",jx,jy);
-    printf("DrawPlayerField(): sx = %d, sy = %d\n",sx,sy);
-    printf("DrawPlayerField(): This should never happen!\n");
-    return;
-  }
-#endif
+    if (!player->active || !IN_SCR_FIELD(SCREENX(last_jx), SCREENY(last_jy)))
+      return;
 
-  if (element == EL_EXPLOSION)
-    return;
+    if (element == EL_EXPLOSION)
+      return;
 
-  action = (player->is_pushing    ? ACTION_PUSHING         :
-           player->is_digging    ? ACTION_DIGGING         :
-           player->is_collecting ? ACTION_COLLECTING      :
-           player->is_moving     ? ACTION_MOVING          :
-           player->is_snapping   ? ACTION_SNAPPING        :
-           player->is_dropping   ? ACTION_DROPPING        :
-           player->is_waiting    ? player->action_waiting : ACTION_DEFAULT);
+    InitPlayerGfxAnimation(player, action, move_dir);
 
-  if (player->is_waiting)
-    move_dir = player->dir_waiting;
+    draw_player[pnr] = TRUE;
+  }
+  else if (drawing_stage == DRAW_PLAYER_STAGE_LAST_FIELD)
+  {
+    // ------------------------------------------------------------------------
+    // draw things in the field the player is leaving, if needed
+    // ------------------------------------------------------------------------
 
-  InitPlayerGfxAnimation(player, action, move_dir);
+    if (!IN_SCR_FIELD(sx, sy))
+      draw_player[pnr] = FALSE;
 
-  // --------------------------------------------------------------------------
-  // draw things in the field the player is leaving, if needed
-  // --------------------------------------------------------------------------
+    if (!player->is_moving)
+      return;
 
-  if (player->is_moving)
-  {
     if (Back[last_jx][last_jy] && IS_DRAWABLE(last_element))
     {
       DrawLevelElement(last_jx, last_jy, Back[last_jx][last_jy]);
@@ -3925,35 +3975,32 @@ void DrawPlayer(struct PlayerInfo *player)
             last_element == EL_EM_DYNAMITE_ACTIVE ||
             last_element == EL_SP_DISK_RED_ACTIVE)
       DrawDynamite(last_jx, last_jy);
-#if 0
-    /* !!! this is not enough to prevent flickering of players which are
-       moving next to each others without a free tile between them -- this
-       can only be solved by drawing all players layer by layer (first the
-       background, then the foreground etc.) !!! => TODO */
-    else if (!IS_PLAYER(last_jx, last_jy))
-      DrawLevelField(last_jx, last_jy);
-#else
     else
       DrawLevelField(last_jx, last_jy);
-#endif
 
     if (player->is_pushing && IN_SCR_FIELD(SCREENX(next_jx), SCREENY(next_jy)))
       DrawLevelElement(next_jx, next_jy, EL_EMPTY);
   }
+  else if (drawing_stage == DRAW_PLAYER_STAGE_FIELD_UNDER_PLAYER)
+  {
+    // ------------------------------------------------------------------------
+    // draw things behind the player, if needed
+    // ------------------------------------------------------------------------
 
-  if (!IN_SCR_FIELD(sx, sy))
-    return;
+    if (Back[jx][jy])
+    {
+      DrawLevelElement(jx, jy, Back[jx][jy]);
 
-  // --------------------------------------------------------------------------
-  // draw things behind the player, if needed
-  // --------------------------------------------------------------------------
+      return;
+    }
+
+    if (IS_ACTIVE_BOMB(element))
+    {
+      DrawLevelElement(jx, jy, EL_EMPTY);
+
+      return;
+    }
 
-  if (Back[jx][jy])
-    DrawLevelElement(jx, jy, Back[jx][jy]);
-  else if (IS_ACTIVE_BOMB(element))
-    DrawLevelElement(jx, jy, EL_EMPTY);
-  else
-  {
     if (player_is_moving && GfxElement[jx][jy] != EL_UNDEFINED)
     {
       int old_element = GfxElement[jx][jy];
@@ -3966,14 +4013,14 @@ void DrawPlayer(struct PlayerInfo *player)
        DrawGraphic(sx, sy, old_graphic, frame);
 
       if (graphic_info[old_graphic].anim_mode & ANIM_OPAQUE_PLAYER)
-       player_is_opaque = TRUE;
+       static_player_is_opaque[pnr] = TRUE;
     }
     else
     {
       GfxElement[jx][jy] = EL_UNDEFINED;
 
       // make sure that pushed elements are drawn with correct frame rate
-      graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
+      int graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
 
       if (player->is_pushing && player->is_moving && !IS_ANIM_MODE_CE(graphic))
        GfxFrame[jx][jy] = player->StepFrame;
@@ -3981,79 +4028,26 @@ void DrawPlayer(struct PlayerInfo *player)
       DrawLevelField(jx, jy);
     }
   }
-
-#if !DRAW_PLAYER_OVER_PUSHED_ELEMENT
-  // -----------------------------------------------------------------------
-  // draw player himself
-  // -----------------------------------------------------------------------
-
-  graphic = getPlayerGraphic(player, move_dir);
-
-  // in the case of changed player action or direction, prevent the current
-  // animation frame from being restarted for identical animations
-  if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
-    player->Frame = last_player_frame;
-
-  frame = getGraphicAnimationFrame(graphic, player->Frame);
-
-  if (player->GfxPos)
-  {
-    if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
-      sxx = player->GfxPos;
-    else
-      syy = player->GfxPos;
-  }
-
-  if (player_is_opaque)
-    DrawGraphicShifted(sx, sy, sxx, syy, graphic, frame,NO_CUTTING,NO_MASKING);
-  else
-    DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
-
-  if (SHIELD_ON(player))
-  {
-    int graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
-                  IMG_SHIELD_NORMAL_ACTIVE);
-    int frame = getGraphicAnimationFrame(graphic, -1);
-
-    DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
-  }
-#endif
-
-#if DRAW_PLAYER_OVER_PUSHED_ELEMENT
-  if (player->GfxPos)
+  else if (drawing_stage == DRAW_PLAYER_STAGE_ELEMENT_PUSHED)
   {
-    if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
-      sxx = player->GfxPos;
-    else
-      syy = player->GfxPos;
-  }
-#endif
+    // ------------------------------------------------------------------------
+    // draw things the player is pushing, if needed
+    // ------------------------------------------------------------------------
 
-  // --------------------------------------------------------------------------
-  // draw things the player is pushing, if needed
-  // --------------------------------------------------------------------------
+    if (!player->is_pushing || !player->is_moving)
+      return;
 
-  if (player->is_pushing && player->is_moving)
-  {
-    int px = SCREENX(jx), py = SCREENY(jy);
-    int pxx = (TILEX - ABS(sxx)) * dx;
-    int pyy = (TILEY - ABS(syy)) * dy;
     int gfx_frame = GfxFrame[jx][jy];
 
-    int graphic;
-    int sync_frame;
-    int frame;
-
     if (!IS_MOVING(jx, jy))            // push movement already finished
     {
       element = Feld[next_jx][next_jy];
       gfx_frame = GfxFrame[next_jx][next_jy];
     }
 
-    graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
-
-    sync_frame = (IS_ANIM_MODE_CE(graphic) ? gfx_frame : player->StepFrame);
-    frame = getGraphicAnimationFrame(graphic, sync_frame);
+    int graphic = el_act_dir2img(element, ACTION_PUSHING, move_dir);
+    int sync_frame = (IS_ANIM_MODE_CE(graphic) ? gfx_frame : player->StepFrame);
+    int frame = getGraphicAnimationFrame(graphic, sync_frame);
 
     // draw background element under pushed element (like the Sokoban field)
     if (game.use_masked_pushing && IS_MOVING(jx, jy))
@@ -4073,6 +4067,10 @@ void DrawPlayer(struct PlayerInfo *player)
     else if (Back[next_jx][next_jy])
       DrawLevelElement(next_jx, next_jy, Back[next_jx][next_jy]);
 
+    int px = SCREENX(jx), py = SCREENY(jy);
+    int pxx = (TILEX - ABS(sxx)) * dx;
+    int pyy = (TILEY - ABS(syy)) * dy;
+
 #if 1
     // do not draw (EM style) pushing animation when pushing is finished
     // (two-tile animations usually do not contain start and end frame)
@@ -4086,96 +4084,118 @@ void DrawPlayer(struct PlayerInfo *player)
     DrawGraphicShiftedThruMask(px, py, pxx, pyy, graphic, frame, NO_CUTTING);
 #endif
   }
+  else if (drawing_stage == DRAW_PLAYER_STAGE_PLAYER)
+  {
+    // ------------------------------------------------------------------------
+    // draw player himself
+    // ------------------------------------------------------------------------
 
-#if DRAW_PLAYER_OVER_PUSHED_ELEMENT
-  // -----------------------------------------------------------------------
-  // draw player himself
-  // -----------------------------------------------------------------------
-
-  graphic = getPlayerGraphic(player, move_dir);
+    int graphic = getPlayerGraphic(player, move_dir);
 
-  // in the case of changed player action or direction, prevent the current
-  // animation frame from being restarted for identical animations
-  if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
-    player->Frame = last_player_frame;
+    // in the case of changed player action or direction, prevent the current
+    // animation frame from being restarted for identical animations
+    if (player->Frame == 0 && equalGraphics(graphic, last_player_graphic))
+      player->Frame = last_player_frame;
 
-  frame = getGraphicAnimationFrame(graphic, player->Frame);
+    int frame = getGraphicAnimationFrame(graphic, player->Frame);
 
-  if (player->GfxPos)
-  {
-    if (move_dir == MV_LEFT || move_dir == MV_RIGHT)
-      sxx = player->GfxPos;
+    if (player_is_opaque)
+      DrawGraphicShifted(sx,sy, sxx,syy, graphic, frame, NO_CUTTING,NO_MASKING);
     else
-      syy = player->GfxPos;
-  }
+      DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
 
-  if (player_is_opaque)
-    DrawGraphicShifted(sx, sy, sxx, syy, graphic, frame,NO_CUTTING,NO_MASKING);
-  else
-    DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
+    if (SHIELD_ON(player))
+    {
+      graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
+                IMG_SHIELD_NORMAL_ACTIVE);
+      frame = getGraphicAnimationFrame(graphic, -1);
 
-  if (SHIELD_ON(player))
+      DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
+    }
+  }
+  else if (drawing_stage == DRAW_PLAYER_STAGE_ELEMENT_OVER_PLAYER)
   {
-    int graphic = (player->shield_deadly_time_left ? IMG_SHIELD_DEADLY_ACTIVE :
-                  IMG_SHIELD_NORMAL_ACTIVE);
-    int frame = getGraphicAnimationFrame(graphic, -1);
+    // ------------------------------------------------------------------------
+    // draw things in front of player (active dynamite or dynabombs)
+    // ------------------------------------------------------------------------
 
-    DrawGraphicShiftedThruMask(sx, sy, sxx, syy, graphic, frame, NO_CUTTING);
-  }
-#endif
+    if (IS_ACTIVE_BOMB(element))
+    {
+      int graphic = el2img(element);
+      int frame = getGraphicAnimationFrame(graphic, GfxFrame[jx][jy]);
 
-  // --------------------------------------------------------------------------
-  // draw things in front of player (active dynamite or dynabombs)
-  // --------------------------------------------------------------------------
+      if (game.emulation == EMU_SUPAPLEX)
+       DrawGraphic(sx, sy, IMG_SP_DISK_RED, frame);
+      else
+       DrawGraphicThruMask(sx, sy, graphic, frame);
+    }
 
-  if (IS_ACTIVE_BOMB(element))
+    if (player_is_moving && last_element == EL_EXPLOSION)
+    {
+      int element = (GfxElement[last_jx][last_jy] != EL_UNDEFINED ?
+                    GfxElement[last_jx][last_jy] :  EL_EMPTY);
+      int graphic = el_act2img(element, ACTION_EXPLODING);
+      int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
+      int phase = ExplodePhase[last_jx][last_jy] - 1;
+      int frame = getGraphicAnimationFrame(graphic, phase - delay);
+
+      if (phase >= delay)
+       DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), graphic, frame);
+    }
+  }
+  else if (drawing_stage == DRAW_PLAYER_STAGE_FIELD_OVER_PLAYER)
   {
-    graphic = el2img(element);
-    frame = getGraphicAnimationFrame(graphic, GfxFrame[jx][jy]);
+    // ------------------------------------------------------------------------
+    // draw elements the player is just walking/passing through/under
+    // ------------------------------------------------------------------------
 
-    if (game.emulation == EMU_SUPAPLEX)
-      DrawGraphic(sx, sy, IMG_SP_DISK_RED, frame);
-    else
-      DrawGraphicThruMask(sx, sy, graphic, frame);
-  }
+    if (player_is_moving)
+    {
+      // handle the field the player is leaving ...
+      if (IS_ACCESSIBLE_INSIDE(last_element))
+       DrawLevelField(last_jx, last_jy);
+      else if (IS_ACCESSIBLE_UNDER(last_element))
+       DrawLevelFieldThruMask(last_jx, last_jy);
+    }
 
-  if (player_is_moving && last_element == EL_EXPLOSION)
-  {
-    int element = (GfxElement[last_jx][last_jy] != EL_UNDEFINED ?
-                  GfxElement[last_jx][last_jy] :  EL_EMPTY);
-    int graphic = el_act2img(element, ACTION_EXPLODING);
-    int delay = (game.emulation == EMU_SUPAPLEX ? 3 : 2);
-    int phase = ExplodePhase[last_jx][last_jy] - 1;
-    int frame = getGraphicAnimationFrame(graphic, phase - delay);
+    // do not redraw accessible elements if the player is just pushing them
+    if (!player_is_moving || !player->is_pushing)
+    {
+      // ... and the field the player is entering
+      if (IS_ACCESSIBLE_INSIDE(element))
+       DrawLevelField(jx, jy);
+      else if (IS_ACCESSIBLE_UNDER(element))
+       DrawLevelFieldThruMask(jx, jy);
+    }
 
-    if (phase >= delay)
-      DrawGraphicThruMask(SCREENX(last_jx), SCREENY(last_jy), graphic, frame);
+    MarkTileDirty(sx, sy);
   }
+}
 
-  // --------------------------------------------------------------------------
-  // draw elements the player is just walking/passing through/under
-  // --------------------------------------------------------------------------
+void DrawPlayer(struct PlayerInfo *player)
+{
+  int i;
 
-  if (player_is_moving)
-  {
-    // handle the field the player is leaving ...
-    if (IS_ACCESSIBLE_INSIDE(last_element))
-      DrawLevelField(last_jx, last_jy);
-    else if (IS_ACCESSIBLE_UNDER(last_element))
-      DrawLevelFieldThruMask(last_jx, last_jy);
-  }
+  for (i = 0; i < NUM_DRAW_PLAYER_STAGES; i++)
+    DrawPlayerExt(player, i);
+}
 
-  // do not redraw accessible elements if the player is just pushing them
-  if (!player_is_moving || !player->is_pushing)
-  {
-    // ... and the field the player is entering
-    if (IS_ACCESSIBLE_INSIDE(element))
-      DrawLevelField(jx, jy);
-    else if (IS_ACCESSIBLE_UNDER(element))
-      DrawLevelFieldThruMask(jx, jy);
-  }
+void DrawAllPlayers(void)
+{
+  int i, j;
+
+  for (i = 0; i < NUM_DRAW_PLAYER_STAGES; i++)
+    for (j = 0; j < MAX_PLAYERS; j++)
+      if (stored_player[j].active)
+       DrawPlayerExt(&stored_player[j], i);
+}
+
+void DrawPlayerField(int x, int y)
+{
+  if (!IS_PLAYER(x, y))
+    return;
 
-  MarkTileDirty(sx, sy);
+  DrawPlayer(PLAYERINFO(x, y));
 }
 
 // ----------------------------------------------------------------------------
@@ -4386,7 +4406,7 @@ static int RequestHandleEvents(unsigned int req_state)
                break;
 
              default:
-               HandleKeysDebug(key);
+               HandleKeysDebug(key, KEY_PRESSED);
                break;
            }
 
@@ -4810,17 +4830,17 @@ static boolean RequestEnvelope(char *text, unsigned int req_state)
 
 boolean Request(char *text, unsigned int req_state)
 {
-  boolean overlay_active = GetOverlayActive();
+  boolean overlay_enabled = GetOverlayEnabled();
   boolean result;
 
-  SetOverlayActive(FALSE);
+  SetOverlayEnabled(FALSE);
 
   if (global.use_envelope_request)
     result = RequestEnvelope(text, req_state);
   else
     result = RequestDoor(text, req_state);
 
-  SetOverlayActive(overlay_active);
+  SetOverlayEnabled(overlay_enabled);
 
   return result;
 }
@@ -9402,29 +9422,58 @@ void SetLevelSetInfo(char *identifier, int level_nr)
   levelset.level_nr = level_nr;
 }
 
-boolean CheckIfPlayfieldViewportHasChanged(void)
+boolean CheckIfAllViewportsHaveChanged(void)
 {
-  // if game status has not changed, playfield viewport has not changed either
+  // if game status has not changed, viewports have not changed either
   if (game_status == game_status_last)
     return FALSE;
 
-  // check if playfield viewport has changed with current game status
+  // check if all viewports have changed with current game status
+
   struct RectWithBorder *vp_playfield = &viewport.playfield[game_status];
+  struct RectWithBorder *vp_door_1    = &viewport.door_1[game_status];
+  struct RectWithBorder *vp_door_2    = &viewport.door_2[game_status];
   int new_real_sx      = vp_playfield->x;
   int new_real_sy      = vp_playfield->y;
   int new_full_sxsize  = vp_playfield->width;
   int new_full_sysize  = vp_playfield->height;
+  int new_dx           = vp_door_1->x;
+  int new_dy           = vp_door_1->y;
+  int new_dxsize       = vp_door_1->width;
+  int new_dysize       = vp_door_1->height;
+  int new_vx           = vp_door_2->x;
+  int new_vy           = vp_door_2->y;
+  int new_vxsize       = vp_door_2->width;
+  int new_vysize       = vp_door_2->height;
+
+  boolean playfield_viewport_has_changed =
+    (new_real_sx != REAL_SX ||
+     new_real_sy != REAL_SY ||
+     new_full_sxsize != FULL_SXSIZE ||
+     new_full_sysize != FULL_SYSIZE);
+
+  boolean door_1_viewport_has_changed =
+    (new_dx != DX ||
+     new_dy != DY ||
+     new_dxsize != DXSIZE ||
+     new_dysize != DYSIZE);
 
-  return (new_real_sx != REAL_SX ||
-         new_real_sy != REAL_SY ||
-         new_full_sxsize != FULL_SXSIZE ||
-         new_full_sysize != FULL_SYSIZE);
+  boolean door_2_viewport_has_changed =
+    (new_vx != VX ||
+     new_vy != VY ||
+     new_vxsize != VXSIZE ||
+     new_vysize != VYSIZE ||
+     game_status_last == GAME_MODE_EDITOR);
+
+  return (playfield_viewport_has_changed &&
+         door_1_viewport_has_changed &&
+         door_2_viewport_has_changed);
 }
 
-boolean CheckIfGlobalBorderOrPlayfieldViewportHasChanged(void)
+boolean CheckFadeAll(void)
 {
   return (CheckIfGlobalBorderHasChanged() ||
-         CheckIfPlayfieldViewportHasChanged());
+         CheckIfAllViewportsHaveChanged());
 }
 
 void ChangeViewportPropertiesIfNeeded(void)
@@ -9441,11 +9490,14 @@ void ChangeViewportPropertiesIfNeeded(void)
   struct RectWithBorder *vp_door_3    = &viewport.door_2[GAME_MODE_EDITOR];
   int new_win_xsize    = vp_window->width;
   int new_win_ysize    = vp_window->height;
-  int border_size      = vp_playfield->border_size;
-  int new_sx           = vp_playfield->x + border_size;
-  int new_sy           = vp_playfield->y + border_size;
-  int new_sxsize       = vp_playfield->width  - 2 * border_size;
-  int new_sysize       = vp_playfield->height - 2 * border_size;
+  int border_left      = vp_playfield->border_left;
+  int border_right     = vp_playfield->border_right;
+  int border_top       = vp_playfield->border_top;
+  int border_bottom    = vp_playfield->border_bottom;
+  int new_sx           = vp_playfield->x      + border_left;
+  int new_sy           = vp_playfield->y      + border_top;
+  int new_sxsize       = vp_playfield->width  - border_left - border_right;
+  int new_sysize       = vp_playfield->height - border_top  - border_bottom;
   int new_real_sx      = vp_playfield->x;
   int new_real_sy      = vp_playfield->y;
   int new_full_sxsize  = vp_playfield->width;
@@ -9550,8 +9602,8 @@ void ChangeViewportPropertiesIfNeeded(void)
     }
 
     // add current and new door 2 area if position or size has changed
-    if (new_dx != VX || new_dy != VY ||
-       new_dxsize != VXSIZE || new_dysize != VYSIZE)
+    if (new_vx != VX || new_vy != VY ||
+       new_vxsize != VXSIZE || new_vysize != VYSIZE)
     {
       JoinRectangles(&FADE_SX, &FADE_SY, &FADE_SXSIZE, &FADE_SYSIZE,
                     VX, VY, VXSIZE, VYSIZE);
@@ -9623,6 +9675,8 @@ void ChangeViewportPropertiesIfNeeded(void)
   {
     // printf("::: init_video_buffer\n");
 
+    FreeAllImageTextures();    // needs old renderer to free the textures
+
     InitVideoBuffer(WIN_XSIZE, WIN_YSIZE, DEFAULT_DEPTH, setup.fullscreen);
     InitImageTextures();
   }