video.c 26 KB

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