video.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. #include "main.h"
  22. // Screen buffer
  23. SDL_Surface *gpScreen = NULL;
  24. // Backup screen buffer
  25. SDL_Surface *gpScreenBak = NULL;
  26. // The real screen surface
  27. #if SDL_VERSION_ATLEAST(2,0,0)
  28. static SDL_Window *gpWindow = NULL;
  29. static SDL_Renderer *gpRenderer = NULL;
  30. #else
  31. static SDL_Surface *gpScreenReal = NULL;
  32. #endif
  33. #if (defined (__SYMBIAN32__) && !defined (__S60_5X__)) || defined (PSP) || defined (GEKKO)
  34. static BOOL bScaleScreen = FALSE;
  35. #else
  36. static BOOL bScaleScreen = TRUE;
  37. #endif
  38. // Initial screen size
  39. static WORD g_wInitialWidth = 640;
  40. static WORD g_wInitialHeight = 400;
  41. // Shake times and level
  42. static WORD g_wShakeTime = 0;
  43. static WORD g_wShakeLevel = 0;
  44. INT
  45. #ifdef GEKKO // Rikku2000: Crash on compile, allready define on WIISDK
  46. VIDEO_Init_GEKKO(
  47. #else
  48. VIDEO_Init(
  49. #endif
  50. WORD wScreenWidth,
  51. WORD wScreenHeight,
  52. BOOL fFullScreen
  53. )
  54. /*++
  55. Purpose:
  56. Initialze the video subsystem.
  57. Parameters:
  58. [IN] wScreenWidth - width of the screen.
  59. [IN] wScreenHeight - height of the screen.
  60. [IN] fFullScreen - TRUE to use full screen mode, FALSE to use windowed mode.
  61. Return value:
  62. 0 = success, -1 = fail to create the screen surface,
  63. -2 = fail to create screen buffer.
  64. --*/
  65. {
  66. g_wInitialWidth = wScreenWidth;
  67. g_wInitialHeight = wScreenHeight;
  68. #if SDL_VERSION_ATLEAST(2,0,0)
  69. // TODO
  70. /*
  71. //
  72. // Create the screen surface.
  73. //
  74. #if defined (NDS)
  75. gpScreenReal = SDL_SetVideoMode(293, 196, 8, SDL_SWSURFACE | SDL_FULLSCREEN);
  76. #elif defined (__SYMBIAN32__)
  77. #ifdef __S60_5X__
  78. gpScreenReal = SDL_SetVideoMode(640, 360, 8,
  79. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  80. #else
  81. gpScreenReal = SDL_SetVideoMode(320, 240, 8,
  82. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  83. #endif
  84. #elif defined (GEKKO)
  85. gpScreenReal = SDL_SetVideoMode(640, 480, 8,
  86. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  87. #elif defined (PSP)
  88. gpScreenReal = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE | SDL_FULLSCREEN);
  89. #else
  90. gpScreenReal = SDL_SetVideoMode(wScreenWidth, wScreenHeight, 8,
  91. SDL_HWSURFACE | SDL_RESIZABLE | (fFullScreen ? SDL_FULLSCREEN : 0));
  92. #endif
  93. if (gpScreenReal == NULL)
  94. {
  95. //
  96. // Fall back to 640x480 software mode.
  97. //
  98. gpScreenReal = SDL_SetVideoMode(640, 480, 8,
  99. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  100. }
  101. //
  102. // Still fail?
  103. //
  104. if (gpScreenReal == NULL)
  105. {
  106. return -1;
  107. }
  108. //
  109. // Create the screen buffer and the backup screen buffer.
  110. //
  111. gpScreen = SDL_CreateRGBSurface(gpScreenReal->flags & ~SDL_HWSURFACE, 320, 200, 8,
  112. gpScreenReal->format->Rmask, gpScreenReal->format->Gmask,
  113. gpScreenReal->format->Bmask, gpScreenReal->format->Amask);
  114. gpScreenBak = SDL_CreateRGBSurface(gpScreenReal->flags & ~SDL_HWSURFACE, 320, 200, 8,
  115. gpScreenReal->format->Rmask, gpScreenReal->format->Gmask,
  116. gpScreenReal->format->Bmask, gpScreenReal->format->Amask);
  117. //
  118. // Failed?
  119. //
  120. if (gpScreen == NULL || gpScreenBak == NULL)
  121. {
  122. if (gpScreen != NULL)
  123. {
  124. SDL_FreeSurface(gpScreen);
  125. }
  126. if (gpScreenBak != NULL)
  127. {
  128. SDL_FreeSurface(gpScreenBak);
  129. }
  130. SDL_FreeSurface(gpScreenReal);
  131. return -2;
  132. }
  133. */
  134. #else
  135. //
  136. // Create the screen surface.
  137. //
  138. #if defined (NDS)
  139. gpScreenReal = SDL_SetVideoMode(293, 196, 8, SDL_SWSURFACE | SDL_FULLSCREEN);
  140. #elif defined (__SYMBIAN32__)
  141. #ifdef __S60_5X__
  142. gpScreenReal = SDL_SetVideoMode(640, 360, 8,
  143. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  144. #else
  145. gpScreenReal = SDL_SetVideoMode(320, 240, 8,
  146. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  147. #endif
  148. #elif defined (GEKKO)
  149. gpScreenReal = SDL_SetVideoMode(640, 480, 8,
  150. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  151. #elif defined (PSP)
  152. gpScreenReal = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE | SDL_FULLSCREEN);
  153. #else
  154. gpScreenReal = SDL_SetVideoMode(wScreenWidth, wScreenHeight, 8,
  155. SDL_HWSURFACE | SDL_RESIZABLE | (fFullScreen ? SDL_FULLSCREEN : 0));
  156. #endif
  157. if (gpScreenReal == NULL)
  158. {
  159. //
  160. // Fall back to 640x480 software mode.
  161. //
  162. gpScreenReal = SDL_SetVideoMode(640, 480, 8,
  163. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  164. }
  165. //
  166. // Still fail?
  167. //
  168. if (gpScreenReal == NULL)
  169. {
  170. return -1;
  171. }
  172. //
  173. // Create the screen buffer and the backup screen buffer.
  174. //
  175. gpScreen = SDL_CreateRGBSurface(gpScreenReal->flags & ~SDL_HWSURFACE, 320, 200, 8,
  176. gpScreenReal->format->Rmask, gpScreenReal->format->Gmask,
  177. gpScreenReal->format->Bmask, gpScreenReal->format->Amask);
  178. gpScreenBak = SDL_CreateRGBSurface(gpScreenReal->flags & ~SDL_HWSURFACE, 320, 200, 8,
  179. gpScreenReal->format->Rmask, gpScreenReal->format->Gmask,
  180. gpScreenReal->format->Bmask, gpScreenReal->format->Amask);
  181. //
  182. // Failed?
  183. //
  184. if (gpScreen == NULL || gpScreenBak == NULL)
  185. {
  186. if (gpScreen != NULL)
  187. {
  188. SDL_FreeSurface(gpScreen);
  189. }
  190. if (gpScreenBak != NULL)
  191. {
  192. SDL_FreeSurface(gpScreenBak);
  193. }
  194. SDL_FreeSurface(gpScreenReal);
  195. return -2;
  196. }
  197. #endif
  198. if (fFullScreen)
  199. {
  200. SDL_ShowCursor(FALSE);
  201. }
  202. return 0;
  203. }
  204. VOID
  205. VIDEO_Shutdown(
  206. VOID
  207. )
  208. /*++
  209. Purpose:
  210. Shutdown the video subsystem.
  211. Parameters:
  212. None.
  213. Return value:
  214. None.
  215. --*/
  216. {
  217. if (gpScreen != NULL)
  218. {
  219. SDL_FreeSurface(gpScreen);
  220. }
  221. gpScreen = NULL;
  222. if (gpScreenBak != NULL)
  223. {
  224. SDL_FreeSurface(gpScreenBak);
  225. }
  226. gpScreenBak = NULL;
  227. #if SDL_VERSION_ATLEAST(2,0,0)
  228. // TODO
  229. #else
  230. if (gpScreenReal != NULL)
  231. {
  232. SDL_FreeSurface(gpScreenReal);
  233. }
  234. gpScreenReal = NULL;
  235. #endif
  236. }
  237. VOID
  238. VIDEO_UpdateScreen(
  239. const SDL_Rect *lpRect
  240. )
  241. /*++
  242. Purpose:
  243. Update the screen area specified by lpRect.
  244. Parameters:
  245. [IN] lpRect - Screen area to update.
  246. Return value:
  247. None.
  248. --*/
  249. {
  250. SDL_Rect srcrect, dstrect;
  251. short offset = 240 - 200;
  252. short screenRealHeight = gpScreenReal->h;
  253. short screenRealY = 0;
  254. //
  255. // Lock surface if needed
  256. //
  257. if (SDL_MUSTLOCK(gpScreenReal))
  258. {
  259. if (SDL_LockSurface(gpScreenReal) < 0)
  260. return;
  261. }
  262. if (!bScaleScreen)
  263. {
  264. screenRealHeight -= offset;
  265. screenRealY = offset / 2;
  266. }
  267. if (lpRect != NULL)
  268. {
  269. dstrect.x = (SHORT)((INT)(lpRect->x) * gpScreenReal->w / gpScreen->w);
  270. dstrect.y = (SHORT)((INT)(screenRealY + lpRect->y) * screenRealHeight / gpScreen->h);
  271. dstrect.w = (WORD)((DWORD)(lpRect->w) * gpScreenReal->w / gpScreen->w);
  272. dstrect.h = (WORD)((DWORD)(lpRect->h) * screenRealHeight / gpScreen->h);
  273. SDL_SoftStretch(gpScreen, (SDL_Rect *)lpRect, gpScreenReal, &dstrect);
  274. if (SDL_MUSTLOCK(gpScreenReal))
  275. {
  276. SDL_UnlockSurface(gpScreenReal);
  277. }
  278. SDL_UpdateRect(gpScreenReal, dstrect.x, dstrect.y, dstrect.w, dstrect.h);
  279. }
  280. else if (g_wShakeTime != 0)
  281. {
  282. //
  283. // Shake the screen
  284. //
  285. srcrect.x = 0;
  286. srcrect.y = 0;
  287. srcrect.w = 320;
  288. srcrect.h = 200 - g_wShakeLevel;
  289. dstrect.x = 0;
  290. dstrect.y = screenRealY;
  291. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  292. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  293. if (g_wShakeTime & 1)
  294. {
  295. srcrect.y = g_wShakeLevel;
  296. }
  297. else
  298. {
  299. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  300. }
  301. SDL_SoftStretch(gpScreen, &srcrect, gpScreenReal, &dstrect);
  302. if (g_wShakeTime & 1)
  303. {
  304. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  305. }
  306. else
  307. {
  308. dstrect.y = screenRealY;
  309. }
  310. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  311. SDL_FillRect(gpScreenReal, &dstrect, 0);
  312. if (SDL_MUSTLOCK(gpScreenReal))
  313. {
  314. SDL_UnlockSurface(gpScreenReal);
  315. }
  316. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  317. g_wShakeTime--;
  318. }
  319. else
  320. {
  321. dstrect.x = 0;
  322. dstrect.y = screenRealY;
  323. dstrect.w = gpScreenReal->w;
  324. dstrect.h = screenRealHeight;
  325. SDL_SoftStretch(gpScreen, NULL, gpScreenReal, &dstrect);
  326. if (SDL_MUSTLOCK(gpScreenReal))
  327. {
  328. SDL_UnlockSurface(gpScreenReal);
  329. }
  330. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  331. }
  332. }
  333. VOID
  334. VIDEO_SetPalette(
  335. SDL_Color rgPalette[256]
  336. )
  337. /*++
  338. Purpose:
  339. Set the palette of the screen.
  340. Parameters:
  341. [IN] rgPalette - array of 256 colors.
  342. Return value:
  343. None.
  344. --*/
  345. {
  346. SDL_SetPalette(gpScreenReal, SDL_LOGPAL | SDL_PHYSPAL, rgPalette, 0, 256);
  347. #if (defined (__SYMBIAN32__))
  348. {
  349. static UINT32 time = 0;
  350. if (SDL_GetTicks() - time > 50)
  351. {
  352. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  353. time = SDL_GetTicks();
  354. }
  355. }
  356. #endif
  357. }
  358. VOID
  359. VIDEO_Resize(
  360. INT w,
  361. INT h
  362. )
  363. /*++
  364. Purpose:
  365. This function is called when user resized the window.
  366. Parameters:
  367. [IN] w - width of the window after resizing.
  368. [IN] h - height of the window after resizing.
  369. Return value:
  370. None.
  371. --*/
  372. {
  373. DWORD flags;
  374. PAL_LARGE SDL_Color palette[256];
  375. int i;
  376. //
  377. // Get the original palette.
  378. //
  379. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  380. {
  381. palette[i] = gpScreenReal->format->palette->colors[i];
  382. }
  383. //
  384. // Create the screen surface.
  385. //
  386. flags = gpScreenReal->flags;
  387. SDL_FreeSurface(gpScreenReal);
  388. gpScreenReal = SDL_SetVideoMode(w, h, 8, flags);
  389. if (gpScreenReal == NULL)
  390. {
  391. #ifdef __SYMBIAN32__
  392. #ifdef __S60_5X__
  393. gpScreenReal = SDL_SetVideoMode(640, 360, 8, SDL_SWSURFACE);
  394. #else
  395. gpScreenReal = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE);
  396. #endif
  397. #else
  398. //
  399. // Fall back to 640x480 software windowed mode.
  400. //
  401. gpScreenReal = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
  402. #endif
  403. }
  404. SDL_SetPalette(gpScreenReal, SDL_PHYSPAL | SDL_LOGPAL, palette, 0, i);
  405. VIDEO_UpdateScreen(NULL);
  406. }
  407. SDL_Color *
  408. VIDEO_GetPalette(
  409. VOID
  410. )
  411. /*++
  412. Purpose:
  413. Get the current palette of the screen.
  414. Parameters:
  415. None.
  416. Return value:
  417. Pointer to the current palette.
  418. --*/
  419. {
  420. return gpScreenReal->format->palette->colors;
  421. }
  422. VOID
  423. VIDEO_ToggleScaleScreen(
  424. VOID
  425. )
  426. /*++
  427. Purpose:
  428. Toggle scalescreen mode.
  429. Parameters:
  430. None.
  431. Return value:
  432. None.
  433. --*/
  434. {
  435. #ifdef __SYMBIAN32__
  436. bScaleScreen = !bScaleScreen;
  437. VIDEO_Resize(320, 240);
  438. VIDEO_UpdateScreen(NULL);
  439. #endif
  440. }
  441. VOID
  442. VIDEO_ToggleFullscreen(
  443. VOID
  444. )
  445. /*++
  446. Purpose:
  447. Toggle fullscreen mode.
  448. Parameters:
  449. None.
  450. Return value:
  451. None.
  452. --*/
  453. {
  454. DWORD flags;
  455. PAL_LARGE SDL_Color palette[256];
  456. int i;
  457. //
  458. // Get the original palette.
  459. //
  460. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  461. {
  462. palette[i] = gpScreenReal->format->palette->colors[i];
  463. }
  464. //
  465. // Get the flags of the original screen surface
  466. //
  467. flags = gpScreenReal->flags;
  468. if (flags & SDL_FULLSCREEN)
  469. {
  470. //
  471. // Already in fullscreen mode. Remove the fullscreen flag.
  472. //
  473. flags &= ~SDL_FULLSCREEN;
  474. flags |= SDL_RESIZABLE;
  475. SDL_ShowCursor(TRUE);
  476. }
  477. else
  478. {
  479. //
  480. // Not in fullscreen mode. Set the fullscreen flag.
  481. //
  482. flags |= SDL_FULLSCREEN;
  483. SDL_ShowCursor(FALSE);
  484. }
  485. //
  486. // Free the original screen surface
  487. //
  488. SDL_FreeSurface(gpScreenReal);
  489. //
  490. // ... and create a new one
  491. //
  492. if (g_wInitialWidth == 640 && g_wInitialHeight == 400 && (flags & SDL_FULLSCREEN))
  493. {
  494. gpScreenReal = SDL_SetVideoMode(640, 480, 8, flags);
  495. }
  496. else if (g_wInitialWidth == 640 && g_wInitialHeight == 480 && !(flags & SDL_FULLSCREEN))
  497. {
  498. gpScreenReal = SDL_SetVideoMode(640, 400, 8, flags);
  499. }
  500. else
  501. {
  502. gpScreenReal = SDL_SetVideoMode(g_wInitialWidth, g_wInitialHeight, 8, flags);
  503. }
  504. VIDEO_SetPalette(palette);
  505. //
  506. // Update the screen
  507. //
  508. VIDEO_UpdateScreen(NULL);
  509. }
  510. VOID
  511. VIDEO_SaveScreenshot(
  512. VOID
  513. )
  514. /*++
  515. Purpose:
  516. Save the screenshot of current screen to a BMP file.
  517. Parameters:
  518. None.
  519. Return value:
  520. None.
  521. --*/
  522. {
  523. int iNumBMP = 0;
  524. FILE *fp;
  525. //
  526. // Find a usable BMP filename.
  527. //
  528. for (iNumBMP = 0; iNumBMP <= 9999; iNumBMP++)
  529. {
  530. fp = fopen(va("%sscrn%.4d.bmp", PAL_PREFIX, iNumBMP), "rb");
  531. if (fp == NULL)
  532. {
  533. break;
  534. }
  535. fclose(fp);
  536. }
  537. if (iNumBMP > 9999)
  538. {
  539. return;
  540. }
  541. //
  542. // Save the screenshot.
  543. //
  544. SDL_SaveBMP(gpScreenReal, va("%sscrn%.4d.bmp", PAL_PREFIX, iNumBMP));
  545. }
  546. VOID
  547. VIDEO_BackupScreen(
  548. VOID
  549. )
  550. /*++
  551. Purpose:
  552. Backup the screen buffer.
  553. Parameters:
  554. None.
  555. Return value:
  556. None.
  557. --*/
  558. {
  559. SDL_BlitSurface(gpScreen, NULL, gpScreenBak, NULL);
  560. }
  561. VOID
  562. VIDEO_RestoreScreen(
  563. VOID
  564. )
  565. /*++
  566. Purpose:
  567. Restore the screen buffer which has been saved with VIDEO_BackupScreen().
  568. Parameters:
  569. None.
  570. Return value:
  571. None.
  572. --*/
  573. {
  574. SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);
  575. }
  576. VOID
  577. VIDEO_ShakeScreen(
  578. WORD wShakeTime,
  579. WORD wShakeLevel
  580. )
  581. /*++
  582. Purpose:
  583. Set the screen shake time and level.
  584. Parameters:
  585. [IN] wShakeTime - how many times should we shake the screen.
  586. [IN] wShakeLevel - level of shaking.
  587. Return value:
  588. None.
  589. --*/
  590. {
  591. g_wShakeTime = wShakeTime;
  592. g_wShakeLevel = wShakeLevel;
  593. }
  594. VOID
  595. VIDEO_SwitchScreen(
  596. WORD wSpeed
  597. )
  598. /*++
  599. Purpose:
  600. Switch the screen from the backup screen buffer to the current screen buffer.
  601. NOTE: This will destroy the backup buffer.
  602. Parameters:
  603. [IN] wSpeed - speed of fading (the larger value, the slower).
  604. Return value:
  605. None.
  606. --*/
  607. {
  608. int i, j;
  609. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  610. SDL_Rect dstrect;
  611. short offset = 240 - 200;
  612. short screenRealHeight = gpScreenReal->h;
  613. short screenRealY = 0;
  614. if (!bScaleScreen)
  615. {
  616. screenRealHeight -= offset;
  617. screenRealY = offset / 2;
  618. }
  619. wSpeed++;
  620. wSpeed *= 10;
  621. for (i = 0; i < 6; i++)
  622. {
  623. for (j = rgIndex[i]; j < gpScreen->pitch * gpScreen->h; j += 6)
  624. {
  625. ((LPBYTE)(gpScreenBak->pixels))[j] = ((LPBYTE)(gpScreen->pixels))[j];
  626. }
  627. //
  628. // Draw the backup buffer to the screen
  629. //
  630. dstrect.x = 0;
  631. dstrect.y = screenRealY;
  632. dstrect.w = gpScreenReal->w;
  633. dstrect.h = screenRealHeight;
  634. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  635. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  636. UTIL_Delay(wSpeed);
  637. }
  638. }
  639. VOID
  640. VIDEO_FadeScreen(
  641. WORD wSpeed
  642. )
  643. /*++
  644. Purpose:
  645. Fade from the backup screen buffer to the current screen buffer.
  646. NOTE: This will destroy the backup buffer.
  647. Parameters:
  648. [IN] wSpeed - speed of fading (the larger value, the slower).
  649. Return value:
  650. None.
  651. --*/
  652. {
  653. int i, j, k;
  654. DWORD time;
  655. BYTE a, b;
  656. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  657. SDL_Rect dstrect;
  658. short offset = 240 - 200;
  659. short screenRealHeight = gpScreenReal->h;
  660. short screenRealY = 0;
  661. //
  662. // Lock surface if needed
  663. //
  664. if (SDL_MUSTLOCK(gpScreenReal))
  665. {
  666. if (SDL_LockSurface(gpScreenReal) < 0)
  667. return;
  668. }
  669. if (!bScaleScreen)
  670. {
  671. screenRealHeight -= offset;
  672. screenRealY = offset / 2;
  673. }
  674. time = SDL_GetTicks();
  675. wSpeed++;
  676. wSpeed *= 10;
  677. for (i = 0; i < 12; i++)
  678. {
  679. for (j = 0; j < 6; j++)
  680. {
  681. PAL_ProcessEvent();
  682. while (SDL_GetTicks() <= time)
  683. {
  684. PAL_ProcessEvent();
  685. SDL_Delay(5);
  686. }
  687. time = SDL_GetTicks() + wSpeed;
  688. //
  689. // Blend the pixels in the 2 buffers, and put the result into the
  690. // backup buffer
  691. //
  692. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  693. {
  694. a = ((LPBYTE)(gpScreen->pixels))[k];
  695. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  696. if (i > 0)
  697. {
  698. if ((a & 0x0F) > (b & 0x0F))
  699. {
  700. b++;
  701. }
  702. else if ((a & 0x0F) < (b & 0x0F))
  703. {
  704. b--;
  705. }
  706. }
  707. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  708. }
  709. //
  710. // Draw the backup buffer to the screen
  711. //
  712. if (g_wShakeTime != 0)
  713. {
  714. //
  715. // Shake the screen
  716. //
  717. SDL_Rect srcrect, dstrect;
  718. srcrect.x = 0;
  719. srcrect.y = 0;
  720. srcrect.w = 320;
  721. srcrect.h = 200 - g_wShakeLevel;
  722. dstrect.x = 0;
  723. dstrect.y = screenRealY;
  724. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  725. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  726. if (g_wShakeTime & 1)
  727. {
  728. srcrect.y = g_wShakeLevel;
  729. }
  730. else
  731. {
  732. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  733. }
  734. SDL_SoftStretch(gpScreenBak, &srcrect, gpScreenReal, &dstrect);
  735. if (g_wShakeTime & 1)
  736. {
  737. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  738. }
  739. else
  740. {
  741. dstrect.y = screenRealY;
  742. }
  743. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  744. SDL_FillRect(gpScreenReal, &dstrect, 0);
  745. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  746. g_wShakeTime--;
  747. }
  748. else
  749. {
  750. dstrect.x = 0;
  751. dstrect.y = screenRealY;
  752. dstrect.w = gpScreenReal->w;
  753. dstrect.h = screenRealHeight;
  754. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  755. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  756. }
  757. }
  758. }
  759. if (SDL_MUSTLOCK(gpScreenReal))
  760. {
  761. SDL_UnlockSurface(gpScreenReal);
  762. }
  763. //
  764. // Draw the result buffer to the screen as the final step
  765. //
  766. VIDEO_UpdateScreen(NULL);
  767. }
  768. #if SDL_VERSION_ATLEAST(2,0,0)
  769. /*++
  770. Purpose:
  771. Set the caption of the window. For compatibility with SDL2 only.
  772. Parameters:
  773. [IN] lpszCaption - the new caption of the window.
  774. [IN] lpReserved - not used, for compatibility only.
  775. Return value:
  776. None.
  777. --*/
  778. VOID
  779. SDL_WM_SetCaption(
  780. LPCSTR lpszCaption,
  781. LPVOID lpReserved
  782. )
  783. {
  784. //
  785. // TODO
  786. //
  787. }
  788. #endif