renamed function 'NextEvent' to 'WaitEvent'
[rocksndiamonds.git] / src / tools.c
index 2ca18cf6b5f773e117aa6e0ebbfaf0fc010479f3..9bde54c7531aff5b7a1dc16d0e083808701f40d3 100644 (file)
@@ -622,11 +622,24 @@ void DrawFramesPerSecond()
   char text[100];
   int font_nr = FONT_TEXT_2;
   int font_width = getFontWidth(font_nr);
+  int draw_deactivation_mask = GetDrawDeactivationMask();
+  boolean draw_masked = (draw_deactivation_mask == REDRAW_NONE);
 
-  sprintf(text, "%04.1f fps", global.frames_per_second);
+  /* draw FPS with leading space (needed if field buffer deactivated) */
+  sprintf(text, " %04.1f fps", global.frames_per_second);
 
-  DrawTextExt(backbuffer, WIN_XSIZE - font_width * strlen(text), 0, text,
-             font_nr, BLIT_OPAQUE);
+  /* override draw deactivation mask (required for invisible warp mode) */
+  SetDrawDeactivationMask(REDRAW_NONE);
+
+  /* draw opaque FPS if field buffer deactivated, else draw masked FPS */
+  DrawTextExt(backbuffer, SX + SXSIZE - font_width * strlen(text), SY, text,
+             font_nr, (draw_masked ? BLIT_MASKED : BLIT_OPAQUE));
+
+  /* set draw deactivation mask to previous value */
+  SetDrawDeactivationMask(draw_deactivation_mask);
+
+  /* force full-screen redraw in this frame */
+  redraw_mask = REDRAW_ALL;
 }
 
 #if DEBUG_FRAME_TIME
@@ -759,6 +772,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);
   }
 
@@ -926,6 +946,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
@@ -941,6 +965,8 @@ void FadeOut(int fade_mask)
 
   SetScreenStates_BeforeFadingOut();
 
+  SetOverlayActive(FALSE);
+
 #if 0
   DrawMaskedBorder(REDRAW_ALL);
 #endif
@@ -2322,6 +2348,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))
@@ -2951,7 +2981,7 @@ void DrawMiniLevel(int size_x, int size_y, int scroll_x, int scroll_y)
   redraw_mask |= REDRAW_FIELD;
 }
 
-static void DrawPreviewLevelPlayfieldExt(int from_x, int from_y)
+static void DrawPreviewLevelPlayfield(int from_x, int from_y)
 {
   boolean show_level_border = (BorderElement != EL_EMPTY);
   int level_xsize = lev_fieldx + (show_level_border ? 2 : 0);
@@ -3016,9 +3046,8 @@ static int getMaxTextLength(struct TextPosInfo *pos, int font_nr)
   return max_text_width / font_width;
 }
 
-static void DrawPreviewLevelLabelExt(int mode)
+static void DrawPreviewLevelLabelExt(int mode, struct TextPosInfo *pos)
 {
-  struct TextPosInfo *pos = &menu.main.text.level_info_2;
   char label_text[MAX_OUTPUT_LINESIZE + 1];
   int max_len_label_text;
   int font_nr = pos->font;
@@ -3061,6 +3090,19 @@ static void DrawPreviewLevelLabelExt(int mode)
   redraw_mask |= REDRAW_FIELD;
 }
 
+static void DrawPreviewLevelLabel(int mode)
+{
+  DrawPreviewLevelLabelExt(mode, &menu.main.text.level_info_2);
+}
+
+static void DrawPreviewLevelInfo(int mode)
+{
+  if (mode == MICROLABEL_LEVEL_NAME)
+    DrawPreviewLevelLabelExt(mode, &menu.main.text.level_name);
+  else if (mode == MICROLABEL_LEVEL_AUTHOR)
+    DrawPreviewLevelLabelExt(mode, &menu.main.text.level_author);
+}
+
 static void DrawPreviewLevelExt(boolean restart)
 {
   static unsigned int scroll_delay = 0;
@@ -3092,8 +3134,11 @@ static void DrawPreviewLevelExt(boolean restart)
     label_state = 1;
     label_counter = 0;
 
-    DrawPreviewLevelPlayfieldExt(from_x, from_y);
-    DrawPreviewLevelLabelExt(label_state);
+    DrawPreviewLevelPlayfield(from_x, from_y);
+    DrawPreviewLevelLabel(label_state);
+
+    DrawPreviewLevelInfo(MICROLABEL_LEVEL_NAME);
+    DrawPreviewLevelInfo(MICROLABEL_LEVEL_AUTHOR);
 
     /* initialize delay counters */
     DelayReached(&scroll_delay, 0);
@@ -3172,7 +3217,7 @@ static void DrawPreviewLevelExt(boolean restart)
        break;
     }
 
-    DrawPreviewLevelPlayfieldExt(from_x, from_y);
+    DrawPreviewLevelPlayfield(from_x, from_y);
   }
 
   /* !!! THIS ALL SUCKS -- SHOULD BE CLEANLY REWRITTEN !!! */
@@ -3213,7 +3258,7 @@ static void DrawPreviewLevelExt(boolean restart)
       label_state = (label_state == MICROLABEL_IMPORTED_FROM_HEAD ?
                     MICROLABEL_IMPORTED_BY_HEAD : MICROLABEL_IMPORTED_BY);
 
-    DrawPreviewLevelLabelExt(label_state);
+    DrawPreviewLevelLabel(label_state);
   }
 }
 
@@ -3719,6 +3764,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);
@@ -3733,12 +3781,16 @@ void WaitForEventToContinue()
     {
       Event event;
 
-      NextEvent(&event);
+      WaitEvent(&event);
 
       switch (event.type)
       {
        case EVENT_BUTTONPRESS:
        case EVENT_KEYPRESS:
+#if defined(TARGET_SDL2)
+        case SDL_CONTROLLERBUTTONDOWN:
+#endif
+        case SDL_JOYBUTTONDOWN:
          still_wait = FALSE;
          break;
 
@@ -3866,6 +3918,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);
@@ -3879,7 +3944,11 @@ static int RequestHandleEvents(unsigned int req_state)
 
              case KSYM_Return:
 #if defined(TARGET_SDL2)
+             case KSYM_Select:
              case KSYM_Menu:
+#if defined(KSYM_Rewind)
+             case KSYM_Rewind:         /* for Amazon Fire TV remote */
+#endif
 #endif
                result = 1;
                break;
@@ -3887,6 +3956,9 @@ static int RequestHandleEvents(unsigned int req_state)
              case KSYM_Escape:
 #if defined(TARGET_SDL2)
              case KSYM_Back:
+#if defined(KSYM_FastForward)
+             case KSYM_FastForward:    /* for Amazon Fire TV remote */
+#endif
 #endif
                result = 0;
                break;
@@ -3906,6 +3978,35 @@ static int RequestHandleEvents(unsigned int req_state)
            ClearPlayerAction();
            break;
 
+#if defined(TARGET_SDL2)
+         case SDL_CONTROLLERBUTTONDOWN:
+           switch (event.cbutton.button)
+           {
+             case SDL_CONTROLLER_BUTTON_A:
+             case SDL_CONTROLLER_BUTTON_X:
+             case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
+               result = 1;
+               break;
+
+             case SDL_CONTROLLER_BUTTON_B:
+             case SDL_CONTROLLER_BUTTON_Y:
+             case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
+             case SDL_CONTROLLER_BUTTON_BACK:
+               result = 0;
+               break;
+           }
+
+           if (req_state & REQ_PLAYER)
+             result = 0;
+
+           break;
+
+         case SDL_CONTROLLERBUTTONUP:
+           HandleJoystickEvent(&event);
+           ClearPlayerAction();
+           break;
+#endif
+
          default:
            HandleOtherEvents(&event);
            break;
@@ -4207,10 +4308,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)
@@ -4771,6 +4881,9 @@ unsigned int MoveDoor(unsigned int door_state)
        SkipUntilDelayReached(&door_delay, door_delay_value, &k, last_frame);
 
        current_move_delay += max_step_delay;
+
+       /* prevent OS (Windows) from complaining about program not responding */
+       CheckQuitEvent();
       }
 
       if (door_part_done_all)
@@ -8288,8 +8401,15 @@ void CheckSingleStepMode_EM(byte action[MAX_PLAYERS], int frame,
 void CheckSingleStepMode_SP(boolean murphy_is_waiting,
                            boolean murphy_is_dropping)
 {
+  boolean murphy_starts_dropping = FALSE;
+  int i;
+
+  for (i = 0; i < MAX_PLAYERS; i++)
+    if (stored_player[i].force_dropping)
+      murphy_starts_dropping = TRUE;
+
   if (tape.single_step && tape.recording && !tape.pausing)
-    if (murphy_is_waiting)
+    if (murphy_is_waiting && !murphy_starts_dropping)
       TapeTogglePause(TAPE_TOGGLE_AUTOMATIC);
 
   CheckSaveEngineSnapshot_SP(murphy_is_waiting, murphy_is_dropping);
@@ -8379,7 +8499,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()
@@ -8475,6 +8625,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;
 
@@ -8488,6 +8640,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);