video.c 24 KB

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