video.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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. SDL_Rect rect;
  473. if (gpTexture)
  474. {
  475. SDL_DestroyTexture(gpTexture);
  476. }
  477. gpTexture = VIDEO_CreateTexture(w, h);
  478. if (gpTexture == NULL)
  479. {
  480. TerminateOnError("Re-creating texture failed on window resize!\n");
  481. }
  482. rect.x = 0;
  483. rect.y = 0;
  484. rect.w = 320;
  485. rect.h = 200;
  486. VIDEO_UpdateScreen(&rect);
  487. #else
  488. DWORD flags;
  489. PAL_LARGE SDL_Color palette[256];
  490. int i;
  491. //
  492. // Get the original palette.
  493. //
  494. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  495. {
  496. palette[i] = gpScreenReal->format->palette->colors[i];
  497. }
  498. //
  499. // Create the screen surface.
  500. //
  501. flags = gpScreenReal->flags;
  502. SDL_FreeSurface(gpScreenReal);
  503. gpScreenReal = SDL_SetVideoMode(w, h, 8, flags);
  504. if (gpScreenReal == NULL)
  505. {
  506. //
  507. // Fall back to software windowed mode in default size.
  508. //
  509. gpScreenReal = SDL_SetVideoMode(PAL_DEFAULT_WINDOW_WIDTH, PAL_DEFAULT_WINDOW_HEIGHT, 8, SDL_SWSURFACE);
  510. }
  511. SDL_SetPalette(gpScreenReal, SDL_PHYSPAL | SDL_LOGPAL, palette, 0, i);
  512. VIDEO_UpdateScreen(NULL);
  513. gpPalette = gpScreenReal->format->palette;
  514. #endif
  515. }
  516. SDL_Color *
  517. VIDEO_GetPalette(
  518. VOID
  519. )
  520. /*++
  521. Purpose:
  522. Get the current palette of the screen.
  523. Parameters:
  524. None.
  525. Return value:
  526. Pointer to the current palette.
  527. --*/
  528. {
  529. return gpPalette->colors;
  530. }
  531. VOID
  532. VIDEO_ToggleScaleScreen(
  533. VOID
  534. )
  535. /*++
  536. Purpose:
  537. Toggle scalescreen mode, only used in some platforms.
  538. Parameters:
  539. None.
  540. Return value:
  541. None.
  542. --*/
  543. {
  544. bScaleScreen = !bScaleScreen;
  545. VIDEO_Resize(PAL_DEFAULT_WINDOW_WIDTH, PAL_DEFAULT_WINDOW_HEIGHT);
  546. VIDEO_UpdateScreen(NULL);
  547. }
  548. VOID
  549. VIDEO_ToggleFullscreen(
  550. VOID
  551. )
  552. /*++
  553. Purpose:
  554. Toggle fullscreen mode.
  555. Parameters:
  556. None.
  557. Return value:
  558. None.
  559. --*/
  560. {
  561. #if SDL_VERSION_ATLEAST(2,0,0)
  562. if (gConfig.fFullScreen)
  563. {
  564. SDL_SetWindowFullscreen(gpWindow, 0);
  565. gConfig.fFullScreen = FALSE;
  566. }
  567. else
  568. {
  569. SDL_SetWindowFullscreen(gpWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  570. gConfig.fFullScreen = TRUE;
  571. }
  572. #else
  573. DWORD flags;
  574. PAL_LARGE SDL_Color palette[256];
  575. int i;
  576. //
  577. // Get the original palette.
  578. //
  579. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  580. {
  581. palette[i] = gpScreenReal->format->palette->colors[i];
  582. }
  583. //
  584. // Get the flags of the original screen surface
  585. //
  586. flags = gpScreenReal->flags;
  587. if (flags & SDL_FULLSCREEN)
  588. {
  589. //
  590. // Already in fullscreen mode. Remove the fullscreen flag.
  591. //
  592. flags &= ~SDL_FULLSCREEN;
  593. flags |= SDL_RESIZABLE;
  594. SDL_ShowCursor(TRUE);
  595. }
  596. else
  597. {
  598. //
  599. // Not in fullscreen mode. Set the fullscreen flag.
  600. //
  601. flags |= SDL_FULLSCREEN;
  602. SDL_ShowCursor(FALSE);
  603. }
  604. //
  605. // Free the original screen surface
  606. //
  607. SDL_FreeSurface(gpScreenReal);
  608. //
  609. // ... and create a new one
  610. //
  611. if (gConfig.dwScreenWidth == 640 && gConfig.dwScreenHeight == 400 && (flags & SDL_FULLSCREEN))
  612. {
  613. gpScreenReal = SDL_SetVideoMode(640, 480, 8, flags);
  614. }
  615. else if (gConfig.dwScreenWidth == 640 && gConfig.dwScreenHeight == 480 && !(flags & SDL_FULLSCREEN))
  616. {
  617. gpScreenReal = SDL_SetVideoMode(640, 400, 8, flags);
  618. }
  619. else
  620. {
  621. gpScreenReal = SDL_SetVideoMode(gConfig.dwScreenWidth, gConfig.dwScreenHeight, 8, flags);
  622. }
  623. VIDEO_SetPalette(palette);
  624. //
  625. // Update the screen
  626. //
  627. VIDEO_UpdateScreen(NULL);
  628. #endif
  629. }
  630. VOID
  631. VIDEO_SaveScreenshot(
  632. VOID
  633. )
  634. /*++
  635. Purpose:
  636. Save the screenshot of current screen to a BMP file.
  637. Parameters:
  638. None.
  639. Return value:
  640. None.
  641. --*/
  642. {
  643. char filename[32];
  644. #ifdef _WIN32
  645. SYSTEMTIME st;
  646. GetLocalTime(&st);
  647. sprintf(filename, "%04d%02d%02d%02d%02d%02d%03d.bmp", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  648. #else
  649. struct timeval tv;
  650. struct tm *ptm;
  651. gettimeofday(&tv, NULL);
  652. ptm = localtime(&tv.tv_sec);
  653. 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));
  654. #endif
  655. //
  656. // Save the screenshot.
  657. //
  658. #if SDL_VERSION_ATLEAST(2,0,0)
  659. SDL_SaveBMP(gpScreen, PAL_CombinePath(0, gConfig.pszSavePath, filename));
  660. #else
  661. SDL_SaveBMP(gpScreenReal, PAL_CombinePath(0, gConfig.pszSavePath, filename));
  662. #endif
  663. }
  664. VOID
  665. VIDEO_ShakeScreen(
  666. WORD wShakeTime,
  667. WORD wShakeLevel
  668. )
  669. /*++
  670. Purpose:
  671. Set the screen shake time and level.
  672. Parameters:
  673. [IN] wShakeTime - how many times should we shake the screen.
  674. [IN] wShakeLevel - level of shaking.
  675. Return value:
  676. None.
  677. --*/
  678. {
  679. g_wShakeTime = wShakeTime;
  680. g_wShakeLevel = wShakeLevel;
  681. }
  682. VOID
  683. VIDEO_SwitchScreen(
  684. WORD wSpeed
  685. )
  686. /*++
  687. Purpose:
  688. Switch the screen from the backup screen buffer to the current screen buffer.
  689. NOTE: This will destroy the backup buffer.
  690. Parameters:
  691. [IN] wSpeed - speed of fading (the larger value, the slower).
  692. Return value:
  693. None.
  694. --*/
  695. {
  696. int i, j;
  697. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  698. SDL_Rect dstrect;
  699. short offset = 240 - 200;
  700. short screenRealHeight = gpScreenReal->h;
  701. short screenRealY = 0;
  702. if (!bScaleScreen)
  703. {
  704. screenRealHeight -= offset;
  705. screenRealY = offset / 2;
  706. }
  707. wSpeed++;
  708. wSpeed *= 10;
  709. for (i = 0; i < 6; i++)
  710. {
  711. for (j = rgIndex[i]; j < gpScreen->pitch * gpScreen->h; j += 6)
  712. {
  713. ((LPBYTE)(gpScreenBak->pixels))[j] = ((LPBYTE)(gpScreen->pixels))[j];
  714. }
  715. //
  716. // Draw the backup buffer to the screen
  717. //
  718. dstrect.x = 0;
  719. dstrect.y = screenRealY;
  720. dstrect.w = gpScreenReal->w;
  721. dstrect.h = screenRealHeight;
  722. if (SDL_MUSTLOCK(gpScreenReal))
  723. {
  724. if (SDL_LockSurface(gpScreenReal) < 0)
  725. return;
  726. }
  727. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  728. #if SDL_VERSION_ATLEAST(2, 0, 0)
  729. VIDEO_RenderCopy();
  730. #else
  731. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  732. #endif
  733. if (SDL_MUSTLOCK(gpScreenReal))
  734. {
  735. SDL_UnlockSurface(gpScreenReal);
  736. }
  737. UTIL_Delay(wSpeed);
  738. }
  739. }
  740. VOID
  741. VIDEO_FadeScreen(
  742. WORD wSpeed
  743. )
  744. /*++
  745. Purpose:
  746. Fade from the backup screen buffer to the current screen buffer.
  747. NOTE: This will destroy the backup buffer.
  748. Parameters:
  749. [IN] wSpeed - speed of fading (the larger value, the slower).
  750. Return value:
  751. None.
  752. --*/
  753. {
  754. int i, j, k;
  755. DWORD time;
  756. BYTE a, b;
  757. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  758. SDL_Rect dstrect;
  759. short offset = 240 - 200;
  760. short screenRealHeight = gpScreenReal->h;
  761. short screenRealY = 0;
  762. //
  763. // Lock surface if needed
  764. //
  765. if (SDL_MUSTLOCK(gpScreenReal))
  766. {
  767. if (SDL_LockSurface(gpScreenReal) < 0)
  768. return;
  769. }
  770. if (!bScaleScreen)
  771. {
  772. screenRealHeight -= offset;
  773. screenRealY = offset / 2;
  774. }
  775. time = SDL_GetTicks();
  776. wSpeed++;
  777. wSpeed *= 10;
  778. for (i = 0; i < 12; i++)
  779. {
  780. for (j = 0; j < 6; j++)
  781. {
  782. PAL_ProcessEvent();
  783. while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
  784. {
  785. PAL_ProcessEvent();
  786. SDL_Delay(5);
  787. }
  788. time = SDL_GetTicks() + wSpeed;
  789. //
  790. // Blend the pixels in the 2 buffers, and put the result into the
  791. // backup buffer
  792. //
  793. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  794. {
  795. a = ((LPBYTE)(gpScreen->pixels))[k];
  796. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  797. if (i > 0)
  798. {
  799. if ((a & 0x0F) > (b & 0x0F))
  800. {
  801. b++;
  802. }
  803. else if ((a & 0x0F) < (b & 0x0F))
  804. {
  805. b--;
  806. }
  807. }
  808. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  809. }
  810. //
  811. // Draw the backup buffer to the screen
  812. //
  813. if (g_wShakeTime != 0)
  814. {
  815. //
  816. // Shake the screen
  817. //
  818. SDL_Rect srcrect, dstrect;
  819. srcrect.x = 0;
  820. srcrect.y = 0;
  821. srcrect.w = 320;
  822. srcrect.h = 200 - g_wShakeLevel;
  823. dstrect.x = 0;
  824. dstrect.y = screenRealY;
  825. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  826. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  827. if (g_wShakeTime & 1)
  828. {
  829. srcrect.y = g_wShakeLevel;
  830. }
  831. else
  832. {
  833. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  834. }
  835. SDL_SoftStretch(gpScreenBak, &srcrect, gpScreenReal, &dstrect);
  836. if (g_wShakeTime & 1)
  837. {
  838. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  839. }
  840. else
  841. {
  842. dstrect.y = screenRealY;
  843. }
  844. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  845. SDL_FillRect(gpScreenReal, &dstrect, 0);
  846. #if SDL_VERSION_ATLEAST(2, 0, 0)
  847. VIDEO_RenderCopy();
  848. #else
  849. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  850. #endif
  851. g_wShakeTime--;
  852. }
  853. else
  854. {
  855. dstrect.x = 0;
  856. dstrect.y = screenRealY;
  857. dstrect.w = gpScreenReal->w;
  858. dstrect.h = screenRealHeight;
  859. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  860. #if SDL_VERSION_ATLEAST(2, 0, 0)
  861. VIDEO_RenderCopy();
  862. #else
  863. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  864. #endif
  865. }
  866. }
  867. }
  868. if (SDL_MUSTLOCK(gpScreenReal))
  869. {
  870. SDL_UnlockSurface(gpScreenReal);
  871. }
  872. //
  873. // Draw the result buffer to the screen as the final step
  874. //
  875. VIDEO_UpdateScreen(NULL);
  876. }
  877. void
  878. VIDEO_SetWindowTitle(
  879. const char* pszTitle
  880. )
  881. /*++
  882. Purpose:
  883. Set the caption of the window.
  884. Parameters:
  885. [IN] pszTitle - the new caption of the window.
  886. Return value:
  887. None.
  888. --*/
  889. {
  890. #if SDL_VERSION_ATLEAST(2,0,0)
  891. SDL_SetWindowTitle(gpWindow, pszTitle);
  892. #else
  893. SDL_WM_SetCaption(pszTitle, NULL);
  894. #endif
  895. }
  896. SDL_Surface *
  897. VIDEO_CreateCompatibleSurface(
  898. SDL_Surface *pSource
  899. )
  900. {
  901. return VIDEO_CreateCompatibleSizedSurface(pSource, NULL);
  902. }
  903. SDL_Surface *
  904. VIDEO_CreateCompatibleSizedSurface(
  905. SDL_Surface *pSource,
  906. const SDL_Rect *pSize
  907. )
  908. /*++
  909. Purpose:
  910. Create a surface that compatible with the source surface.
  911. Parameters:
  912. [IN] pSource - the source surface from which attributes are taken.
  913. [IN] pSize - the size (width & height) of the created surface.
  914. Return value:
  915. None.
  916. --*/
  917. {
  918. //
  919. // Create the surface
  920. //
  921. SDL_Surface *dest = SDL_CreateRGBSurface(pSource->flags,
  922. pSize ? pSize->w : pSource->w,
  923. pSize ? pSize->h : pSource->h,
  924. pSource->format->BitsPerPixel,
  925. pSource->format->Rmask, pSource->format->Gmask,
  926. pSource->format->Bmask, pSource->format->Amask);
  927. if (dest)
  928. {
  929. VIDEO_UpdateSurfacePalette(dest);
  930. }
  931. return dest;
  932. }
  933. SDL_Surface *
  934. VIDEO_DuplicateSurface(
  935. SDL_Surface *pSource,
  936. const SDL_Rect *pRect
  937. )
  938. /*++
  939. Purpose:
  940. Duplicate the selected area from the source surface into new surface.
  941. Parameters:
  942. [IN] pSource - the source surface.
  943. [IN] pRect - the area to be duplicated, NULL for entire surface.
  944. Return value:
  945. None.
  946. --*/
  947. {
  948. SDL_Surface* dest = VIDEO_CreateCompatibleSizedSurface(pSource, pRect);
  949. if (dest)
  950. {
  951. VIDEO_CopySurface(pSource, pRect, dest, NULL);
  952. }
  953. return dest;
  954. }
  955. void
  956. VIDEO_UpdateSurfacePalette(
  957. SDL_Surface *pSurface
  958. )
  959. /*++
  960. Purpose:
  961. Use the global palette to update the palette of pSurface.
  962. Parameters:
  963. [IN] pSurface - the surface whose palette should be updated.
  964. Return value:
  965. None.
  966. --*/
  967. {
  968. #if SDL_VERSION_ATLEAST(2, 0, 0)
  969. SDL_SetSurfacePalette(pSurface, gpPalette);
  970. #else
  971. SDL_SetPalette(pSurface, SDL_PHYSPAL | SDL_LOGPAL, gpPalette->colors, 0, 256);
  972. #endif
  973. }
  974. VOID
  975. VIDEO_DrawSurfaceToScreen(
  976. SDL_Surface *pSurface
  977. )
  978. /*++
  979. Purpose:
  980. Draw a surface directly to screen.
  981. Parameters:
  982. [IN] pSurface - the surface which needs to be drawn to screen.
  983. Return value:
  984. None.
  985. --*/
  986. {
  987. #if SDL_VERSION_ATLEAST(2, 0, 0)
  988. //
  989. // Draw the surface to screen.
  990. //
  991. if (g_bRenderPaused)
  992. {
  993. return;
  994. }
  995. SDL_BlitScaled(pSurface, NULL, gpScreenReal, NULL);
  996. VIDEO_RenderCopy();
  997. #else
  998. SDL_Surface *pCompatSurface;
  999. SDL_Rect rect;
  1000. rect.x = rect.y = 0;
  1001. rect.w = pSurface->w;
  1002. rect.h = pSurface->h;
  1003. pCompatSurface = VIDEO_CreateCompatibleSizedSurface(gpScreenReal, &rect);
  1004. //
  1005. // First convert the surface to compatible format.
  1006. //
  1007. SDL_BlitSurface(pSurface, NULL, pCompatSurface, NULL);
  1008. //
  1009. // Draw the surface to screen.
  1010. //
  1011. SDL_SoftStretch(pCompatSurface, NULL, gpScreenReal, NULL);
  1012. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  1013. SDL_FreeSurface(pCompatSurface);
  1014. #endif
  1015. }