video.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. #if (defined (__SYMBIAN32__) && !defined (__S60_5X__)) || defined (PSP) || defined (GEKKO)
  38. static BOOL bScaleScreen = FALSE;
  39. #else
  40. static BOOL bScaleScreen = TRUE;
  41. #endif
  42. // Shake times and level
  43. static WORD g_wShakeTime = 0;
  44. static WORD g_wShakeLevel = 0;
  45. #if SDL_VERSION_ATLEAST(2, 0, 0)
  46. #define SDL_SoftStretch SDL_UpperBlit
  47. #endif
  48. static SDL_Texture *VIDEO_CreateTexture(int width, int height)
  49. {
  50. int texture_width, texture_height;
  51. float ratio = (float)width / (float)height;
  52. //
  53. // Check whether to keep the aspect ratio
  54. //
  55. if (gConfig.fKeepAspectRatio && ratio != 1.6f)
  56. {
  57. if (ratio > 1.6f)
  58. {
  59. texture_height = 200;
  60. texture_width = (int)(200 * ratio) & ~0x3;
  61. ratio = (float)height / 200.0f;
  62. }
  63. else
  64. {
  65. texture_width = 320;
  66. texture_height = (int)(320 / ratio) & ~0x3;
  67. ratio = (float)width / 320.0f;
  68. }
  69. WORD w = (WORD)(ratio * 320.0f) & ~0x3;
  70. WORD h = (WORD)(ratio * 200.0f) & ~0x3;
  71. gOverlayRect.x = (width - w) / 2;
  72. gOverlayRect.y = (height - h) / 2;
  73. gOverlayRect.w = w;
  74. gOverlayRect.h = h;
  75. gTextureRect.x = (texture_width - 320) / 2;
  76. gTextureRect.y = (texture_height - 200) / 2;
  77. gTextureRect.w = 320; gTextureRect.h = 200;
  78. PAL_SetTouchBounds(width, height, gOverlayRect);
  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. INT
  96. VIDEO_Startup(
  97. VOID
  98. )
  99. /*++
  100. Purpose:
  101. Initialze the video subsystem.
  102. Parameters:
  103. None.
  104. Return value:
  105. 0 = success, -1 = fail to create the screen surface,
  106. -2 = fail to create screen buffer.
  107. --*/
  108. {
  109. #if SDL_VERSION_ATLEAST(2,0,0)
  110. //
  111. // Before we can render anything, we need a window and a renderer.
  112. //
  113. gpWindow = SDL_CreateWindow("Pal", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  114. gConfig.dwScreenWidth, gConfig.dwScreenHeight, PAL_VIDEO_INIT_FLAGS);
  115. if (gpWindow == NULL)
  116. {
  117. return -1;
  118. }
  119. gpRenderer = SDL_CreateRenderer(gpWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  120. if (gpRenderer == NULL)
  121. {
  122. return -1;
  123. }
  124. #if defined (__IOS__)
  125. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
  126. SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 1);
  127. #endif
  128. //
  129. // Create the screen buffer and the backup screen buffer.
  130. //
  131. gpScreen = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 200, 8, 0, 0, 0, 0);
  132. gpScreenBak = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 200, 8, 0, 0, 0, 0);
  133. gpScreenReal = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 200, 32,
  134. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
  135. //
  136. // Create texture for screen.
  137. //
  138. gpTexture = VIDEO_CreateTexture(gConfig.dwScreenWidth, gConfig.dwScreenHeight);
  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 (__SYMBIAN32__))
  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. #ifdef __SYMBIAN32__
  518. #ifdef __S60_5X__
  519. gpScreenReal = SDL_SetVideoMode(640, 360, 8, SDL_SWSURFACE);
  520. #else
  521. gpScreenReal = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE);
  522. #endif
  523. #else
  524. //
  525. // Fall back to 640x480 software windowed mode.
  526. //
  527. gpScreenReal = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
  528. #endif
  529. }
  530. SDL_SetPalette(gpScreenReal, SDL_PHYSPAL | SDL_LOGPAL, palette, 0, i);
  531. VIDEO_UpdateScreen(NULL);
  532. #endif
  533. }
  534. SDL_Color *
  535. VIDEO_GetPalette(
  536. VOID
  537. )
  538. /*++
  539. Purpose:
  540. Get the current palette of the screen.
  541. Parameters:
  542. None.
  543. Return value:
  544. Pointer to the current palette.
  545. --*/
  546. {
  547. #if SDL_VERSION_ATLEAST(2,0,0)
  548. return gpScreen->format->palette->colors;
  549. #else
  550. return gpScreenReal->format->palette->colors;
  551. #endif
  552. }
  553. VOID
  554. VIDEO_ToggleScaleScreen(
  555. VOID
  556. )
  557. /*++
  558. Purpose:
  559. Toggle scalescreen mode.
  560. Parameters:
  561. None.
  562. Return value:
  563. None.
  564. --*/
  565. {
  566. #ifdef __SYMBIAN32__
  567. bScaleScreen = !bScaleScreen;
  568. VIDEO_Resize(320, 240);
  569. VIDEO_UpdateScreen(NULL);
  570. #endif
  571. }
  572. VOID
  573. VIDEO_ToggleFullscreen(
  574. VOID
  575. )
  576. /*++
  577. Purpose:
  578. Toggle fullscreen mode.
  579. Parameters:
  580. None.
  581. Return value:
  582. None.
  583. --*/
  584. {
  585. #if SDL_VERSION_ATLEAST(2,0,0)
  586. if (gConfig.fFullScreen)
  587. {
  588. SDL_SetWindowFullscreen(gpWindow, 0);
  589. gConfig.fFullScreen = FALSE;
  590. }
  591. else
  592. {
  593. SDL_SetWindowFullscreen(gpWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  594. gConfig.fFullScreen = TRUE;
  595. }
  596. #else
  597. DWORD flags;
  598. PAL_LARGE SDL_Color palette[256];
  599. int i;
  600. //
  601. // Get the original palette.
  602. //
  603. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  604. {
  605. palette[i] = gpScreenReal->format->palette->colors[i];
  606. }
  607. //
  608. // Get the flags of the original screen surface
  609. //
  610. flags = gpScreenReal->flags;
  611. if (flags & SDL_FULLSCREEN)
  612. {
  613. //
  614. // Already in fullscreen mode. Remove the fullscreen flag.
  615. //
  616. flags &= ~SDL_FULLSCREEN;
  617. flags |= SDL_RESIZABLE;
  618. SDL_ShowCursor(TRUE);
  619. }
  620. else
  621. {
  622. //
  623. // Not in fullscreen mode. Set the fullscreen flag.
  624. //
  625. flags |= SDL_FULLSCREEN;
  626. SDL_ShowCursor(FALSE);
  627. }
  628. //
  629. // Free the original screen surface
  630. //
  631. SDL_FreeSurface(gpScreenReal);
  632. //
  633. // ... and create a new one
  634. //
  635. if (gConfig.dwScreenWidth == 640 && gConfig.dwScreenHeight == 400 && (flags & SDL_FULLSCREEN))
  636. {
  637. gpScreenReal = SDL_SetVideoMode(640, 480, 8, flags);
  638. }
  639. else if (gConfig.dwScreenWidth == 640 && gConfig.dwScreenHeight == 480 && !(flags & SDL_FULLSCREEN))
  640. {
  641. gpScreenReal = SDL_SetVideoMode(640, 400, 8, flags);
  642. }
  643. else
  644. {
  645. gpScreenReal = SDL_SetVideoMode(gConfig.dwScreenWidth, gConfig.dwScreenHeight, 8, flags);
  646. }
  647. VIDEO_SetPalette(palette);
  648. //
  649. // Update the screen
  650. //
  651. VIDEO_UpdateScreen(NULL);
  652. #endif
  653. }
  654. VOID
  655. VIDEO_SaveScreenshot(
  656. VOID
  657. )
  658. /*++
  659. Purpose:
  660. Save the screenshot of current screen to a BMP file.
  661. Parameters:
  662. None.
  663. Return value:
  664. None.
  665. --*/
  666. {
  667. char filename[1024];
  668. #ifdef _WIN32
  669. SYSTEMTIME st;
  670. GetLocalTime(&st);
  671. 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);
  672. #else
  673. struct timeval tv;
  674. struct tm *ptm;
  675. gettimeofday(&tv, NULL);
  676. ptm = localtime(&tv.tv_sec);
  677. 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, tv.tv_usec / 1000);
  678. #endif
  679. //
  680. // Save the screenshot.
  681. //
  682. #if SDL_VERSION_ATLEAST(2,0,0)
  683. SDL_SaveBMP(gpScreen, filename);
  684. #else
  685. SDL_SaveBMP(gpScreenReal, filename);
  686. #endif
  687. }
  688. VOID
  689. VIDEO_BackupScreen(
  690. VOID
  691. )
  692. /*++
  693. Purpose:
  694. Backup the screen buffer.
  695. Parameters:
  696. None.
  697. Return value:
  698. None.
  699. --*/
  700. {
  701. SDL_BlitSurface(gpScreen, NULL, gpScreenBak, NULL);
  702. }
  703. VOID
  704. VIDEO_RestoreScreen(
  705. VOID
  706. )
  707. /*++
  708. Purpose:
  709. Restore the screen buffer which has been saved with VIDEO_BackupScreen().
  710. Parameters:
  711. None.
  712. Return value:
  713. None.
  714. --*/
  715. {
  716. SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);
  717. }
  718. VOID
  719. VIDEO_ShakeScreen(
  720. WORD wShakeTime,
  721. WORD wShakeLevel
  722. )
  723. /*++
  724. Purpose:
  725. Set the screen shake time and level.
  726. Parameters:
  727. [IN] wShakeTime - how many times should we shake the screen.
  728. [IN] wShakeLevel - level of shaking.
  729. Return value:
  730. None.
  731. --*/
  732. {
  733. g_wShakeTime = wShakeTime;
  734. g_wShakeLevel = wShakeLevel;
  735. }
  736. VOID
  737. VIDEO_SwitchScreen(
  738. WORD wSpeed
  739. )
  740. /*++
  741. Purpose:
  742. Switch the screen from the backup screen buffer to the current screen buffer.
  743. NOTE: This will destroy the backup buffer.
  744. Parameters:
  745. [IN] wSpeed - speed of fading (the larger value, the slower).
  746. Return value:
  747. None.
  748. --*/
  749. {
  750. int i, j;
  751. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  752. SDL_Rect dstrect;
  753. short offset = 240 - 200;
  754. short screenRealHeight = gpScreenReal->h;
  755. short screenRealY = 0;
  756. if (!bScaleScreen)
  757. {
  758. screenRealHeight -= offset;
  759. screenRealY = offset / 2;
  760. }
  761. wSpeed++;
  762. wSpeed *= 10;
  763. for (i = 0; i < 6; i++)
  764. {
  765. for (j = rgIndex[i]; j < gpScreen->pitch * gpScreen->h; j += 6)
  766. {
  767. ((LPBYTE)(gpScreenBak->pixels))[j] = ((LPBYTE)(gpScreen->pixels))[j];
  768. }
  769. //
  770. // Draw the backup buffer to the screen
  771. //
  772. dstrect.x = 0;
  773. dstrect.y = screenRealY;
  774. dstrect.w = gpScreenReal->w;
  775. dstrect.h = screenRealHeight;
  776. if (SDL_MUSTLOCK(gpScreenReal))
  777. {
  778. if (SDL_LockSurface(gpScreenReal) < 0)
  779. return;
  780. }
  781. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  782. #if SDL_VERSION_ATLEAST(2, 0, 0)
  783. VIDEO_RenderCopy();
  784. #else
  785. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  786. #endif
  787. if (SDL_MUSTLOCK(gpScreenReal))
  788. {
  789. SDL_UnlockSurface(gpScreenReal);
  790. }
  791. UTIL_Delay(wSpeed);
  792. }
  793. }
  794. VOID
  795. VIDEO_FadeScreen(
  796. WORD wSpeed
  797. )
  798. /*++
  799. Purpose:
  800. Fade from the backup screen buffer to the current screen buffer.
  801. NOTE: This will destroy the backup buffer.
  802. Parameters:
  803. [IN] wSpeed - speed of fading (the larger value, the slower).
  804. Return value:
  805. None.
  806. --*/
  807. {
  808. int i, j, k;
  809. DWORD time;
  810. BYTE a, b;
  811. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  812. SDL_Rect dstrect;
  813. short offset = 240 - 200;
  814. short screenRealHeight = gpScreenReal->h;
  815. short screenRealY = 0;
  816. //
  817. // Lock surface if needed
  818. //
  819. if (SDL_MUSTLOCK(gpScreenReal))
  820. {
  821. if (SDL_LockSurface(gpScreenReal) < 0)
  822. return;
  823. }
  824. if (!bScaleScreen)
  825. {
  826. screenRealHeight -= offset;
  827. screenRealY = offset / 2;
  828. }
  829. time = SDL_GetTicks();
  830. wSpeed++;
  831. wSpeed *= 10;
  832. for (i = 0; i < 12; i++)
  833. {
  834. for (j = 0; j < 6; j++)
  835. {
  836. PAL_ProcessEvent();
  837. while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
  838. {
  839. PAL_ProcessEvent();
  840. SDL_Delay(5);
  841. }
  842. time = SDL_GetTicks() + wSpeed;
  843. //
  844. // Blend the pixels in the 2 buffers, and put the result into the
  845. // backup buffer
  846. //
  847. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  848. {
  849. a = ((LPBYTE)(gpScreen->pixels))[k];
  850. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  851. if (i > 0)
  852. {
  853. if ((a & 0x0F) > (b & 0x0F))
  854. {
  855. b++;
  856. }
  857. else if ((a & 0x0F) < (b & 0x0F))
  858. {
  859. b--;
  860. }
  861. }
  862. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  863. }
  864. //
  865. // Draw the backup buffer to the screen
  866. //
  867. if (g_wShakeTime != 0)
  868. {
  869. //
  870. // Shake the screen
  871. //
  872. SDL_Rect srcrect, dstrect;
  873. srcrect.x = 0;
  874. srcrect.y = 0;
  875. srcrect.w = 320;
  876. srcrect.h = 200 - g_wShakeLevel;
  877. dstrect.x = 0;
  878. dstrect.y = screenRealY;
  879. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  880. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  881. if (g_wShakeTime & 1)
  882. {
  883. srcrect.y = g_wShakeLevel;
  884. }
  885. else
  886. {
  887. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  888. }
  889. SDL_SoftStretch(gpScreenBak, &srcrect, gpScreenReal, &dstrect);
  890. if (g_wShakeTime & 1)
  891. {
  892. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  893. }
  894. else
  895. {
  896. dstrect.y = screenRealY;
  897. }
  898. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  899. SDL_FillRect(gpScreenReal, &dstrect, 0);
  900. #if SDL_VERSION_ATLEAST(2, 0, 0)
  901. VIDEO_RenderCopy();
  902. #else
  903. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  904. #endif
  905. g_wShakeTime--;
  906. }
  907. else
  908. {
  909. dstrect.x = 0;
  910. dstrect.y = screenRealY;
  911. dstrect.w = gpScreenReal->w;
  912. dstrect.h = screenRealHeight;
  913. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  914. #if SDL_VERSION_ATLEAST(2, 0, 0)
  915. VIDEO_RenderCopy();
  916. #else
  917. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  918. #endif
  919. }
  920. }
  921. }
  922. if (SDL_MUSTLOCK(gpScreenReal))
  923. {
  924. SDL_UnlockSurface(gpScreenReal);
  925. }
  926. //
  927. // Draw the result buffer to the screen as the final step
  928. //
  929. VIDEO_UpdateScreen(NULL);
  930. }
  931. #if SDL_VERSION_ATLEAST(2,0,0)
  932. /*++
  933. Purpose:
  934. Set the caption of the window. For compatibility with SDL2 only.
  935. Parameters:
  936. [IN] lpszCaption - the new caption of the window.
  937. [IN] lpReserved - not used, for compatibility only.
  938. Return value:
  939. None.
  940. --*/
  941. VOID
  942. SDL_WM_SetCaption(
  943. LPCSTR lpszCaption,
  944. LPVOID lpReserved
  945. )
  946. {
  947. if (gpWindow != NULL)
  948. {
  949. SDL_SetWindowTitle(gpWindow, lpszCaption);
  950. }
  951. }
  952. #endif