video.c 24 KB

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