rnd-20060520-1-src
[rocksndiamonds.git] / src / tools.c
index 1533897305dbb533bbc73b222a6d7acca7c3a92a..8990e7fd28afb210c3697fc305f39491f9fcd0ff 100644 (file)
@@ -414,119 +414,48 @@ void FadeToFront()
   BackToFront();
 }
 
-#define FADE_MODE_FADE_IN      0
-#define FADE_MODE_FADE_OUT     1
-#define FADE_MODE_CROSSFADE    2
-
-static void FadeExt(Bitmap *bitmap_cross, int fade_ms, int fade_mode)
+void FadeIn(int fade_delay)
 {
-  static boolean initialization_needed = TRUE;
-  static SDL_Surface *surface_screen_copy = NULL;
-  static SDL_Surface *surface_black = NULL;
-  SDL_Surface *surface_screen = backbuffer->surface;
-  SDL_Surface *surface_cross;          /* initialized later */
-  boolean fade_reverse;                        /* initialized later */
-  unsigned int time_last, time_current;
-  float alpha;
-  int alpha_final;
-
-  if (initialization_needed)
-  {
-    unsigned int flags = SDL_SRCALPHA;
-
-    /* use same surface type as screen surface */
-    if ((surface_screen->flags & SDL_HWSURFACE))
-      flags |= SDL_HWSURFACE;
-    else
-      flags |= SDL_SWSURFACE;
-
-    /* create surface for temporary copy of screen buffer */
-    if ((surface_screen_copy =
-        SDL_CreateRGBSurface(flags,
-                             surface_screen->w,
-                             surface_screen->h,
-                             surface_screen->format->BitsPerPixel,
-                             surface_screen->format->Rmask,
-                             surface_screen->format->Gmask,
-                             surface_screen->format->Bmask,
-                             surface_screen->format->Amask)) == NULL)
-      Error(ERR_EXIT, "SDL_CreateRGBSurface(   ) failed: %s", SDL_GetError());
-
-    /* create black surface for fading from/to black */
-    if ((surface_black =
-        SDL_CreateRGBSurface(flags,
-                             surface_screen->w,
-                             surface_screen->h,
-                             surface_screen->format->BitsPerPixel,
-                             surface_screen->format->Rmask,
-                             surface_screen->format->Gmask,
-                             surface_screen->format->Bmask,
-                             surface_screen->format->Amask)) == NULL)
-      Error(ERR_EXIT, "SDL_CreateRGBSurface(   ) failed: %s", SDL_GetError());
-
-    /* completely fill the surface with black color pixels */
-    SDL_FillRect(surface_black, NULL,
-                SDL_MapRGB(surface_screen->format, 0, 0, 0));
-
-    initialization_needed = FALSE;
-  }
+  if (fade_delay == 0)
+  {
+    BackToFront();
 
-  /* copy the current screen backbuffer to the temporary screen copy buffer */
-  SDL_BlitSurface(surface_screen, NULL, surface_screen_copy, NULL);
+    return;
+  }
 
-  fade_reverse = (fade_mode == FADE_MODE_FADE_IN ? TRUE : FALSE);
-  surface_cross = (fade_mode == FADE_MODE_CROSSFADE ? bitmap_cross->surface :
-                  surface_black);
+  FadeScreen(NULL, FADE_MODE_FADE_IN, fade_delay, 0);
 
-  time_current = SDL_GetTicks();
+  redraw_mask = REDRAW_NONE;
+}
 
-  for (alpha = 0.0; alpha < 255.0;)
+void FadeOut(int fade_delay, int post_delay)
+{
+  if (fade_delay == 0)
   {
-    time_last = time_current;
-    time_current = SDL_GetTicks();
-    alpha += 255 * ((float)(time_current - time_last) / fade_ms);
-    alpha_final = (int)(fade_reverse ? 255.0 - alpha : alpha);
-    alpha_final = MIN(MAX(0, alpha_final), 255);
-
-    /* draw existing image to screen buffer */
-    SDL_BlitSurface(surface_screen_copy, NULL, surface_screen, NULL);
-
-    /* draw new image to screen buffer using alpha blending */
-    SDL_SetAlpha(surface_cross, SDL_SRCALPHA, alpha_final);
-    SDL_BlitSurface(surface_cross, NULL, surface_screen, NULL);
+    ClearRectangle(backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE);
+    BackToFront();
 
-    /* draw screen buffer to visible display */
-    SDL_Flip(surface_screen);
+    return;
   }
 
+  FadeScreen(NULL, FADE_MODE_FADE_OUT, fade_delay, post_delay);
+
   redraw_mask = REDRAW_NONE;
 }
 
-void FadeIn(int fade_ms)
+void FadeCross(int fade_delay)
 {
-#ifdef TARGET_SDL
-  FadeExt(NULL, fade_ms, FADE_MODE_FADE_IN);
-#else
-  BackToFront();
-#endif
-}
+  if (fade_delay == 0)
+  {
+    BlitBitmap(bitmap_db_title, backbuffer, 0, 0, WIN_XSIZE, WIN_YSIZE, 0, 0);
+    BackToFront();
 
-void FadeOut(int fade_ms)
-{
-#ifdef TARGET_SDL
-  FadeExt(NULL, fade_ms, FADE_MODE_FADE_OUT);
-#else
-  BackToFront();
-#endif
-}
+    return;
+  }
 
-void FadeCross(Bitmap *bitmap, int fade_ms)
-{
-#ifdef TARGET_SDL
-  FadeExt(bitmap, fade_ms, FADE_MODE_CROSSFADE);
-#else
-  BackToFront();
-#endif
+  FadeScreen(bitmap_db_title, FADE_MODE_CROSSFADE, fade_delay, 0);
+
+  redraw_mask = REDRAW_NONE;
 }
 
 void SetMainBackgroundImageIfDefined(int graphic)
@@ -1469,8 +1398,8 @@ void AnimateEnvelope(int envelope_nr, int anim_mode, int action)
   int font_nr = FONT_ENVELOPE_1 + envelope_nr;
   int font_width = getFontWidth(font_nr);
   int font_height = getFontHeight(font_nr);
-  int max_xsize = level.envelope_xsize[envelope_nr];
-  int max_ysize = level.envelope_ysize[envelope_nr];
+  int max_xsize = level.envelope[envelope_nr].xsize;
+  int max_ysize = level.envelope[envelope_nr].ysize;
   int xstart = (anim_mode & ANIM_VERTICAL ? max_xsize : 0);
   int ystart = (anim_mode & ANIM_HORIZONTAL ? max_ysize : 0);
   int xend = max_xsize;
@@ -1497,7 +1426,7 @@ void AnimateEnvelope(int envelope_nr, int anim_mode, int action)
       DrawEnvelopeBackground(envelope_nr, sx,sy, xx,yy, xsize, ysize, font_nr);
 
     DrawTextToTextArea(SX + sx + font_width, SY + sy + font_height,
-                      level.envelope_text[envelope_nr], font_nr, max_xsize,
+                      level.envelope[envelope_nr].text, font_nr, max_xsize,
                       xsize - 2, ysize - 2, mask_mode);
 
     redraw_mask |= REDRAW_FIELD | REDRAW_FROM_BACKBUFFER;
@@ -1523,7 +1452,7 @@ void ShowEnvelope(int envelope_nr)
 
   game.envelope_active = TRUE; /* needed for RedrawPlayfield() events */
 
-  PlaySoundStereo(sound_opening, SOUND_MIDDLE);
+  PlayMenuSoundStereo(sound_opening, SOUND_MIDDLE);
 
   if (anim_mode == ANIM_DEFAULT)
     AnimateEnvelope(envelope_nr, ANIM_DEFAULT, ACTION_OPENING);
@@ -1535,7 +1464,7 @@ void ShowEnvelope(int envelope_nr)
   else
     WaitForEventToContinue();
 
-  PlaySoundStereo(sound_closing, SOUND_MIDDLE);
+  PlayMenuSoundStereo(sound_closing, SOUND_MIDDLE);
 
   if (anim_mode != ANIM_NONE)
     AnimateEnvelope(envelope_nr, main_anim_mode, ACTION_CLOSING);
@@ -2641,14 +2570,17 @@ unsigned int MoveDoor(unsigned int door_state)
     return (door1 | door2);
   }
 
-  if (door1 == DOOR_OPEN_1 && door_state & DOOR_OPEN_1)
-    door_state &= ~DOOR_OPEN_1;
-  else if (door1 == DOOR_CLOSE_1 && door_state & DOOR_CLOSE_1)
-    door_state &= ~DOOR_CLOSE_1;
-  if (door2 == DOOR_OPEN_2 && door_state & DOOR_OPEN_2)
-    door_state &= ~DOOR_OPEN_2;
-  else if (door2 == DOOR_CLOSE_2 && door_state & DOOR_CLOSE_2)
-    door_state &= ~DOOR_CLOSE_2;
+  if (!(door_state & DOOR_FORCE_REDRAW))
+  {
+    if (door1 == DOOR_OPEN_1 && door_state & DOOR_OPEN_1)
+      door_state &= ~DOOR_OPEN_1;
+    else if (door1 == DOOR_CLOSE_1 && door_state & DOOR_CLOSE_1)
+      door_state &= ~DOOR_CLOSE_1;
+    if (door2 == DOOR_OPEN_2 && door_state & DOOR_OPEN_2)
+      door_state &= ~DOOR_OPEN_2;
+    else if (door2 == DOOR_CLOSE_2 && door_state & DOOR_CLOSE_2)
+      door_state &= ~DOOR_CLOSE_2;
+  }
 
   door_delay_value = (door_state & DOOR_ACTION_1 ? door_1.step_delay :
                      door_2.step_delay);
@@ -2683,8 +2615,8 @@ unsigned int MoveDoor(unsigned int door_state)
 #if 1
     int end = door_size;
 #else
-    int end = (door_state & DOOR_ACTION_1 &&
-              door_1.anim_mode & ANIM_VERTICAL ? DYSIZE : DXSIZE);
+    int end = (door_state & DOOR_ACTION_1 && door_1.anim_mode & ANIM_VERTICAL ?
+              DYSIZE : DXSIZE);
 #endif
 #if 1
     int start = ((door_state & DOOR_NO_DELAY) ? end : 0);
@@ -2697,9 +2629,9 @@ unsigned int MoveDoor(unsigned int door_state)
     {
       /* opening door sound has priority over simultaneously closing door */
       if (door_state & (DOOR_OPEN_1 | DOOR_OPEN_2))
-       PlaySoundStereo(SND_DOOR_OPENING, SOUND_MIDDLE);
+       PlayMenuSoundStereo(SND_DOOR_OPENING, SOUND_MIDDLE);
       else if (door_state & (DOOR_CLOSE_1 | DOOR_CLOSE_2))
-       PlaySoundStereo(SND_DOOR_CLOSING, SOUND_MIDDLE);
+       PlayMenuSoundStereo(SND_DOOR_CLOSING, SOUND_MIDDLE);
     }
 
     for (k = start; k <= end && !(door_1_done && door_2_done); k += stepsize)
@@ -2802,9 +2734,15 @@ unsigned int MoveDoor(unsigned int door_state)
 
       if (door_state & DOOR_ACTION_2)
       {
+#if 1
+       int a = MIN(x * door_2.step_offset, door_size);
+       int p = (door_state & DOOR_OPEN_2 ? door_size - a : a);
+       int i = p + door_skip;
+#else
        int a = MIN(x * door_2.step_offset, door_size_2);
        int p = (door_state & DOOR_OPEN_2 ? door_size_2 - a : a);
        int i = p + door_skip;
+#endif
 
        if (door_2.anim_mode & ANIM_STATIC_PANEL)
        {
@@ -5982,3 +5920,60 @@ void InitGraphicInfo_EM(void)
   exit(0);
 #endif
 }
+
+void PlayMenuSound()
+{
+  int sound = menu.sound[game_status];
+
+  if (sound == SND_UNDEFINED)
+    return;
+
+  if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
+      (!setup.sound_loops && IS_LOOP_SOUND(sound)))
+    return;
+
+  if (IS_LOOP_SOUND(sound))
+    PlaySoundLoop(sound);
+  else
+    PlaySound(sound);
+}
+
+void PlayMenuSoundStereo(int sound, int stereo_position)
+{
+  if (sound == SND_UNDEFINED)
+    return;
+
+  if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
+      (!setup.sound_loops && IS_LOOP_SOUND(sound)))
+    return;
+
+  if (IS_LOOP_SOUND(sound))
+    PlaySoundExt(sound, SOUND_MAX_VOLUME, stereo_position, SND_CTRL_PLAY_LOOP);
+  else
+    PlaySoundStereo(sound, stereo_position);
+}
+
+void PlayMenuSoundIfLoop()
+{
+  int sound = menu.sound[game_status];
+
+  if (sound == SND_UNDEFINED)
+    return;
+
+  if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
+      (!setup.sound_loops && IS_LOOP_SOUND(sound)))
+    return;
+
+  if (IS_LOOP_SOUND(sound))
+    PlaySoundLoop(sound);
+}
+
+void PlayMenuMusic()
+{
+  int music = menu.music[game_status];
+
+  if (music == MUS_UNDEFINED)
+    return;
+
+  PlayMusic(music);
+}