added support for separate animations for 'old' (entered from main menu) and 'new...
[rocksndiamonds.git] / src / tools.c
index 86ad19b75e8c034d25eeb401a1efacc9d2a37511..f907d5dd2bf39816a2bc8ef2a8bbb78eef83147f 100644 (file)
@@ -192,6 +192,22 @@ static char *print_if_not_empty(int element)
   return s;
 }
 
+int correctLevelPosX_EM(int lx)
+{
+  lx -= 1;
+  lx -= (BorderElement != EL_EMPTY ? 1 : 0);
+
+  return lx;
+}
+
+int correctLevelPosY_EM(int ly)
+{
+  ly -= 1;
+  ly -= (BorderElement != EL_EMPTY ? 1 : 0);
+
+  return ly;
+}
+
 static int getFieldbufferOffsetX_RND()
 {
   int full_lev_fieldx = lev_fieldx + (BorderElement != EL_EMPTY ? 2 : 0);
@@ -288,8 +304,7 @@ static int getLevelFromScreenX_EM(int sx)
   int px = sx - SX;
   int lx = LEVELX((px + dx) / TILESIZE_VAR);
 
-  lx -= 1;
-  lx -= (BorderElement != EL_EMPTY ? 1 : 0);
+  lx = correctLevelPosX_EM(lx);
 
   return lx;
 }
@@ -306,8 +321,7 @@ static int getLevelFromScreenY_EM(int sy)
   int py = sy - SY;
   int ly = LEVELY((py + dy) / TILESIZE_VAR);
 
-  ly -= 1;
-  ly -= (BorderElement != EL_EMPTY ? 1 : 0);
+  ly = correctLevelPosY_EM(ly);
 
   return ly;
 }
@@ -395,6 +409,7 @@ void DumpTile(int x, int y)
   printf("  GfxElement:  %d\n", GfxElement[x][y]);
   printf("  GfxAction:   %d\n", GfxAction[x][y]);
   printf("  GfxFrame:    %d [%d]\n", GfxFrame[x][y], FrameCounter);
+  printf("  Player x/y:  %d, %d\n", local_player->jx, local_player->jy);
   printf("\n");
 }
 
@@ -744,6 +759,13 @@ void BackToFront()
       y2 = MAX(y2, EY + EYSIZE);
     }
 
+    // make sure that at least one pixel is blitted, and inside the screen
+    // (else nothing is blitted, causing the animations not to be updated)
+    x1 = MIN(MAX(0, x1), WIN_XSIZE - 1);
+    y1 = MIN(MAX(0, y1), WIN_YSIZE - 1);
+    x2 = MIN(MAX(1, x2), WIN_XSIZE);
+    y2 = MIN(MAX(1, y2), WIN_YSIZE);
+
     BlitBitmap(backbuffer, window, x1, y1, x2 - x1, y2 - y1, x1, y1);
   }
 
@@ -911,6 +933,10 @@ 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))
+    SetOverlayActive(TRUE);
+
   SetScreenStates_AfterFadingIn();
 
   // force update of global animation status in case of rapid screen changes
@@ -926,6 +952,8 @@ void FadeOut(int fade_mask)
 
   SetScreenStates_BeforeFadingOut();
 
+  SetOverlayActive(FALSE);
+
 #if 0
   DrawMaskedBorder(REDRAW_ALL);
 #endif
@@ -2307,6 +2335,10 @@ void DrawScreenField(int x, int y)
       int newly = ly + (dir == MV_UP   ? -1 : dir == MV_DOWN  ? +1 : 0);
 
       DrawLevelElementThruMask(newlx, newly, EL_ACID);
+
+      // prevent target field from being drawn again (but without masking)
+      // (this would happen if target field is scanned after moving element)
+      Stop[newlx][newly] = TRUE;
     }
   }
   else if (IS_BLOCKED(lx, ly))
@@ -3704,6 +3736,9 @@ void WaitForEventToContinue()
 {
   boolean still_wait = TRUE;
 
+  if (program.headless)
+    return;
+
   /* simulate releasing mouse button over last gadget, if still pressed */
   if (button_status)
     HandleGadgets(-1, -1, 0);
@@ -3851,6 +3886,19 @@ static int RequestHandleEvents(unsigned int req_state)
            break;
          }
 
+#if defined(TARGET_SDL2)
+      case SDL_WINDOWEVENT:
+       HandleWindowEvent((WindowEvent *) &event);
+       break;
+
+      case SDL_APP_WILLENTERBACKGROUND:
+      case SDL_APP_DIDENTERBACKGROUND:
+      case SDL_APP_WILLENTERFOREGROUND:
+      case SDL_APP_DIDENTERFOREGROUND:
+       HandlePauseResumeEvent((PauseResumeEvent *) &event);
+       break;
+#endif
+
          case EVENT_KEYPRESS:
          {
            Key key = GetEventKey((KeyEvent *)&event, TRUE);
@@ -4192,10 +4240,19 @@ static boolean RequestEnvelope(char *text, unsigned int req_state)
 
 boolean Request(char *text, unsigned int req_state)
 {
+  boolean overlay_active = GetOverlayActive();
+  boolean result;
+
+  SetOverlayActive(FALSE);
+
   if (global.use_envelope_request)
-    return RequestEnvelope(text, req_state);
+    result = RequestEnvelope(text, req_state);
   else
-    return RequestDoor(text, req_state);
+    result = RequestDoor(text, req_state);
+
+  SetOverlayActive(overlay_active);
+
+  return result;
 }
 
 static int compareDoorPartOrderInfo(const void *object1, const void *object2)
@@ -8364,7 +8421,37 @@ void PlayMenuMusicExt(int music)
 
 void PlayMenuMusic()
 {
-  PlayMenuMusicExt(menu.music[game_status]);
+  char *curr_music = getCurrentlyPlayingMusicFilename();
+  char *next_music = getMusicListEntry(menu.music[game_status])->filename;
+
+  if (!strEqual(curr_music, next_music))
+    PlayMenuMusicExt(menu.music[game_status]);
+}
+
+void PlayMenuSoundsAndMusic()
+{
+  PlayMenuSound();
+  PlayMenuMusic();
+}
+
+static void FadeMenuSounds()
+{
+  FadeSounds();
+}
+
+static void FadeMenuMusic()
+{
+  char *curr_music = getCurrentlyPlayingMusicFilename();
+  char *next_music = getMusicListEntry(menu.music[game_status])->filename;
+
+  if (!strEqual(curr_music, next_music))
+    FadeMusic();
+}
+
+void FadeMenuSoundsAndMusic()
+{
+  FadeMenuSounds();
+  FadeMenuMusic();
 }
 
 void PlaySoundActivating()
@@ -8460,6 +8547,8 @@ void SetAnimStatus(int anim_status_new)
 {
   if (anim_status_new == GAME_MODE_MAIN)
     anim_status_new = GAME_MODE_PSEUDO_MAINONLY;
+  else if (anim_status_new == GAME_MODE_SCORES)
+    anim_status_new = GAME_MODE_PSEUDO_SCORESOLD;
 
   global.anim_status_next = anim_status_new;
 
@@ -8473,6 +8562,9 @@ void SetAnimStatus(int anim_status_new)
 
 void SetGameStatus(int game_status_new)
 {
+  if (game_status_new != game_status)
+    game_status_last_screen = game_status;
+
   game_status = game_status_new;
 
   SetAnimStatus(game_status_new);