video.c 24 KB

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