video.c 21 KB

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