video.c 26 KB

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