video.c 20 KB

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