video.c 26 KB

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