video.c 23 KB

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