video.c 25 KB

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