video.c 24 KB

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