rnd-20060313-1-src
[rocksndiamonds.git] / src / game_em / graphics.c
index b6cad3c56f4aff9f810a4fdc364da64f6582ee18..331491cfb25c7c0209bbae411d2d54e7a009645a 100644 (file)
@@ -40,7 +40,11 @@ static int crumbled_state[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
 
 static boolean redraw[MAX_BUF_XSIZE][MAX_BUF_YSIZE];
 
+#if 1
+int centered_player_nr;
+#else
 static int centered_player_nr;
+#endif
 
 /* copy the entire screen to the window at the scroll position */
 
@@ -570,7 +574,7 @@ void DrawRelocatePlayer(struct PlayerInfo *player, boolean quick_relocation)
 }
 #endif
 
-void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
+static void setMinimalPlayerBoundaries(int *sx1, int *sy1, int *sx2, int *sy2)
 {
   boolean num_checked_players = 0;
   int i;
@@ -610,7 +614,7 @@ boolean checkIfAllPlayersFitToScreen()
          sy2 - sy1 <= SCR_FIELDY * TILEY);
 }
 
-void setScreenCenteredToAllPlayers(int *sx, int *sy)
+static void setScreenCenteredToAllPlayers(int *sx, int *sy)
 {
   int sx1 = screen_x, sy1 = screen_y, sx2 = screen_x, sy2 = screen_y;
 
@@ -620,21 +624,22 @@ void setScreenCenteredToAllPlayers(int *sx, int *sy)
   *sy = (sy1 + sy2) / 2;
 }
 
-void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy)
+static void setMaxCenterDistanceForAllPlayers(int *max_dx, int *max_dy,
+                                             int center_x, int center_y)
 {
-  int sx1 = screen_x, sy1 = screen_y, sx2 = screen_x, sy2 = screen_y;
+  int sx1 = center_x, sy1 = center_y, sx2 = center_x, sy2 = center_y;
 
   setMinimalPlayerBoundaries(&sx1, &sy1, &sx2, &sy2);
 
-  *max_dx = MAX(ABS(sx1 - screen_x), ABS(sx2 - screen_x));
-  *max_dy = MAX(ABS(sy1 - screen_y), ABS(sy2 - screen_y));
+  *max_dx = MAX(ABS(sx1 - center_x), ABS(sx2 - center_x));
+  *max_dy = MAX(ABS(sy1 - center_y), ABS(sy2 - center_y));
 }
 
-boolean checkIfAllPlayersAreVisible()
+static boolean checkIfAllPlayersAreVisible(int center_x, int center_y)
 {
   int max_dx, max_dy;
 
-  setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy);
+  setMaxCenterDistanceForAllPlayers(&max_dx, &max_dy, center_x, center_y);
 
   return (max_dx <= SCR_FIELDX * TILEX / 2 &&
          max_dy <= SCR_FIELDY * TILEY / 2);
@@ -642,11 +647,15 @@ boolean checkIfAllPlayersAreVisible()
 
 void RedrawPlayfield_EM(boolean force_redraw)
 {
+#if 0
   boolean all_players_visible = checkIfAllPlayersAreVisible();
-  boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
+#endif
   boolean draw_new_player_location = FALSE;
   boolean quick_relocation = setup.quick_switch;
+#if 0
   boolean scrolling = (screen_x % TILEX != 0 || screen_y % TILEY != 0);
+#endif
+  boolean set_centered_player = getSetCenteredPlayer_EM();
   int centered_player_nr_next = getCenteredPlayerNr_EM();
   int offset = (setup.scroll_delay ? 3 : 0) * TILEX;
   int offset_x = offset;
@@ -656,24 +665,49 @@ void RedrawPlayfield_EM(boolean force_redraw)
   int x, y, sx, sy;
   int i;
 
-  /* switching to "all players" only possible if all players fit to screen */
-  if (centered_player_nr_next == -1 && !all_players_fit_to_screen)
+  if (set_centered_player)
   {
-    setCenteredPlayerNr_EM(centered_player_nr);
+    boolean all_players_fit_to_screen = checkIfAllPlayersFitToScreen();
 
-    centered_player_nr_next = centered_player_nr;
+    /* switching to "all players" only possible if all players fit to screen */
+    if (centered_player_nr_next == -1 && !all_players_fit_to_screen)
+    {
+      centered_player_nr_next = centered_player_nr;
+      setCenteredPlayerNr_EM(centered_player_nr);
+
+      set_centered_player = FALSE;
+      setSetCenteredPlayer_EM(FALSE);
+    }
+
+    /* do not switch focus to non-existing (or non-active) player */
+    if (centered_player_nr_next >= 0 && !ply[centered_player_nr_next].alive)
+    {
+      centered_player_nr_next = centered_player_nr;
+      setCenteredPlayerNr_EM(centered_player_nr);
+
+      set_centered_player = FALSE;
+      setSetCenteredPlayer_EM(FALSE);
+    }
   }
 
-#if 0
+#if 1
+  /* also allow focus switching when screen is scrolled to half tile */
+#else
   if (!scrolling)      /* screen currently aligned at tile position */
 #endif
   {
+#if 1
+    if (set_centered_player)
+#else
     if (centered_player_nr != centered_player_nr_next)
+#endif
     {
       centered_player_nr = centered_player_nr_next;
 
       draw_new_player_location = TRUE;
       force_redraw = TRUE;
+
+      setSetCenteredPlayer_EM(FALSE);
     }
   }
 
@@ -695,6 +729,19 @@ void RedrawPlayfield_EM(boolean force_redraw)
     sy = PLAYER_SCREEN_Y(centered_player_nr);
   }
 
+  if (draw_new_player_location && quick_relocation)
+  {
+    screen_x = VALID_SCREEN_X(sx);
+    screen_y = VALID_SCREEN_Y(sy);
+    screen_x_old = screen_x;
+    screen_y_old = screen_y;
+
+#if 0
+    offset_x = 0;
+    offset_y = 0;
+#endif
+  }
+
   if (draw_new_player_location && !quick_relocation)
   {
 #if 1
@@ -715,6 +762,32 @@ void RedrawPlayfield_EM(boolean force_redraw)
       if (dx == 0 && dy == 0)          /* no scrolling needed at all */
        break;
 
+#if 1
+
+      if (ABS(screen_xx - screen_x) >= TILEX)
+      {
+       screen_x -= dx * TILEX;
+       dxx = dx * TILEX / 2;
+      }
+      else
+      {
+       screen_x = screen_xx;
+       dxx = 0;
+      }
+
+      if (ABS(screen_yy - screen_y) >= TILEY)
+      {
+       screen_y -= dy * TILEY;
+       dyy = dy * TILEY / 2;
+      }
+      else
+      {
+       screen_y = screen_yy;
+       dyy = 0;
+      }
+
+#else
+
 #if 1
       if (ABS(screen_xx - screen_x) >= TILEX ||
          ABS(screen_yy - screen_y) >= TILEY)
@@ -739,6 +812,8 @@ void RedrawPlayfield_EM(boolean force_redraw)
 
       dxx += dx * TILEX / 2;
       dyy += dy * TILEY / 2;
+#endif
+
 #endif
 
       /* scroll in two steps of half tile size to make things smoother */
@@ -808,13 +883,29 @@ void RedrawPlayfield_EM(boolean force_redraw)
   /* prevent scrolling away from the other players when focus on all players */
   if (centered_player_nr == -1)
   {
-    all_players_visible = checkIfAllPlayersAreVisible();
+#if 1
+    /* check if all players are still visible with new scrolling position */
+    if (!checkIfAllPlayersAreVisible(screen_x, screen_y))
+    {
+      /* reset horizontal scroll position to last value, if needed */
+      if (!checkIfAllPlayersAreVisible(screen_x, screen_y_old))
+       screen_x = screen_x_old;
+
+      /* reset vertical scroll position to last value, if needed */
+      if (!checkIfAllPlayersAreVisible(screen_x_old, screen_y))
+       screen_y = screen_y_old;
+    }
+#else
+    boolean all_players_visible = checkIfAllPlayersAreVisible();
 
     if (!all_players_visible)
     {
+      printf("::: not all players visible\n");
+
       screen_x = screen_x_old;
       screen_y = screen_y_old;
     }
+#endif
   }
 
   /* prevent scrolling (for screen correcting) if no player is moving */
@@ -831,12 +922,12 @@ void RedrawPlayfield_EM(boolean force_redraw)
     int dx = SIGN(screen_x - screen_x_old);
     int dy = SIGN(screen_y - screen_y_old);
 
-    if ((dx < 0 && player_move_dir == MV_RIGHT) ||
-       (dx > 0 && player_move_dir == MV_LEFT))
+    if ((dx < 0 && player_move_dir != MV_LEFT) ||
+       (dx > 0 && player_move_dir != MV_RIGHT))
       screen_x = screen_x_old;
 
-    if ((dy < 0 && player_move_dir == MV_DOWN) ||
-       (dy > 0 && player_move_dir == MV_UP))
+    if ((dy < 0 && player_move_dir != MV_UP) ||
+       (dy > 0 && player_move_dir != MV_DOWN))
       screen_y = screen_y_old;
   }