video.c 25 KB

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