Browse Source

Remove some direct calls to "SDL_xxx" outside video.c

LouYihua 7 years ago
parent
commit
2de87ad8a9
11 changed files with 208 additions and 339 deletions
  1. 12 50
      battle.c
  2. 0 5
      battle.h
  3. 18 46
      ending.c
  4. 17 17
      fight.c
  5. 13 23
      main.c
  6. 11 11
      script.c
  7. 3 3
      text.c
  8. 29 58
      ui.c
  9. 4 4
      uigame.c
  10. 81 103
      video.c
  11. 20 19
      video.h

+ 12 - 50
battle.c

@@ -195,28 +195,6 @@ PAL_BattleMakeScene(
    }
 }
 
-VOID
-PAL_BattleBackupScene(
-   VOID
-)
-/*++
-  Purpose:
-
-    Backup the scene buffer.
-
-  Parameters:
-
-    None.
-
-  Return value:
-
-    None.
-
---*/
-{
-   SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreenBak, NULL);
-}
-
 VOID
 PAL_BattleFadeScene(
    VOID
@@ -277,7 +255,7 @@ PAL_BattleFadeScene(
          //
          // Draw the backup buffer to the screen
          //
-         SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);
+		 VIDEO_RestoreScreen(gpScreen);
 
          PAL_BattleUIUpdate();
          VIDEO_UpdateScreen(NULL);
@@ -287,7 +265,7 @@ PAL_BattleFadeScene(
    //
    // Draw the result buffer to the screen as the final step
    //
-   SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+   VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
    PAL_BattleUIUpdate();
 
    VIDEO_UpdateScreen(NULL);
@@ -315,13 +293,13 @@ PAL_BattleMain(
    int         i;
    DWORD       dwTime;
    
-   VIDEO_BackupScreen();
+   VIDEO_BackupScreen(gpScreen);
 
    //
    // Generate the scene and draw the scene to the screen buffer
    //
    PAL_BattleMakeScene();
-   SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+   VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
    //
    // Fade out the music and delay for a while
@@ -581,20 +559,13 @@ PAL_LoadBattleBackground(
    //
    // Create the surface
    //
-   g_Battle.lpBackground =
-      SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
-      gpScreen->format->Rmask, gpScreen->format->Gmask,
-      gpScreen->format->Bmask, gpScreen->format->Amask);
+   g_Battle.lpBackground = VIDEO_CreateCompatibleSurface(gpScreen);
 
    if (g_Battle.lpBackground == NULL)
    {
       TerminateOnError("PAL_LoadBattleBackground(): failed to create surface!");
    }
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-   SDL_SetSurfacePalette(g_Battle.lpBackground, gpScreen->format->palette);
-#else
-   SDL_SetPalette(g_Battle.lpBackground, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
-#endif
+
    //
    // Load the picture
    //
@@ -639,7 +610,7 @@ PAL_BattleWon(
    //
    OrigPlayerRoles = gpGlobals->g.PlayerRoles;
 
-   VIDEO_BackupScreen();
+   VIDEO_BackupScreen(gpScreen);
 
    if (g_Battle.iExpGained > 0)
    {
@@ -740,7 +711,7 @@ PAL_BattleWon(
 
       if (fLevelUp)
       {
-         VIDEO_RestoreScreen();
+         VIDEO_RestoreScreen(gpScreen);
          //
          // Player has gained a level. Show the message
          //
@@ -1035,7 +1006,7 @@ PAL_BattleEnemyEscape(
    	  }
 
    	  PAL_BattleMakeScene();
-      SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+      VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
       VIDEO_UpdateScreen(NULL);
 
       UTIL_Delay(10);
@@ -1355,22 +1326,13 @@ PAL_StartBattle(
    //
    // Create the surface for scene buffer
    //
-   g_Battle.lpSceneBuf =
-      SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
-      gpScreen->format->Rmask, gpScreen->format->Gmask,
-      gpScreen->format->Bmask, gpScreen->format->Amask);
+   g_Battle.lpSceneBuf = VIDEO_CreateCompatibleSurface(gpScreen);
 
    if (g_Battle.lpSceneBuf == NULL)
    {
       TerminateOnError("PAL_StartBattle(): creating surface for scene buffer failed!");
    }
 
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-   SDL_SetSurfacePalette(g_Battle.lpSceneBuf, gpScreen->format->palette);
-#else
-   SDL_SetPalette(g_Battle.lpSceneBuf, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
-#endif
-
    PAL_UpdateEquipments();
 
    g_Battle.iExpGained = 0;
@@ -1465,8 +1427,8 @@ PAL_StartBattle(
    //
    // Free the surfaces for the background picture and scene buffer
    //
-   SDL_FreeSurface(g_Battle.lpBackground);
-   SDL_FreeSurface(g_Battle.lpSceneBuf);
+   VIDEO_FreeSurface(g_Battle.lpBackground);
+   VIDEO_FreeSurface(g_Battle.lpSceneBuf);
 
    g_Battle.lpBackground = NULL;
    g_Battle.lpSceneBuf = NULL;

+ 0 - 5
battle.h

@@ -206,11 +206,6 @@ PAL_BattleMakeScene(
    VOID
 );
 
-VOID
-PAL_BattleBackupScene(
-   VOID
-);
-
 VOID
 PAL_BattleFadeScene(
    VOID

+ 18 - 46
ending.c

@@ -70,7 +70,6 @@ PAL_ShowFBP(
    PAL_LARGE BYTE            buf[320 * 200];
    PAL_LARGE BYTE            bufSprite[320 * 200];
    const int                 rgIndex[6] = {0, 3, 1, 5, 2, 4};
-   SDL_Surface              *p;
    int                       i, j, k;
    BYTE                      a, b;
 
@@ -86,19 +85,13 @@ PAL_ShowFBP(
 
    if (wFade)
    {
+      SDL_Surface *p = VIDEO_CreateCompatibleSurface(gpScreen);
+
       wFade++;
       wFade *= 10;
 
-      p = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
-         gpScreen->format->Rmask, gpScreen->format->Gmask,
-         gpScreen->format->Bmask, gpScreen->format->Amask);
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-      SDL_SetSurfacePalette(p, gpScreen->format->palette);
-#else
-      SDL_SetPalette(p, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
-#endif
       PAL_FBPBlitToSurface(buf, p);
-      VIDEO_BackupScreen();
+      VIDEO_BackupScreen(gpScreen);
 
       for (i = 0; i < 16; i++)
       {
@@ -128,7 +121,7 @@ PAL_ShowFBP(
                ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
             }
 
-            SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);
+			VIDEO_RestoreScreen(gpScreen);
 
             if (g_wCurEffectSprite != 0)
             {
@@ -142,7 +135,7 @@ PAL_ShowFBP(
          }
       }
 
-      SDL_FreeSurface(p);
+	  VIDEO_FreeSurface(p);
    }
 
    //
@@ -197,22 +190,14 @@ PAL_ScrollFBP(
       PAL_MKFDecompressChunk(bufSprite, 320 * 200, g_wCurEffectSprite, gpGlobals->f.fpMGO);
    }
 
-   p = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
-      gpScreen->format->Rmask, gpScreen->format->Gmask,
-      gpScreen->format->Bmask, gpScreen->format->Amask);
+   p = VIDEO_CreateCompatibleSurface(gpScreen);
 
    if (p == NULL)
    {
       return;
    }
 
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-   SDL_SetSurfacePalette(p, gpScreen->format->palette);
-#else
-   SDL_SetPalette(p, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
-#endif
-
-   VIDEO_BackupScreen();
+   VIDEO_BackupScreen(gpScreen);
    PAL_FBPBlitToSurface(buf, p);
 
    if (wScrollSpeed == 0)
@@ -248,7 +233,7 @@ PAL_ScrollFBP(
          dstrect.h = 200 - i;
       }
 
-      SDL_BlitSurface(gpScreenBak, &rect, gpScreen, &dstrect);
+      VIDEO_CopySurface(gpScreenBak, &rect, gpScreen, &dstrect);
 
       if (fScrollDown)
       {
@@ -265,7 +250,7 @@ PAL_ScrollFBP(
          dstrect.h = i;
       }
 
-      SDL_BlitSurface(p, &rect, gpScreen, &dstrect);
+	  VIDEO_CopySurface(p, &rect, gpScreen, &dstrect);
 
       PAL_ApplyWave(gpScreen);
 
@@ -292,8 +277,8 @@ PAL_ScrollFBP(
       UTIL_Delay(800 / wScrollSpeed);
    }
 
-   SDL_BlitSurface(p, NULL, gpScreen, NULL);
-   SDL_FreeSurface(p);
+   VIDEO_CopyEntireSurface(p, gpScreen);
+   VIDEO_FreeSurface(p);
    VIDEO_UpdateScreen(NULL);
 }
 
@@ -328,21 +313,8 @@ PAL_EndingAnimation(
    buf = (LPBYTE)UTIL_calloc(1, 64000);
    bufGirl = (LPBYTE)UTIL_calloc(1, 6000);
 
-   pUpper = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
-      gpScreen->format->Rmask, gpScreen->format->Gmask,
-      gpScreen->format->Bmask, gpScreen->format->Amask);
-
-   pLower = SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
-      gpScreen->format->Rmask, gpScreen->format->Gmask,
-      gpScreen->format->Bmask, gpScreen->format->Amask);
-
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-   SDL_SetSurfacePalette(pUpper, gpScreen->format->palette);
-   SDL_SetSurfacePalette(pLower, gpScreen->format->palette);
-#else
-   SDL_SetPalette(pUpper, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
-   SDL_SetPalette(pLower, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
-#endif
+   pUpper = VIDEO_CreateCompatibleSurface(gpScreen);
+   pLower = VIDEO_CreateCompatibleSurface(gpScreen);
 
    PAL_MKFDecompressChunk(buf, 64000, gConfig.fIsWIN95 ? 69 : 61, gpGlobals->f.fpFBP);
    PAL_FBPBlitToSurface(buf, pUpper);
@@ -371,7 +343,7 @@ PAL_EndingAnimation(
       dstrect.y = i / 2;
       dstrect.h = 200 - i / 2;
 
-      SDL_BlitSurface(pLower, &srcrect, gpScreen, &dstrect);
+	  VIDEO_CopySurface(pLower, &srcrect, gpScreen, &dstrect);
 
       srcrect.y = 200 - i / 2;
       srcrect.h = i / 2;
@@ -379,7 +351,7 @@ PAL_EndingAnimation(
       dstrect.y = 0;
       dstrect.h = i / 2;
 
-      SDL_BlitSurface(pUpper, &srcrect, gpScreen, &dstrect);
+	  VIDEO_CopySurface(pUpper, &srcrect, gpScreen, &dstrect);
 
       PAL_ApplyWave(gpScreen);
 
@@ -422,8 +394,8 @@ PAL_EndingAnimation(
 
    gpGlobals->wScreenWave = 0;
 
-   SDL_FreeSurface(pUpper);
-   SDL_FreeSurface(pLower);
+   VIDEO_FreeSurface(pUpper);
+   VIDEO_FreeSurface(pLower);
 
    free(buf);
    free(bufGirl);
@@ -475,7 +447,7 @@ PAL_EndingScreen(
 	PAL_EndingSetEffectSprite(0);
 	PAL_ShowFBP(77, 10);
 
-	VIDEO_BackupScreen();
+	VIDEO_BackupScreen(gpScreen);
 
 	PAL_EndingSetEffectSprite(0x27b);
 	PAL_ShowFBP(76, 7);

+ 17 - 17
fight.c

@@ -498,7 +498,7 @@ PAL_BattleDelay(
       dwTime = SDL_GetTicks() + BATTLE_FRAME_TIME;
 
       PAL_BattleMakeScene();
-      SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+      VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
       PAL_BattleUIUpdate();
 
       if (wObjectID != 0)
@@ -765,7 +765,7 @@ PAL_BattlePostActionCheck(
                   PAL_BattleDelay(10, 0, TRUE);
 
                   PAL_BattleMakeScene();
-                  SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+                  VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
                   VIDEO_UpdateScreen(NULL);
 
                   g_Battle.BattleResult = kBattleResultPause;
@@ -828,7 +828,7 @@ PAL_BattlePostActionCheck(
                   PAL_BattleDelay(10, 0, TRUE);
 
                   PAL_BattleMakeScene();
-                  SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+                  VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
                   VIDEO_UpdateScreen(NULL);
 
                   g_Battle.BattleResult = kBattleResultPause;
@@ -849,7 +849,7 @@ PAL_BattlePostActionCheck(
 end:
    if (fFade)
    {
-      PAL_BattleBackupScene();
+      VIDEO_BackupScreen(g_Battle.lpSceneBuf);
       PAL_BattleMakeScene();
       PAL_BattleFadeScene();
    }
@@ -867,7 +867,7 @@ end:
 
       g_Battle.sBackgroundColorShift = 0;
 
-      PAL_BattleBackupScene();
+      VIDEO_BackupScreen(g_Battle.lpSceneBuf);
       PAL_BattleMakeScene();
       PAL_BattleFadeScene();
    }
@@ -1068,7 +1068,7 @@ PAL_BattleStartFrame(
    // Update the scene
    //
    PAL_BattleMakeScene();
-   SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+   VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
    //
    // Check if the battle is over
@@ -1125,7 +1125,7 @@ PAL_BattleStartFrame(
 
       if (g_Battle.iHidingTime == 0)
       {
-         PAL_BattleBackupScene();
+         VIDEO_BackupScreen(g_Battle.lpSceneBuf);
          PAL_BattleMakeScene();
          PAL_BattleFadeScene();
       }
@@ -1603,7 +1603,7 @@ PAL_BattleStartFrame(
          {
             if (--g_Battle.iHidingTime == 0)
             {
-               PAL_BattleBackupScene();
+               VIDEO_BackupScreen(g_Battle.lpSceneBuf);
                PAL_BattleMakeScene();
                PAL_BattleFadeScene();
             }
@@ -2028,7 +2028,7 @@ PAL_BattleShowPlayerAttackAnim(
       }
 
       PAL_BattleMakeScene();
-      SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+      VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
       PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
       x -= 16;
@@ -2275,7 +2275,7 @@ PAL_BattleShowPlayerPreMagicAnim(
          }
 
          PAL_BattleMakeScene();
-         SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+         VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
          PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
 
@@ -2356,7 +2356,7 @@ PAL_BattleShowPlayerDefMagicAnim(
          (gpGlobals->g.lprgMagic[iMagicNum].wSpeed + 5) * 10;
 
       PAL_BattleMakeScene();
-      SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+      VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
       if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeApplyToParty)
       {
@@ -2578,7 +2578,7 @@ PAL_BattleShowPlayerOffMagicAnim(
          (gpGlobals->g.lprgMagic[iMagicNum].wSpeed + 5) * 10;
 
       PAL_BattleMakeScene();
-      SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+      VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
       if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeNormal)
       {
@@ -2782,7 +2782,7 @@ PAL_BattleShowEnemyMagicAnim(
          (gpGlobals->g.lprgMagic[iMagicNum].wSpeed + 5) * 10;
 
       PAL_BattleMakeScene();
-      SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+      VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
       if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeNormal)
       {
@@ -2935,7 +2935,7 @@ PAL_BattleShowPlayerSummonMagicAnim(
       PAL_BattleDelay(1, wObjectID, TRUE);
    }
 
-   PAL_BattleBackupScene();
+   VIDEO_BackupScreen(g_Battle.lpSceneBuf);
 
    //
    // Load the sprite of the summoned god
@@ -2976,7 +2976,7 @@ PAL_BattleShowPlayerSummonMagicAnim(
          (gpGlobals->g.lprgMagic[wMagicNum].wSpeed + 5) * 10;
 
       PAL_BattleMakeScene();
-      SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+      VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
 
       PAL_BattleUIUpdate();
 
@@ -3892,7 +3892,7 @@ PAL_BattlePlayerPerformAction(
                      PAL_BattleDelay(1, 0, TRUE);
                   }
 
-                  PAL_BattleBackupScene();
+                  VIDEO_BackupScreen(g_Battle.lpSceneBuf);
                   PAL_LoadBattleSprites();
 
                   g_Battle.rgPlayer[wPlayerIndex].iColorShift = 0;
@@ -4064,7 +4064,7 @@ PAL_BattlePlayerPerformAction(
             g_Battle.iHidingTime *= 1.2;
          }
 #endif
-         PAL_BattleBackupScene();
+         VIDEO_BackupScreen(g_Battle.lpSceneBuf);
          PAL_BattleMakeScene();
          PAL_BattleFadeScene();
       }

+ 13 - 23
main.c

@@ -92,7 +92,7 @@ PAL_Init(
       TerminateOnError("Could not initialize Video: %d.\n", e);
    }
 
-   SDL_WM_SetCaption("Loading...", NULL);
+   VIDEO_SetWindowTitle("Loading...");
 
    if (!gConfig.fIsWIN95 && gConfig.fUseEmbeddedFonts)
    {
@@ -131,17 +131,17 @@ PAL_Init(
    if (gConfig.fIsWIN95)
    {
 #ifdef _DEBUG
-      SDL_WM_SetCaption("Pal WIN95 (Debug Build)", NULL);
+      VIDEO_SetWindowTitle("Pal WIN95 (Debug Build)");
 #else
-      SDL_WM_SetCaption("Pal WIN95", NULL);
+      VIDEO_SetWindowTitle("Pal WIN95");
 #endif
    }
    else
    {
 #ifdef _DEBUG
-      SDL_WM_SetCaption("Pal (Debug Build)", NULL);
+      VIDEO_SetWindowTitle("Pal (Debug Build)");
 #else
-      SDL_WM_SetCaption("Pal", NULL);
+      VIDEO_SetWindowTitle("Pal");
 #endif
    }
 }
@@ -258,20 +258,8 @@ PAL_SplashScreen(
    //
    // Create the surfaces
    //
-   lpBitmapDown = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
-      gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
-      gpScreen->format->Amask);
-   lpBitmapUp = SDL_CreateRGBSurface(gpScreen->flags, 320, 200, 8,
-      gpScreen->format->Rmask, gpScreen->format->Gmask, gpScreen->format->Bmask,
-      gpScreen->format->Amask);
-
-#if SDL_VERSION_ATLEAST(2, 0, 0)
-   SDL_SetSurfacePalette(lpBitmapDown, gpScreen->format->palette);
-   SDL_SetSurfacePalette(lpBitmapUp, gpScreen->format->palette);
-#else
-   SDL_SetPalette(lpBitmapDown, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
-   SDL_SetPalette(lpBitmapUp, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
-#endif
+   lpBitmapDown = VIDEO_CreateCompatibleSurface(gpScreen);
+   lpBitmapUp = VIDEO_CreateCompatibleSurface(gpScreen);
 
    //
    // Read the bitmaps
@@ -368,7 +356,7 @@ PAL_SplashScreen(
       dstrect.y = 0;
       dstrect.h = srcrect.h;
 
-      SDL_BlitSurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
+	  VIDEO_CopySurface(lpBitmapUp, &srcrect, gpScreen, &dstrect);
 
       //
       // The lower part...
@@ -379,7 +367,7 @@ PAL_SplashScreen(
       dstrect.y = 200 - iImgPos;
       dstrect.h = srcrect.h;
 
-      SDL_BlitSurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
+	  VIDEO_CopySurface(lpBitmapDown, &srcrect, gpScreen, &dstrect);
 
       //
       // Draw the cranes...
@@ -471,8 +459,8 @@ PAL_SplashScreen(
       }
    }
 
-   SDL_FreeSurface(lpBitmapDown);
-   SDL_FreeSurface(lpBitmapUp);
+   VIDEO_FreeSurface(lpBitmapDown);
+   VIDEO_FreeSurface(lpBitmapUp);
    free(buf);
 
    if (!fUseCD)
@@ -483,6 +471,8 @@ PAL_SplashScreen(
    PAL_FadeOut(1);
 }
 
+
+
 int
 main(
    int      argc,

+ 11 - 11
script.c

@@ -2071,7 +2071,7 @@ PAL_InterpretInstruction(
       //
       // Fade the screen to scene
       //
-      VIDEO_BackupScreen();
+      VIDEO_BackupScreen(gpScreen);
       PAL_MakeScene();
       VIDEO_FadeScreen(pScript->rgwOperand[0]);
       break;
@@ -2583,7 +2583,7 @@ PAL_InterpretInstruction(
             }
             PAL_BattleDelay(1, 0, TRUE);
          }
-         PAL_BattleBackupScene();
+         VIDEO_BackupScreen(g_Battle.lpSceneBuf);
          PAL_BattleUpdateFighters();
          PAL_BattleMakeScene();
          PAL_BattleFadeScene();
@@ -2694,7 +2694,7 @@ PAL_InterpretInstruction(
       // Fade to the current scene
       // FIXME: This is obviously wrong
       //
-      VIDEO_BackupScreen();
+      VIDEO_BackupScreen(gpScreen);
       PAL_MakeScene();
       VIDEO_FadeScreen(2);
       break;
@@ -2838,7 +2838,7 @@ PAL_InterpretInstruction(
 
          PAL_BattleDelay(2, 0, TRUE);
 
-         PAL_BattleBackupScene();
+         VIDEO_BackupScreen(g_Battle.lpSceneBuf);
          PAL_LoadBattleSprites();
          PAL_BattleMakeScene();
          AUDIO_PlaySound(212);
@@ -2849,7 +2849,7 @@ PAL_InterpretInstruction(
             g_Battle.rgEnemy[i].iColorShift = 0;
          }
 
-         PAL_BattleBackupScene();
+         VIDEO_BackupScreen(g_Battle.lpSceneBuf);
          PAL_BattleMakeScene();
          PAL_BattleFadeScene();
       }
@@ -2882,7 +2882,7 @@ PAL_InterpretInstruction(
          g_Battle.rgEnemy[wEventObjectID].iColorShift = 0;
 
 		 AUDIO_PlaySound(47);
-         PAL_BattleBackupScene();
+         VIDEO_BackupScreen(g_Battle.lpSceneBuf);
          PAL_LoadBattleSprites();
          PAL_BattleMakeScene();
          PAL_BattleFadeScene();
@@ -2973,7 +2973,7 @@ PAL_InterpretInstruction(
       //
       // backup screen
       //
-      VIDEO_BackupScreen();
+      VIDEO_BackupScreen(gpScreen);
       break;
 
    default:
@@ -3122,12 +3122,12 @@ PAL_RunTriggerScript(
 
          if (PAL_DialogIsPlayingRNG())
          {
-            VIDEO_RestoreScreen();
+            VIDEO_RestoreScreen(gpScreen);
          }
          else if (gpGlobals->fInBattle)
          {
             PAL_BattleMakeScene();
-            SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
+            VIDEO_CopyEntireSurface(g_Battle.lpSceneBuf, gpScreen);
             VIDEO_UpdateScreen(NULL);
          }
          else
@@ -3280,7 +3280,7 @@ PAL_RunTriggerScript(
          // Restore the screen
          //
          PAL_ClearDialog(TRUE);
-         VIDEO_RestoreScreen();
+         VIDEO_RestoreScreen(gpScreen);
          VIDEO_UpdateScreen(NULL);
          wScriptEntry++;
          break;
@@ -3300,7 +3300,7 @@ PAL_RunTriggerScript(
                   // Restore the screen
                   //
                   PAL_ClearDialog(TRUE);
-                  VIDEO_RestoreScreen();
+                  VIDEO_RestoreScreen(gpScreen);
                   VIDEO_UpdateScreen(NULL);
                }
 			   else

+ 3 - 3
text.c

@@ -972,7 +972,7 @@ PAL_StartDialog(
 
    if (fPlayingRNG && iNumCharFace)
    {
-      VIDEO_BackupScreen();
+      VIDEO_BackupScreen(gpScreen);
       g_TextLib.fPlayingRNG = TRUE;
    }
 
@@ -1181,7 +1181,7 @@ PAL_ShowDialogText(
       //
       PAL_DialogWaitForKey();
       g_TextLib.nCurrentDialogLine = 0;
-      VIDEO_RestoreScreen();
+      VIDEO_RestoreScreen(gpScreen);
       VIDEO_UpdateScreen(NULL);
    }
 
@@ -1264,7 +1264,7 @@ PAL_ShowDialogText(
             //
             // Save the screen before we show the first line of dialog
             //
-            VIDEO_BackupScreen();
+            VIDEO_BackupScreen(gpScreen);
          }
 
          while (lpszText != NULL && *lpszText != '\0')

+ 29 - 58
ui.c

@@ -25,6 +25,31 @@
 
 LPSPRITE      gpSpriteUI = NULL;
 
+static LPBOX
+PAL_CreateBoxInternal(
+	const SDL_Rect *rect
+)
+{
+	LPBOX lpBox = (LPBOX)calloc(1, sizeof(BOX));
+	if (lpBox == NULL)
+	{
+		return NULL;
+	}
+
+	lpBox->pos = PAL_XY(rect->x, rect->y);
+	lpBox->lpSavedArea = VIDEO_DuplicateSurface(gpScreen, rect);
+	lpBox->wHeight = (WORD)rect->w;
+	lpBox->wWidth = (WORD)rect->h;
+
+	if (lpBox->lpSavedArea == NULL)
+	{
+		free(lpBox);
+		return NULL;
+	}
+
+	return lpBox;
+}
+
 INT
 PAL_InitUI(
    VOID
@@ -140,7 +165,6 @@ PAL_CreateBoxWithShadow(
    int              i, j, x, m, n;
    LPCBITMAPRLE     rglpBorderBitmap[3][3];
    LPBOX            lpBox = NULL;
-   SDL_Surface     *save;
    SDL_Rect         rect;
 
    //
@@ -185,33 +209,7 @@ PAL_CreateBoxWithShadow(
       //
       // Save the used part of the screen
       //
-      save = SDL_CreateRGBSurface(gpScreen->flags, rect.w, rect.h, 8,
-         gpScreen->format->Rmask, gpScreen->format->Gmask,
-         gpScreen->format->Bmask, gpScreen->format->Amask);
-
-      if (save == NULL)
-      {
-         return NULL;
-      }
-
-      lpBox = (LPBOX)calloc(1, sizeof(BOX));
-      if (lpBox == NULL)
-      {
-         SDL_FreeSurface(save);
-         return NULL;
-      }
-
-#if SDL_VERSION_ATLEAST(2,0,0)
-      SDL_SetSurfacePalette(save, gpScreen->format->palette);
-#else
-      SDL_SetPalette(save, SDL_LOGPAL | SDL_PHYSPAL, VIDEO_GetPalette(), 0, 256);
-#endif
-      SDL_BlitSurface(gpScreen, &rect, save, NULL);
-
-      lpBox->lpSavedArea = save;
-      lpBox->pos = pos;
-      lpBox->wWidth = rect.w;
-      lpBox->wHeight = rect.h;
+      lpBox = PAL_CreateBoxInternal(&rect);
    }
 
    //
@@ -286,7 +284,6 @@ PAL_CreateSingleLineBoxWithShadow(
    LPCBITMAPRLE          lpBitmapLeft;
    LPCBITMAPRLE          lpBitmapMid;
    LPCBITMAPRLE          lpBitmapRight;
-   SDL_Surface          *save;
    SDL_Rect              rect;
    LPBOX                 lpBox = NULL;
    int                   i;
@@ -318,33 +315,7 @@ PAL_CreateSingleLineBoxWithShadow(
       //
       // Save the used part of the screen
       //
-      save = SDL_CreateRGBSurface(gpScreen->flags, rect.w, rect.h, 8,
-         gpScreen->format->Rmask, gpScreen->format->Gmask,
-         gpScreen->format->Bmask, gpScreen->format->Amask);
-
-      if (save == NULL)
-      {
-         return NULL;
-      }
-
-      lpBox = (LPBOX)calloc(1, sizeof(BOX));
-      if (lpBox == NULL)
-      {
-         SDL_FreeSurface(gpScreen);
-         return NULL;
-      }
-
-#if SDL_VERSION_ATLEAST(2,0,0)
-      SDL_SetSurfacePalette(save, gpScreen->format->palette);
-#else
-      SDL_SetPalette(save, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
-#endif
-      SDL_BlitSurface(gpScreen, &rect, save, NULL);
-
-      lpBox->pos = pos;
-      lpBox->lpSavedArea = save;
-      lpBox->wHeight = (WORD)rect.w;
-      lpBox->wWidth = (WORD)rect.h;
+      lpBox = PAL_CreateBoxInternal(&rect);
    }
    xSaved = rect.x;
 
@@ -419,12 +390,12 @@ PAL_DeleteBox(
    rect.w = lpBox->wWidth;
    rect.h = lpBox->wHeight;
 
-   SDL_BlitSurface(lpBox->lpSavedArea, NULL, gpScreen, &rect);
+   VIDEO_CopySurface(lpBox->lpSavedArea, NULL, gpScreen, &rect);
 
    //
    // Free the memory used by the box
    //
-   SDL_FreeSurface(lpBox->lpSavedArea);
+   VIDEO_FreeSurface(lpBox->lpSavedArea);
    free(lpBox);
 }
 

+ 4 - 4
uigame.c

@@ -124,9 +124,9 @@ PAL_OpeningMenu(
          //
          // Load game
          //
-         VIDEO_BackupScreen();
+         VIDEO_BackupScreen(gpScreen);
          wItemSelected = PAL_SaveSlotMenu(1);
-         VIDEO_RestoreScreen();
+         VIDEO_RestoreScreen(gpScreen);
          VIDEO_UpdateScreen(NULL);
          if (wItemSelected != MENUITEM_VALUE_CANCELLED)
          {
@@ -963,7 +963,7 @@ PAL_InGameMenu(
    const SDL_Rect       rect = {0, 0, 320, 185};
    
    // Fix render problem with shadow
-   VIDEO_BackupScreen();
+   VIDEO_BackupScreen(gpScreen);
 
    //
    // Create menu items
@@ -1045,7 +1045,7 @@ out:
    PAL_DeleteBox(lpMenuBox);
 
    // Fix render problem with shadow
-   VIDEO_RestoreScreen();
+   VIDEO_RestoreScreen(gpScreen);
 }
 
 VOID

+ 81 - 103
video.c

@@ -168,36 +168,7 @@ VIDEO_Startup(
    //
    if (gpScreen == NULL || gpScreenBak == NULL || gpScreenReal == NULL || gpTexture == NULL)
    {
-      if (gpScreen != NULL)
-      {
-         SDL_FreeSurface(gpScreen);
-         gpScreen = NULL;
-      }
-
-      if (gpScreenBak != NULL)
-      {
-         SDL_FreeSurface(gpScreenBak);
-         gpScreenBak = NULL;
-      }
-
-      if (gpScreenReal != NULL)
-      {
-         SDL_FreeSurface(gpScreenReal);
-         gpScreenReal = NULL;
-      }
-
-	  if (gpTexture != NULL)
-	  {
-		 SDL_DestroyTexture(gpTexture);
-		 gpTexture = NULL;
-	  }
-
-      SDL_DestroyRenderer(gpRenderer);
-      gpRenderer = NULL;
-
-      SDL_DestroyWindow(gpWindow);
-      gpWindow = NULL;
-
+      VIDEO_Shutdown();
       return -2;
    }
 
@@ -258,21 +229,7 @@ VIDEO_Startup(
    //
    if (gpScreen == NULL || gpScreenBak == NULL)
    {
-      if (gpScreen != NULL)
-      {
-         SDL_FreeSurface(gpScreen);
-		 gpScreen = NULL;
-      }
-
-      if (gpScreenBak != NULL)
-      {
-         SDL_FreeSurface(gpScreenBak);
-		 gpScreenBak = NULL;
-      }
-
-      SDL_FreeSurface(gpScreenReal);
-	  gpScreenReal = NULL;
-
+      VIDEO_Shutdown();
       return -2;
    }
 
@@ -826,50 +783,6 @@ VIDEO_SaveScreenshot(
 #endif
 }
 
-VOID
-VIDEO_BackupScreen(
-   VOID
-)
-/*++
-  Purpose:
-
-    Backup the screen buffer.
-
-  Parameters:
-
-    None.
-
-  Return value:
-
-    None.
-
---*/
-{
-   SDL_BlitSurface(gpScreen, NULL, gpScreenBak, NULL);
-}
-
-VOID
-VIDEO_RestoreScreen(
-   VOID
-)
-/*++
-  Purpose:
-
-    Restore the screen buffer which has been saved with VIDEO_BackupScreen().
-
-  Parameters:
-
-    None.
-
-  Return value:
-
-    None.
-
---*/
-{
-   SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);
-}
-
 VOID
 VIDEO_ShakeScreen(
    WORD           wShakeTime,
@@ -1133,34 +1046,99 @@ VIDEO_FadeScreen(
    VIDEO_UpdateScreen(NULL);
 }
 
+void
+VIDEO_SetWindowTitle(
+	const char*     pszTitle
+)
+/*++
+  Purpose:
+
+    Set the caption of the window.
+
+  Parameters:
+
+    [IN]  lpszTitle - the new caption of the window.
+
+  Return value:
+
+    None.
+
+--*/
+{
 #if SDL_VERSION_ATLEAST(2,0,0)
+	SDL_SetWindowTitle(gpWindow, pszTitle);
+#else
+	SDL_WM_SetCaption(lpszCaption, NULL);
+#endif
+}
 
+SDL_Surface *
+VIDEO_CreateCompatibleSurface(
+	SDL_Surface    *pSource
+)
 /*++
   Purpose:
 
-    Set the caption of the window. For compatibility with SDL2 only.
+    Create a surface that compatible with the source surface.
 
   Parameters:
 
-    [IN]  lpszCaption - the new caption of the window.
-
-    [IN]  lpReserved - not used, for compatibility only.
+    [IN]  pSource   - the source surface from which attributes are taken.
 
   Return value:
 
     None.
 
 --*/
-VOID
-SDL_WM_SetCaption(
-   LPCSTR         lpszCaption,
-   LPVOID         lpReserved
-)
 {
-   if (gpWindow != NULL)
-   {
-      SDL_SetWindowTitle(gpWindow, lpszCaption);
-   }
-}
+	//
+	// Create the surface
+	//
+	SDL_Surface *dest = SDL_CreateRGBSurface(pSource->flags,
+		pSource->w, pSource->h, pSource->format->BitsPerPixel,
+		pSource->format->Rmask, pSource->format->Gmask,
+		pSource->format->Bmask, pSource->format->Amask);
 
+	if (dest)
+	{
+#if SDL_VERSION_ATLEAST(2, 0, 0)
+		SDL_SetSurfacePalette(dest, pSource->format->palette);
+#else
+		SDL_SetPalette(dest, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
 #endif
+	}
+
+	return dest;
+}
+
+SDL_Surface *
+VIDEO_DuplicateSurface(
+	SDL_Surface    *pSource,
+	const SDL_Rect *pRect
+)
+/*++
+  Purpose:
+
+    Duplicate the selected area from the source surface into new surface.
+
+  Parameters:
+
+    [IN]  pSource - the source surface.
+	[IN]  flagsMask - flags that should be taken away from the new surface.
+	[IN]  pRect   - the area to be duplicated, NULL for entire surface.
+
+  Return value:
+
+    None.
+
+--*/
+{
+	SDL_Surface* dest = VIDEO_CreateCompatibleSurface(pSource);
+
+	if (dest)
+	{
+		VIDEO_CopySurface(pSource, pRect, dest, NULL);
+	}
+
+	return dest;
+}

+ 20 - 19
video.h

@@ -33,6 +33,12 @@ extern SDL_Surface *gpScreen;
 extern SDL_Surface *gpScreenBak;
 extern volatile BOOL g_bRenderPaused;
 
+#define VIDEO_CopySurface(s, sr, t, tr) SDL_BlitSurface((s), (sr), (t), (tr))
+#define VIDEO_CopyEntireSurface(s, t)   SDL_BlitSurface((s), NULL, (t), NULL)
+#define VIDEO_BackupScreen(s)           SDL_BlitSurface((s), NULL, gpScreenBak, NULL)
+#define VIDEO_RestoreScreen(t)          SDL_BlitSurface(gpScreenBak, NULL, (t), NULL)
+#define VIDEO_FreeSurface(s)            SDL_FreeSurface(s)
+
 INT
 VIDEO_Startup(
    VOID
@@ -74,16 +80,6 @@ VIDEO_SaveScreenshot(
    VOID
 );
 
-VOID
-VIDEO_BackupScreen(
-   VOID
-);
-
-VOID
-VIDEO_RestoreScreen(
-   VOID
-);
-
 VOID
 VIDEO_ShakeScreen(
    WORD           wShakeTime,
@@ -100,16 +96,21 @@ VIDEO_FadeScreen(
    WORD           wSpeed
 );
 
-#if SDL_VERSION_ATLEAST(2,0,0)
-//
-// For compatibility with SDL2.
-//
-VOID
-SDL_WM_SetCaption(
-   LPCSTR         lpszCaption,
-   LPVOID         lpReserved
+void
+VIDEO_SetWindowTitle(
+	const char*   pszTitle
+);
+
+SDL_Surface *
+VIDEO_DuplicateSurface(
+	SDL_Surface    *pSource,
+	const SDL_Rect *pRect
+);
+
+SDL_Surface *
+VIDEO_CreateCompatibleSurface(
+	SDL_Surface    *pSource
 );
-#endif
 
 #ifdef __cplusplus
 }