removed forcing next screen redraw after handling global animations
[rocksndiamonds.git] / src / anim.c
index fd6898eadf3fdce4ed53d13df18f7c72ad64e43b..b9ea8becc2a44d2b84ea4e32e8424a95e96a7e87 100644 (file)
 #define ANIM_STATE_RUNNING             (1 << 2)
 
 /* values for global animation control */
-#define ANIM_START                     0
-#define ANIM_CONTINUE                  1
-#define ANIM_STOP                      2
+#define ANIM_NO_ACTION                 0
+#define ANIM_START                     1
+#define ANIM_CONTINUE                  2
+#define ANIM_STOP                      3
 
 
 struct GlobalAnimPartControlInfo
@@ -68,6 +69,7 @@ struct GlobalAnimPartControlInfo
   int mode_nr;
 
   int sound;
+  int music;
   int graphic;
 
   struct GraphicInfo graphic_info;
@@ -182,6 +184,8 @@ static int anim_class_game_modes[NUM_ANIM_CLASSES];
 static int anim_status_last = GAME_MODE_DEFAULT;
 static int anim_classes_last = ANIM_CLASS_NONE;
 
+static boolean drawing_to_fading_buffer = FALSE;
+
 
 /* ========================================================================= */
 /* generic animation frame calculation                                       */
@@ -329,6 +333,7 @@ static void InitToonControls()
   {
     struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
     int sound = SND_UNDEFINED;
+    int music = MUS_UNDEFINED;
     int graphic = IMG_TOON_1 + i;
     int control = graphic;
 
@@ -336,6 +341,7 @@ static void InitToonControls()
     part->anim_nr = anim_nr;
     part->mode_nr = mode_nr;
     part->sound = sound;
+    part->music = music;
     part->graphic = graphic;
     part->graphic_info = graphic_info[graphic];
     part->control_info = graphic_info[control];
@@ -367,7 +373,7 @@ void InitGlobalAnimControls()
 {
   int i, m, a, p;
   int mode_nr, anim_nr, part_nr;
-  int sound, graphic, control;
+  int sound, music, graphic, control;
 
   anim_sync_frame = 0;
 
@@ -414,6 +420,7 @@ void InitGlobalAnimControls()
        struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
 
        sound   = global_anim_info[a].sound[p][m];
+       music   = global_anim_info[a].music[p][m];
        graphic = global_anim_info[a].graphic[p][m];
        control = global_anim_info[ctrl_id].graphic[p][m];
 
@@ -435,6 +442,7 @@ void InitGlobalAnimControls()
        part->anim_nr = anim_nr;
        part->mode_nr = mode_nr;
        part->sound = sound;
+       part->music = music;
        part->graphic = graphic;
        part->graphic_info = graphic_info[graphic];
        part->control_info = graphic_info[control];
@@ -511,10 +519,25 @@ void InitGlobalAnimations()
   InitGlobalAnimControls();
 }
 
-void DrawGlobalAnimationsExt(int drawing_stage)
+void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
 {
+  Bitmap *fade_bitmap =
+    (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
+     drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
+  int game_mode_anim_action[NUM_GAME_MODES];
   int mode_nr;
 
+  if (!setup.toons)
+    return;
+
+  if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
+      drawing_target == DRAW_TO_SCREEN)
+    DoAnimationExt();
+
+  // always start with reliable default values (no animation actions)
+  for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
+    game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
+
   if (global.anim_status != anim_status_last)
   {
     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
@@ -522,19 +545,22 @@ void DrawGlobalAnimationsExt(int drawing_stage)
     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
     int i;
 
+    if (drawing_target == DRAW_TO_FADE_TARGET)
+      after_fading = TRUE;
+
     // ---------- part 1 ------------------------------------------------------
     // start or stop global animations by change of game mode
     // (special handling of animations for "current screen" and "all screens")
 
     // stop animations for last screen
-    HandleGlobalAnim(ANIM_STOP, anim_status_last);
+    game_mode_anim_action[anim_status_last] = ANIM_STOP;
 
     // start animations for current screen
-    HandleGlobalAnim(ANIM_START, global.anim_status);
+    game_mode_anim_action[global.anim_status] = ANIM_START;
 
     // start animations for all screens after loading new artwork set
     if (anim_status_last == GAME_MODE_LOADING)
-      HandleGlobalAnim(ANIM_START, GAME_MODE_DEFAULT);
+      game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
 
     // ---------- part 2 ------------------------------------------------------
     // start or stop global animations by change of animation class
@@ -549,30 +575,54 @@ void DrawGlobalAnimationsExt(int drawing_stage)
 
       // stop animations for changed screen class before fading to new screen
       if (before_fading && anim_class_last && !anim_class_next)
-       HandleGlobalAnim(ANIM_STOP, anim_class_game_mode);
+       game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
 
       // start animations for changed screen class after fading to new screen
       if (after_fading && !anim_class_last && anim_class_next)
-       HandleGlobalAnim(ANIM_START, anim_class_game_mode);
+       game_mode_anim_action[anim_class_game_mode] = ANIM_START;
     }
 
-    if (after_fading)
-      anim_classes_last = anim_classes_next;
+    if (drawing_target == DRAW_TO_SCREEN)
+    {
+      if (after_fading)
+       anim_classes_last = anim_classes_next;
+
+      anim_status_last = global.anim_status;
 
-    anim_status_last = global.anim_status;
+      // start or stop animations determined to be started or stopped above
+      for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
+       if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
+         HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
+    }
+    else if (drawing_target == DRAW_TO_FADE_TARGET)
+    {
+      drawing_to_fading_buffer = TRUE;
+
+      // start animations determined to be (temporary) started above
+      for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
+       if (game_mode_anim_action[mode_nr] == ANIM_START)
+         HandleGlobalAnim(ANIM_START, mode_nr);
+    }
   }
 
-  if (!setup.toons || global.anim_status == GAME_MODE_LOADING)
+  if (global.anim_status == GAME_MODE_LOADING)
     return;
 
-  if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
-    DoAnimationExt();
-
   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
   {
     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
     int anim_nr;
 
+    // when preparing source fading buffer, only draw animations to be stopped
+    if (drawing_target == DRAW_TO_FADE_SOURCE &&
+       game_mode_anim_action[mode_nr] != ANIM_STOP)
+      continue;
+
+    // when preparing target fading buffer, only draw animations to be started
+    if (drawing_target == DRAW_TO_FADE_TARGET &&
+       game_mode_anim_action[mode_nr] != ANIM_START)
+      continue;
+
 #if 0
     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
        mode_nr != game_status)
@@ -655,16 +705,30 @@ void DrawGlobalAnimationsExt(int drawing_stage)
        src_x += cut_x;
        src_y += cut_y;
 
-       BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
+       if (drawing_target == DRAW_TO_SCREEN)
+         BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
+                            dst_x, dst_y);
+       else
+         BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
                           dst_x, dst_y);
       }
     }
   }
+
+  if (drawing_target == DRAW_TO_FADE_TARGET)
+  {
+    // stop animations determined to be (temporary) started above
+    for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
+      if (game_mode_anim_action[mode_nr] == ANIM_START)
+       HandleGlobalAnim(ANIM_STOP, mode_nr);
+
+    drawing_to_fading_buffer = FALSE;
+  }
 }
 
-void DrawGlobalAnimations(int drawing_stage)
+void DrawGlobalAnimations(int drawing_target, int drawing_stage)
 {
-  DrawGlobalAnimationsExt(drawing_stage);
+  DrawGlobalAnimationsExt(drawing_target, drawing_stage);
 }
 
 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
@@ -730,7 +794,7 @@ boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
   return changed;
 }
 
-void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
+static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
 {
   int sound = part->sound;
 
@@ -748,12 +812,12 @@ void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
     PlaySound(sound);
 
 #if 0
-  printf("::: PLAY %d.%d.%d: %d\n",
+  printf("::: PLAY SOUND %d.%d.%d: %d\n",
         part->anim_nr, part->nr, part->mode_nr, sound);
 #endif
 }
 
-void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
+static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
 {
   int sound = part->sound;
 
@@ -763,11 +827,60 @@ void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
   StopSound(sound);
 
 #if 0
-  printf("::: STOP %d.%d.%d: %d\n",
+  printf("::: STOP SOUND %d.%d.%d: %d\n",
         part->anim_nr, part->nr, part->mode_nr, sound);
 #endif
 }
 
+static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
+{
+  int music = part->music;
+
+  if (music == MUS_UNDEFINED)
+    return;
+
+  if (!setup.sound_music)
+    return;
+
+  PlayMusic(music);
+
+#if 0
+  printf("::: PLAY MUSIC %d.%d.%d: %d\n",
+        part->anim_nr, part->nr, part->mode_nr, music);
+#endif
+}
+
+static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
+{
+  int music = part->music;
+
+  if (music == MUS_UNDEFINED)
+    return;
+
+  StopMusic();
+
+#if 0
+  printf("::: STOP MUSIC %d.%d.%d: %d\n",
+        part->anim_nr, part->nr, part->mode_nr, music);
+#endif
+}
+
+static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
+{
+  // when drawing animations to fading buffer, do not play sounds or music
+  if (drawing_to_fading_buffer)
+    return;
+
+  PlayGlobalAnimSound(part);
+  PlayGlobalAnimMusic(part);
+}
+
+static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
+{
+  StopGlobalAnimSound(part);
+  StopGlobalAnimMusic(part);
+}
+
 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
 {
   struct GraphicInfo *g = &part->graphic_info;
@@ -779,6 +892,11 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
 
   if (state & ANIM_STATE_RESTART)
   {
+    // when drawing animations to fading buffer, only start fixed animations
+    if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
+                                    c->y == ARG_UNDEFINED_VALUE))
+      return ANIM_STATE_INACTIVE;
+
     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
 
     part->init_delay_counter =
@@ -864,7 +982,7 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
       part->step_yoffset = c->step_yoffset;
 
     if (part->init_delay_counter == 0)
-      PlayGlobalAnimSound(part);
+      PlayGlobalAnimSoundAndMusic(part);
   }
 
   if (part->init_delay_counter > 0)
@@ -872,7 +990,7 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
     part->init_delay_counter--;
 
     if (part->init_delay_counter == 0)
-      PlayGlobalAnimSound(part);
+      PlayGlobalAnimSoundAndMusic(part);
 
     return ANIM_STATE_WAITING;
   }
@@ -887,7 +1005,7 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
     if (part->anim_delay_counter == 0 &&
        part->post_delay_counter == 0)
     {
-      StopGlobalAnimSound(part);
+      StopGlobalAnimSoundAndMusic(part);
 
       part->post_delay_counter =
        (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
@@ -906,7 +1024,7 @@ int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
 
     if (part->anim_delay_counter == 0)
     {
-      StopGlobalAnimSound(part);
+      StopGlobalAnimSoundAndMusic(part);
 
       part->post_delay_counter =
        (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
@@ -1001,7 +1119,7 @@ void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
        int i;
 
        for (i = 0; i < num_parts; i++)
-         StopGlobalAnimSound(&anim->part[i]);
+         StopGlobalAnimSoundAndMusic(&anim->part[i]);
       }
 
       return;
@@ -1140,7 +1258,7 @@ static void DoAnimationExt()
   for (i = 0; i < NUM_GAME_MODES; i++)
     HandleGlobalAnim(ANIM_CONTINUE, i);
 
-#if 1
+#if 0
   // force screen redraw in next frame to continue drawing global animations
   redraw_mask = REDRAW_ALL;
 #endif