video.c 24 KB

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