video.c 24 KB

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