video.c 27 KB

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