video.c 21 KB

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