added pausing global animations on playfield if game engine is paused
[rocksndiamonds.git] / src / anim.c
index 0e866110cb24cd4a6ec290dabe928f4ee60bb727..060bb60e8f9e7995644742030e8a410cdd5d7205 100644 (file)
@@ -17,6 +17,7 @@
 #include "files.h"
 #include "events.h"
 #include "screens.h"
+#include "tape.h"
 
 
 #define DEBUG_ANIM_DELAY               0
@@ -114,6 +115,8 @@ struct GlobalAnimPartControlInfo
   struct GraphicInfo graphic_info;
   struct GraphicInfo control_info;
 
+  boolean class_playfield;
+
   int viewport_x;
   int viewport_y;
   int viewport_width;
@@ -372,6 +375,27 @@ static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
   return compare_result;
 }
 
+static boolean isPausedOnPlayfield(struct GlobalAnimPartControlInfo *part)
+{
+  // only pause playfield animations when playing
+  if (game_status != GAME_MODE_PLAYING)
+    return FALSE;
+
+  // do not pause animations when game ended (and engine is running)
+  if (checkGameEnded())
+    return FALSE;
+
+  // only pause animations on playfield
+  if (!part->class_playfield)
+    return FALSE;
+
+  // only pause animations when engine is paused or request dialog is open(ing)
+  if (!tape.pausing && !game.request_active_or_moving)
+    return FALSE;
+
+  return TRUE;
+}
+
 static void InitToonControls(void)
 {
   int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
@@ -857,6 +881,10 @@ static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
     }
   }
 
+  // when restarting global animations, do not redraw them, but stop here
+  if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
+    return;
+
   if (global.anim_status == GAME_MODE_LOADING)
     return;
 
@@ -917,6 +945,10 @@ static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
        if (part->drawing_stage != drawing_stage)
          continue;
 
+       // if game is paused, also pause playfield animations
+       if (isPausedOnPlayfield(part))
+         part->initial_anim_sync_frame++;
+
        sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
 
        // re-initialize random animation frame after animation delay
@@ -970,8 +1002,6 @@ void DrawGlobalAnimations(int drawing_target, int drawing_stage)
     ResetGlobalAnim_Clicked();
   }
 
-  DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
-
   if (gfx.cursor_mode_override != last_cursor_mode_override)
     SetMouseCursor(gfx.cursor_mode);
 }
@@ -992,6 +1022,8 @@ static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part
 
   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
 
+  part->class_playfield = FALSE;
+
   if (part->control_info.class == get_hash_from_key("window") ||
       part->control_info.class == get_hash_from_key("border"))
   {
@@ -1053,6 +1085,8 @@ static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part
     viewport_y = REAL_SY;
     viewport_width  = FULL_SXSIZE;
     viewport_height = FULL_SYSIZE;
+
+    part->class_playfield = TRUE;
   }
 
   if (viewport_x != part->viewport_x ||
@@ -1393,7 +1427,7 @@ static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
       if (!(part2->state & ANIM_STATE_RUNNING))
        continue;
 
-      if (isClickablePart(part2, mask))
+      if (isClickablePart(part2, mask) && !part2->triggered)
       {
        struct GraphicInfo *c = &part2->control_info;
 
@@ -1411,6 +1445,10 @@ static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
 
        part2->triggered = TRUE;
 
+       // do not trigger any other animation if CE change event was consumed
+       if (c->style == STYLE_CONSUME_CE_EVENT)
+         return;
+
 #if 0
        Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
              "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
@@ -1459,6 +1497,10 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
   struct GraphicInfo *c = &part->control_info;
   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
 
+  // if game is paused, also pause playfield animations
+  if (isPausedOnPlayfield(part))
+    return state;
+
   if (viewport_changed)
     state |= ANIM_STATE_RESTART;
 
@@ -1592,12 +1634,7 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
       part->init_event_state)
   {
     if (part->initial_anim_sync_frame > 0)
-    {
-      if (part->init_delay_counter > 0)
-       part->initial_anim_sync_frame -= part->init_delay_counter - 1;
-      else
-       part->initial_anim_sync_frame = anim_sync_frame;
-    }
+      part->initial_anim_sync_frame = anim_sync_frame;
 
     part->init_delay_counter = 1;
     part->init_event_state = FALSE;
@@ -1626,9 +1663,13 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
 
       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
-    }
 
-    return ANIM_STATE_WAITING;
+      // continue with state ANIM_STATE_RUNNING (set below)
+    }
+    else
+    {
+      return ANIM_STATE_WAITING;
+    }
   }
 
   if (part->init_event_state)
@@ -1725,12 +1766,17 @@ static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
   if (c->position == POS_CE)
   {
     // calculate playfield position (with scrolling) for related CE tile
+    // (do not use FX/FY, which are incorrect during envelope requests)
+    int FX0 = 2 * TILEX_VAR;   // same as FX during DRAW_TO_FIELDBUFFER
+    int FY0 = 2 * TILEY_VAR;   // same as FY during DRAW_TO_FIELDBUFFER
     int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
     int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
-    int sx = FX + SCREENX(part->tile_x) * TILEX_VAR;
-    int sy = FY + SCREENY(part->tile_y) * TILEY_VAR;
-    int x = sx - fx;
-    int y = sy - fy;
+    int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
+    int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
+    int cx = SX - REAL_SX;
+    int cy = SY - REAL_SY;
+    int x = sx - fx + cx;
+    int y = sy - fy + cy;
 
     part->tile_xoffset += part->step_xoffset;
     part->tile_yoffset += part->step_yoffset;
@@ -2152,11 +2198,16 @@ void RestartGlobalAnimsByStatus(int status)
   global.anim_status = status;
 
   // force restarting global animations by changed global animation status
-  SDLRedrawWindow();
+  DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
 
   global.anim_status = anim_status_last;
 }
 
+void SetAnimStatusBeforeFading(int status)
+{
+  anim_status_last_before_fading = status;
+}
+
 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
 {
   static boolean click_consumed = FALSE;