video.c 24 KB

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