video.c 24 KB

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