video.c 24 KB

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