video.c 21 KB

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