Browse Source

SDL_GetTicks() ralted fix, avoiding possible bugs on extreme cases when SDL_GetTicks() wrapped, and remove some redundant codes on SDL 2.0 drawing.

louyihua 8 years ago
parent
commit
5fa74edd95
15 changed files with 77 additions and 180 deletions
  1. 2 12
      battle.c
  2. 8 0
      common.h
  3. 8 43
      fight.c
  4. 1 6
      game.c
  5. 6 3
      input.c
  6. 1 1
      itemmenu.c
  7. 1 1
      magicmenu.c
  8. 3 3
      palette.c
  9. 1 1
      play.c
  10. 1 6
      rngplay.c
  11. 4 24
      script.c
  12. 2 2
      uibattle.c
  13. 2 2
      uigame.c
  14. 1 1
      util.c
  15. 36 75
      video.c

+ 2 - 12
battle.c

@@ -247,12 +247,7 @@ PAL_BattleFadeScene(
    {
       for (j = 0; j < 6; j++)
       {
-         PAL_ProcessEvent();
-         while (SDL_GetTicks() <= time)
-         {
-            PAL_ProcessEvent();
-            SDL_Delay(1);
-         }
+         PAL_DelayUntil(time);
          time = SDL_GetTicks() + 16;
 
          //
@@ -396,12 +391,7 @@ PAL_BattleMain(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.

+ 8 - 0
common.h

@@ -343,6 +343,14 @@ typedef const WCHAR        *LPCWSTR;
 #define __WIDETEXT(quote) L##quote
 #define WIDETEXT(quote) __WIDETEXT(quote)
 
+#define PAL_DelayUntil(t) \
+   PAL_ProcessEvent(); \
+   while (!SDL_TICKS_PASSED(SDL_GetTicks(), (t))) \
+   { \
+      PAL_ProcessEvent(); \
+      SDL_Delay(1); \
+   }
+
 typedef enum tagCODEPAGE {
 	CP_UNKNOWN = -1,
 	CP_MIN = 0,

+ 8 - 43
fight.c

@@ -408,7 +408,7 @@ PAL_GetTimeChargingSpeed(
 {
    if ((g_Battle.UI.state == kBattleUISelectMove &&
       g_Battle.UI.MenuState != kBattleMenuMain) ||
-      SDL_GetTicks() < g_Battle.UI.dwMsgShowTime)
+      !SDL_TICKS_PASSED(SDL_GetTicks(), g_Battle.UI.dwMsgShowTime))
    {
       //
       // Pause the time when there are submenus or text messages
@@ -490,12 +490,7 @@ PAL_BattleDelay(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.
@@ -2003,12 +1998,7 @@ PAL_BattleShowPlayerAttackAnim(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.
@@ -2255,12 +2245,7 @@ PAL_BattleShowPlayerPreMagicAnim(
          //
          // Wait for the time of one frame. Accept input here.
          //
-         PAL_ProcessEvent();
-         while (SDL_GetTicks() <= dwTime)
-         {
-            PAL_ProcessEvent();
-            SDL_Delay(1);
-         }
+		 PAL_DelayUntil(dwTime);
 
          //
          // Set the time of the next frame.
@@ -2365,12 +2350,7 @@ PAL_BattleShowPlayerDefMagicAnim(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.
@@ -2592,12 +2572,7 @@ PAL_BattleShowPlayerOffMagicAnim(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.
@@ -2792,12 +2767,7 @@ PAL_BattleShowEnemyMagicAnim(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.
@@ -2988,12 +2958,7 @@ PAL_BattleShowPlayerSummonMagicAnim(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.

+ 1 - 6
game.c

@@ -113,12 +113,7 @@ PAL_GameMain(
       //
       // Wait for the time of one frame. Accept input here.
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= dwTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(dwTime);
 
       //
       // Set the time of the next frame.

+ 6 - 3
input.c

@@ -106,13 +106,16 @@ PAL_KeyboardEventFilter(
 
 #ifdef __WINPHONE__
       case SDLK_AC_BACK:
-         if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
+         if (g_uiLastBackKeyTime != 0 && !SDL_TICKS_PASSED(SDL_GetTicks(), g_uiLastBackKeyTime + 800))
          {
             PAL_Shutdown();
             exit(0);
          }
-         g_uiLastBackKeyTime = SDL_GetTicks();
-         VIDEO_UpdateScreen(NULL);
+		 else
+		 {
+            g_uiLastBackKeyTime = SDL_GetTicks();
+            VIDEO_UpdateScreen(NULL);
+		 }
          break;
 #endif
 

+ 1 - 1
itemmenu.c

@@ -431,7 +431,7 @@ PAL_ItemSelectMenu(
       PAL_ClearKeyState();
 
       PAL_ProcessEvent();
-      while (SDL_GetTicks() < dwTime)
+      while (!SDL_TICKS_PASSED(SDL_GetTicks(), dwTime))
       {
          PAL_ProcessEvent();
          if (g_InputState.dwKeyPress != 0)

+ 1 - 1
magicmenu.c

@@ -462,7 +462,7 @@ PAL_MagicSelectionMenu(
       }
 
       PAL_ProcessEvent();
-      while (SDL_GetTicks() < dwTime)
+      while (!SDL_TICKS_PASSED(SDL_GetTicks(), dwTime))
       {
          PAL_ProcessEvent();
          if (g_InputState.dwKeyPress != 0)

+ 3 - 3
palette.c

@@ -324,7 +324,7 @@ PAL_SceneFade(
 
          while (PAL_PollEvent(NULL));
 
-         while (SDL_GetTicks() < time)
+         while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
          {
             while (PAL_PollEvent(NULL));
             SDL_Delay(5);
@@ -360,7 +360,7 @@ PAL_SceneFade(
 
          while (PAL_PollEvent(NULL));
 
-         while (SDL_GetTicks() < time)
+         while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
          {
             while (PAL_PollEvent(NULL));
             SDL_Delay(5);
@@ -440,7 +440,7 @@ PAL_PaletteFade(
 
       while (PAL_PollEvent(NULL));
 
-      while (SDL_GetTicks() < time)
+      while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
       {
          while (PAL_PollEvent(NULL));
          SDL_Delay(5);

+ 1 - 1
play.c

@@ -612,7 +612,7 @@ PAL_WaitForKey(
 
    PAL_ClearKeyState();
 
-   while (wTimeOut == 0 || SDL_GetTicks() < dwTimeOut)
+   while (wTimeOut == 0 || !SDL_TICKS_PASSED(SDL_GetTicks(), dwTimeOut))
    {
       UTIL_Delay(5);
 

+ 1 - 6
rngplay.c

@@ -470,12 +470,7 @@ PAL_RNGPlay(
       //
       // Delay for a while
       //
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= iTime)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(iTime);
    }
 
    fclose(fp);

+ 4 - 24
script.c

@@ -139,12 +139,7 @@ PAL_PartyWalkTo(
 
    while (xOffset != 0 || yOffset != 0)
    {
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= t)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(t);
 
       t = SDL_GetTicks() + FRAME_TIME;
 
@@ -252,12 +247,7 @@ PAL_PartyRideEventObject(
 
    while (xOffset != 0 || yOffset != 0)
    {
-      PAL_ProcessEvent();
-      while (SDL_GetTicks() <= t)
-      {
-         PAL_ProcessEvent();
-         SDL_Delay(1);
-      }
+      PAL_DelayUntil(t);
 
       t = SDL_GetTicks() + FRAME_TIME;
 
@@ -2322,12 +2312,7 @@ PAL_InterpretInstruction(
             //
             // Delay for one frame
             //
-            PAL_ProcessEvent();
-            while (SDL_GetTicks() < time)
-            {
-               PAL_ProcessEvent();
-               SDL_Delay(1);
-            }
+			PAL_DelayUntil(time);
             time = SDL_GetTicks() + FRAME_TIME;
          } while (++i < (SHORT)(pScript->rgwOperand[2]));
       }
@@ -3225,12 +3210,7 @@ PAL_RunTriggerScript(
 
             for (i = 0; i < (pScript->rgwOperand[0] ? pScript->rgwOperand[0] : 1); i++)
             {
-               PAL_ProcessEvent();
-               while (SDL_GetTicks() < time)
-               {
-                  PAL_ProcessEvent();
-                  SDL_Delay(1);
-               }
+               PAL_DelayUntil(time);
 
                time = SDL_GetTicks() + FRAME_TIME;
 

+ 2 - 2
uibattle.c

@@ -566,7 +566,7 @@ PAL_BattleUIShowText(
 
 --*/
 {
-   if (SDL_GetTicks() < g_Battle.UI.dwMsgShowTime)
+   if (!SDL_TICKS_PASSED(SDL_GetTicks(), g_Battle.UI.dwMsgShowTime))
    {
       wcscpy(g_Battle.UI.szNextMsg, lpszText);
       g_Battle.UI.wNextMsgDuration = wDuration;
@@ -1687,7 +1687,7 @@ end:
    // Show the text message if there is one.
    //
 #ifndef PAL_CLASSIC
-   if (SDL_GetTicks() < g_Battle.UI.dwMsgShowTime)
+   if (!SDL_TICKS_PASSED(SDL_GetTicks(), g_Battle.UI.dwMsgShowTime))
    {
       //
       // The text should be shown in a small window at the center of the screen

+ 2 - 2
uigame.c

@@ -1408,7 +1408,7 @@ PAL_ItemUseMenu(
          //
          // See if we should change the highlight color
          //
-         if (SDL_GetTicks() > dwColorChangeTime)
+         if (SDL_TICKS_PASSED(SDL_GetTicks(), dwColorChangeTime))
          {
             if ((WORD)bSelectedColor + 1 >=
                (WORD)MENUITEM_COLOR_SELECTED_FIRST + MENUITEM_COLOR_SELECTED_TOTALNUM)
@@ -1866,7 +1866,7 @@ PAL_EquipItemMenu(
          //
          // See if we should change the highlight color
          //
-         if (SDL_GetTicks() > dwColorChangeTime)
+         if (SDL_TICKS_PASSED(SDL_GetTicks(), dwColorChangeTime))
          {
             if ((WORD)bSelectedColor + 1 >=
                (WORD)MENUITEM_COLOR_SELECTED_FIRST + MENUITEM_COLOR_SELECTED_TOTALNUM)

+ 1 - 1
util.c

@@ -271,7 +271,7 @@ UTIL_Delay(
 
    while (PAL_PollEvent(NULL));
 
-   while (SDL_GetTicks() < t)
+   while (!SDL_TICKS_PASSED(SDL_GetTicks(), t))
    {
       SDL_Delay(1);
       while (PAL_PollEvent(NULL));

+ 36 - 75
video.c

@@ -176,13 +176,10 @@ VIDEO_Startup(
       // Totally ugly hack to satisfy M$'s silly requirements.
       // No need to understand this crap.
       //
-      SDL_Color palette[256] = { 0 };
-      SDL_Surface *p;
-      palette[0].r = palette[0].g = palette[0].b = palette[0].a = 0;
-      palette[1].r = palette[1].g = palette[1].b = palette[1].a = 255;
+      SDL_Color palette[256] = { { 0, 0, 0, 0 }, { 255, 255, 255, 255 } };
+      SDL_Surface *p = gpScreen;
       SDL_FillRect(gpScreenBak, NULL, 0);
       VIDEO_SetPalette(palette);
-      p = gpScreen;
       gpScreen = gpScreenBak;
 	  switch(gpGlobals->iCodePage)
 	  {
@@ -193,7 +190,8 @@ VIDEO_Startup(
 		  PAL_DrawText(L"\x518D\x6B21\x6309 Back \x7ED3\x675F", PAL_XY(30, 30), 1, FALSE, FALSE);
 		  break;
 	  case CP_SHIFTJIS:
-		  PAL_DrawText(L"Press Back again to end", PAL_XY(30, 30), 1, FALSE, FALSE);	// TODO: Japanese string
+		  PAL_DrawText(L"\x7D42\x4E86\x3059\x308B\x306B\x306F\x3001\x3082\x3046\x4E00\x5EA6 Back \x30AD\x30FC", PAL_XY(30, 30), 1, FALSE, FALSE);
+		  PAL_DrawText(L"\x3092\x62BC\x3057\x3066\x304F\x3060\x3055\x3044", PAL_XY(30, 46), 1, FALSE, FALSE);
 		  break;
 	  }
       gpScreen = p;
@@ -363,6 +361,33 @@ VIDEO_Shutdown(
    gpScreenReal = NULL;
 }
 
+#if SDL_VERSION_ATLEAST(2,0,0)
+PAL_FORCE_INLINE
+VOID
+VIDEO_RenderCopy(
+   VOID
+)
+{
+   SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
+   SDL_RenderCopy(gpRenderer, gpTexture, NULL, gpRenderRect);
+   if (gpTouchOverlay)
+   {
+      SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, gpRenderRect);
+   }
+#ifdef __WINPHONE__
+   if (gpBackKeyMessage)
+   {
+      extern unsigned int g_uiLastBackKeyTime;
+      if (g_uiLastBackKeyTime != 0 && !SDL_TICKS_PASSED(SDL_GetTicks(), g_uiLastBackKeyTime + 800))
+      {
+         SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, gpRenderRect);
+      }
+   }
+#endif
+   SDL_RenderPresent(gpRenderer);
+}
+#endif
+
 VOID
 VIDEO_UpdateScreen(
    const SDL_Rect  *lpRect
@@ -496,23 +521,7 @@ VIDEO_UpdateScreen(
    }
 
 #if SDL_VERSION_ATLEAST(2,0,0)
-   SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
-   SDL_RenderCopy(gpRenderer, gpTexture, NULL, gpRenderRect);
-   if (gpTouchOverlay)
-   {
-     SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
-   }
-#ifdef __WINPHONE__
-   if (gpBackKeyMessage)
-   {
-      extern unsigned int g_uiLastBackKeyTime;
-      if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
-      {
-         SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, NULL);
-      }
-   }
-#endif
-   SDL_RenderPresent(gpRenderer);
+   VIDEO_RenderCopy();
 #else
    SDL_UpdateRect(gpScreenReal, dstrect.x, dstrect.y, dstrect.w, dstrect.h);
 #endif
@@ -959,23 +968,7 @@ VIDEO_SwitchScreen(
 
       SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
 #if SDL_VERSION_ATLEAST(2, 0, 0)
-      SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
-      SDL_RenderCopy(gpRenderer, gpTexture, NULL, NULL);
-      if (gpTouchOverlay)
-      {
-         SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
-      }
-#ifdef __WINPHONE__
-      if (gpBackKeyMessage)
-      {
-         extern unsigned int g_uiLastBackKeyTime;
-         if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
-         {
-           SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, NULL);
-         }
-      }
-#endif
-      SDL_RenderPresent(gpRenderer);
+      VIDEO_RenderCopy();
 #else
       SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
 #endif
@@ -1037,7 +1030,7 @@ VIDEO_FadeScreen(
       for (j = 0; j < 6; j++)
       {
          PAL_ProcessEvent();
-         while (SDL_GetTicks() <= time)
+         while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
          {
             PAL_ProcessEvent();
             SDL_Delay(5);
@@ -1112,23 +1105,7 @@ VIDEO_FadeScreen(
 
             SDL_FillRect(gpScreenReal, &dstrect, 0);
 #if SDL_VERSION_ATLEAST(2, 0, 0)
-            SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
-            SDL_RenderCopy(gpRenderer, gpTexture, NULL, NULL);
-            if (gpTouchOverlay)
-            {
-               SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
-            }
-#ifdef __WINPHONE__
-            if (gpBackKeyMessage)
-            {
-               extern unsigned int g_uiLastBackKeyTime;
-               if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
-               {
-                  SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, NULL);
-               }
-            }
-#endif
-			SDL_RenderPresent(gpRenderer);
+            VIDEO_RenderCopy();
 #else
 			SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
 #endif
@@ -1143,23 +1120,7 @@ VIDEO_FadeScreen(
 
             SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
 #if SDL_VERSION_ATLEAST(2, 0, 0)
-            SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
-            SDL_RenderCopy(gpRenderer, gpTexture, NULL, NULL);
-            if (gpTouchOverlay)
-            {
-               SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
-            }
-#ifdef __WINPHONE__
-            if (gpBackKeyMessage)
-            {
-               extern unsigned int g_uiLastBackKeyTime;
-               if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
-               {
-                  SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, NULL);
-               }
-            }
-#endif
-            SDL_RenderPresent(gpRenderer);
+            VIDEO_RenderCopy();
 #else
             SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
 #endif