video.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  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[32];
  638. #ifdef _WIN32
  639. SYSTEMTIME st;
  640. GetLocalTime(&st);
  641. sprintf(filename, "%04d%02d%02d%02d%02d%02d%03d.bmp", 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, "%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));
  648. #endif
  649. //
  650. // Save the screenshot.
  651. //
  652. #if SDL_VERSION_ATLEAST(2,0,0)
  653. SDL_SaveBMP(gpScreen, PAL_CombinePath(0, gConfig.pszSavePath, filename));
  654. #else
  655. SDL_SaveBMP(gpScreenReal, PAL_CombinePath(0, gConfig.pszSavePath, 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. if (g_bRenderPaused)
  986. {
  987. return;
  988. }
  989. SDL_BlitScaled(pSurface, NULL, gpScreenReal, NULL);
  990. VIDEO_RenderCopy();
  991. #else
  992. SDL_Surface *pCompatSurface;
  993. SDL_Rect rect;
  994. rect.x = rect.y = 0;
  995. rect.w = pSurface->w;
  996. rect.h = pSurface->h;
  997. pCompatSurface = VIDEO_CreateCompatibleSizedSurface(gpScreenReal, &rect);
  998. //
  999. // First convert the surface to compatible format.
  1000. //
  1001. SDL_BlitSurface(pSurface, NULL, pCompatSurface, NULL);
  1002. //
  1003. // Draw the surface to screen.
  1004. //
  1005. SDL_SoftStretch(pCompatSurface, NULL, gpScreenReal, NULL);
  1006. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  1007. SDL_FreeSurface(pCompatSurface);
  1008. #endif
  1009. }