video.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. // The real screen surface
  27. #if SDL_VERSION_ATLEAST(2,0,0)
  28. static SDL_Window *gpWindow = NULL;
  29. static SDL_Renderer *gpRenderer = NULL;
  30. static SDL_Texture *gpScreenReal = NULL;
  31. static SDL_Texture *gpTouchOverlay = NULL;
  32. #else
  33. static SDL_Surface *gpScreenReal = NULL;
  34. #endif
  35. volatile BOOL g_bRenderPaused = FALSE;
  36. #if !SDL_VERSION_ATLEAST(2, 0, 0)
  37. #if (defined (__SYMBIAN32__) && !defined (__S60_5X__)) || defined (PSP) || defined (GEKKO)
  38. static BOOL bScaleScreen = FALSE;
  39. #else
  40. static BOOL bScaleScreen = TRUE;
  41. #endif
  42. #endif
  43. // Initial screen size
  44. static WORD g_wInitialWidth = 640;
  45. static WORD g_wInitialHeight = 400;
  46. // Shake times and level
  47. static WORD g_wShakeTime = 0;
  48. static WORD g_wShakeLevel = 0;
  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. //
  108. // Failed?
  109. //
  110. if (gpScreen == NULL || gpScreenBak == NULL)
  111. {
  112. if (gpScreen != NULL)
  113. {
  114. SDL_FreeSurface(gpScreen);
  115. gpScreen = NULL;
  116. }
  117. if (gpScreenBak != NULL)
  118. {
  119. SDL_FreeSurface(gpScreenBak);
  120. gpScreenBak = NULL;
  121. }
  122. SDL_DestroyRenderer(gpRenderer);
  123. gpRenderer = NULL;
  124. SDL_DestroyWindow(gpWindow);
  125. gpWindow = NULL;
  126. return -2;
  127. }
  128. overlay = SDL_LoadBMP(PAL_PREFIX "overlay.bmp");
  129. if (overlay != NULL)
  130. {
  131. SDL_SetColorKey(overlay, SDL_RLEACCEL, SDL_MapRGB(overlay->format, 255, 0, 255));
  132. gpTouchOverlay = SDL_CreateTextureFromSurface(gpRenderer, overlay);
  133. SDL_SetTextureAlphaMod(gpTouchOverlay, 120);
  134. SDL_FreeSurface(overlay);
  135. }
  136. #else
  137. //
  138. // Create the screen surface.
  139. //
  140. #if defined (NDS)
  141. gpScreenReal = SDL_SetVideoMode(293, 196, 8, SDL_SWSURFACE | SDL_FULLSCREEN);
  142. #elif defined (__SYMBIAN32__)
  143. #ifdef __S60_5X__
  144. gpScreenReal = SDL_SetVideoMode(640, 360, 8,
  145. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  146. #else
  147. gpScreenReal = SDL_SetVideoMode(320, 240, 8,
  148. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  149. #endif
  150. #elif defined (GEKKO)
  151. gpScreenReal = SDL_SetVideoMode(640, 480, 8,
  152. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  153. #elif defined (PSP)
  154. gpScreenReal = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE | SDL_FULLSCREEN);
  155. #else
  156. gpScreenReal = SDL_SetVideoMode(wScreenWidth, wScreenHeight, 8,
  157. SDL_HWSURFACE | SDL_RESIZABLE | (fFullScreen ? SDL_FULLSCREEN : 0));
  158. #endif
  159. if (gpScreenReal == NULL)
  160. {
  161. //
  162. // Fall back to 640x480 software mode.
  163. //
  164. gpScreenReal = SDL_SetVideoMode(640, 480, 8,
  165. SDL_SWSURFACE | (fFullScreen ? SDL_FULLSCREEN : 0));
  166. }
  167. //
  168. // Still fail?
  169. //
  170. if (gpScreenReal == NULL)
  171. {
  172. return -1;
  173. }
  174. //
  175. // Create the screen buffer and the backup screen buffer.
  176. //
  177. gpScreen = SDL_CreateRGBSurface(gpScreenReal->flags & ~SDL_HWSURFACE, 320, 200, 8,
  178. gpScreenReal->format->Rmask, gpScreenReal->format->Gmask,
  179. gpScreenReal->format->Bmask, gpScreenReal->format->Amask);
  180. gpScreenBak = SDL_CreateRGBSurface(gpScreenReal->flags & ~SDL_HWSURFACE, 320, 200, 8,
  181. gpScreenReal->format->Rmask, gpScreenReal->format->Gmask,
  182. gpScreenReal->format->Bmask, gpScreenReal->format->Amask);
  183. //
  184. // Failed?
  185. //
  186. if (gpScreen == NULL || gpScreenBak == NULL)
  187. {
  188. if (gpScreen != NULL)
  189. {
  190. SDL_FreeSurface(gpScreen);
  191. gpScreen = NULL;
  192. }
  193. if (gpScreenBak != NULL)
  194. {
  195. SDL_FreeSurface(gpScreenBak);
  196. gpScreenBak = NULL;
  197. }
  198. SDL_FreeSurface(gpScreenReal);
  199. gpScreenReal = NULL;
  200. return -2;
  201. }
  202. #endif
  203. if (fFullScreen)
  204. {
  205. SDL_ShowCursor(FALSE);
  206. }
  207. return 0;
  208. }
  209. VOID
  210. VIDEO_Shutdown(
  211. VOID
  212. )
  213. /*++
  214. Purpose:
  215. Shutdown the video subsystem.
  216. Parameters:
  217. None.
  218. Return value:
  219. None.
  220. --*/
  221. {
  222. if (gpScreen != NULL)
  223. {
  224. SDL_FreeSurface(gpScreen);
  225. }
  226. gpScreen = NULL;
  227. if (gpScreenBak != NULL)
  228. {
  229. SDL_FreeSurface(gpScreenBak);
  230. }
  231. gpScreenBak = NULL;
  232. #if SDL_VERSION_ATLEAST(2,0,0)
  233. if (gpTouchOverlay)
  234. {
  235. SDL_DestroyTexture(gpTouchOverlay);
  236. }
  237. gpTouchOverlay = NULL;
  238. if (gpRenderer)
  239. {
  240. SDL_DestroyRenderer(gpRenderer);
  241. }
  242. gpRenderer = NULL;
  243. if (gpWindow)
  244. {
  245. SDL_DestroyWindow(gpWindow);
  246. }
  247. gpWindow = NULL;
  248. #else
  249. if (gpScreenReal != NULL)
  250. {
  251. SDL_FreeSurface(gpScreenReal);
  252. }
  253. gpScreenReal = NULL;
  254. #endif
  255. }
  256. VOID
  257. VIDEO_UpdateScreen(
  258. const SDL_Rect *lpRect
  259. )
  260. /*++
  261. Purpose:
  262. Update the screen area specified by lpRect.
  263. Parameters:
  264. [IN] lpRect - Screen area to update.
  265. Return value:
  266. None.
  267. --*/
  268. {
  269. #if SDL_VERSION_ATLEAST(2,0,0)
  270. if (!g_bRenderPaused)
  271. {
  272. SDL_Texture *pTexture = SDL_CreateTextureFromSurface(gpRenderer, gpScreen);
  273. if (lpRect != NULL)
  274. {
  275. SDL_Rect viewport, dstrect;
  276. SDL_RenderGetViewport(gpRenderer, &viewport);
  277. dstrect.x = (SHORT)((INT)(lpRect->x) * viewport.w / gpScreen->w);
  278. dstrect.y = (SHORT)((INT)(lpRect->y) * viewport.h / gpScreen->h);
  279. dstrect.w = (WORD)((DWORD)(lpRect->w) * viewport.w / gpScreen->w);
  280. dstrect.h = (WORD)((DWORD)(lpRect->h) * viewport.h / gpScreen->h);
  281. SDL_RenderCopy(gpRenderer, pTexture, lpRect, &dstrect);
  282. if (gpTouchOverlay)
  283. {
  284. SDL_RenderCopy(gpRenderer, gpTouchOverlay, lpRect, &dstrect);
  285. }
  286. }
  287. else if (g_wShakeTime != 0)
  288. {
  289. //
  290. // Shake the screen
  291. //
  292. SDL_Rect srcrect, dstrect, viewport;
  293. SDL_RenderGetViewport(gpRenderer, &viewport);
  294. srcrect.x = 0;
  295. srcrect.y = 0;
  296. srcrect.w = 320;
  297. srcrect.h = 200 - g_wShakeLevel;
  298. dstrect.x = 0;
  299. dstrect.y = 0;
  300. dstrect.w = 320 * viewport.w / gpScreen->w;
  301. dstrect.h = (200 - g_wShakeLevel) * viewport.h / gpScreen->h;
  302. if (g_wShakeTime & 1)
  303. {
  304. srcrect.y = g_wShakeLevel;
  305. }
  306. else
  307. {
  308. dstrect.y = g_wShakeLevel * viewport.h / gpScreen->h;
  309. }
  310. SDL_RenderClear(gpRenderer);
  311. SDL_RenderCopy(gpRenderer, pTexture, &srcrect, &dstrect);
  312. if (gpTouchOverlay)
  313. {
  314. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  315. }
  316. g_wShakeTime--;
  317. }
  318. else
  319. {
  320. SDL_RenderCopy(gpRenderer, pTexture, NULL, NULL);
  321. if (gpTouchOverlay)
  322. {
  323. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  324. }
  325. }
  326. SDL_RenderPresent(gpRenderer);
  327. SDL_DestroyTexture(pTexture);
  328. }
  329. #else
  330. SDL_Rect srcrect, dstrect;
  331. short offset = 240 - 200;
  332. short screenRealHeight = gpScreenReal->h;
  333. short screenRealY = 0;
  334. //
  335. // Lock surface if needed
  336. //
  337. if (SDL_MUSTLOCK(gpScreenReal))
  338. {
  339. if (SDL_LockSurface(gpScreenReal) < 0)
  340. return;
  341. }
  342. if (!bScaleScreen)
  343. {
  344. screenRealHeight -= offset;
  345. screenRealY = offset / 2;
  346. }
  347. if (lpRect != NULL)
  348. {
  349. dstrect.x = (SHORT)((INT)(lpRect->x) * gpScreenReal->w / gpScreen->w);
  350. dstrect.y = (SHORT)((INT)(screenRealY + lpRect->y) * screenRealHeight / gpScreen->h);
  351. dstrect.w = (WORD)((DWORD)(lpRect->w) * gpScreenReal->w / gpScreen->w);
  352. dstrect.h = (WORD)((DWORD)(lpRect->h) * screenRealHeight / gpScreen->h);
  353. SDL_SoftStretch(gpScreen, (SDL_Rect *)lpRect, gpScreenReal, &dstrect);
  354. if (SDL_MUSTLOCK(gpScreenReal))
  355. {
  356. SDL_UnlockSurface(gpScreenReal);
  357. }
  358. SDL_UpdateRect(gpScreenReal, dstrect.x, dstrect.y, dstrect.w, dstrect.h);
  359. }
  360. else if (g_wShakeTime != 0)
  361. {
  362. //
  363. // Shake the screen
  364. //
  365. srcrect.x = 0;
  366. srcrect.y = 0;
  367. srcrect.w = 320;
  368. srcrect.h = 200 - g_wShakeLevel;
  369. dstrect.x = 0;
  370. dstrect.y = screenRealY;
  371. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  372. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  373. if (g_wShakeTime & 1)
  374. {
  375. srcrect.y = g_wShakeLevel;
  376. }
  377. else
  378. {
  379. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  380. }
  381. SDL_SoftStretch(gpScreen, &srcrect, gpScreenReal, &dstrect);
  382. if (g_wShakeTime & 1)
  383. {
  384. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  385. }
  386. else
  387. {
  388. dstrect.y = screenRealY;
  389. }
  390. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  391. SDL_FillRect(gpScreenReal, &dstrect, 0);
  392. if (SDL_MUSTLOCK(gpScreenReal))
  393. {
  394. SDL_UnlockSurface(gpScreenReal);
  395. }
  396. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  397. g_wShakeTime--;
  398. }
  399. else
  400. {
  401. dstrect.x = 0;
  402. dstrect.y = screenRealY;
  403. dstrect.w = gpScreenReal->w;
  404. dstrect.h = screenRealHeight;
  405. SDL_SoftStretch(gpScreen, NULL, gpScreenReal, &dstrect);
  406. if (SDL_MUSTLOCK(gpScreenReal))
  407. {
  408. SDL_UnlockSurface(gpScreenReal);
  409. }
  410. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  411. }
  412. #endif
  413. }
  414. VOID
  415. VIDEO_SetPalette(
  416. SDL_Color rgPalette[256]
  417. )
  418. /*++
  419. Purpose:
  420. Set the palette of the screen.
  421. Parameters:
  422. [IN] rgPalette - array of 256 colors.
  423. Return value:
  424. None.
  425. --*/
  426. {
  427. #if SDL_VERSION_ATLEAST(2,0,0)
  428. int i;
  429. SDL_Palette *palette = SDL_AllocPalette(256);
  430. if (palette == NULL)
  431. {
  432. return;
  433. }
  434. for (i = 0; i < 256; i++)
  435. {
  436. palette->colors[i] = rgPalette[i];
  437. }
  438. SDL_SetSurfacePalette(gpScreen, palette);
  439. SDL_SetSurfacePalette(gpScreenBak, palette);
  440. VIDEO_UpdateScreen(NULL);
  441. #else
  442. SDL_SetPalette(gpScreen, 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 (g_wInitialWidth == 640 && g_wInitialHeight == 400 && (flags & SDL_FULLSCREEN))
  602. {
  603. gpScreenReal = SDL_SetVideoMode(640, 480, 8, flags);
  604. }
  605. else if (g_wInitialWidth == 640 && g_wInitialHeight == 480 && !(flags & SDL_FULLSCREEN))
  606. {
  607. gpScreenReal = SDL_SetVideoMode(640, 400, 8, flags);
  608. }
  609. else
  610. {
  611. gpScreenReal = SDL_SetVideoMode(g_wInitialWidth, g_wInitialHeight, 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. #if SDL_VERSION_ATLEAST(2,0,0)
  723. int i, j;
  724. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  725. SDL_Texture *pTexture;
  726. if (g_bRenderPaused)
  727. {
  728. return;
  729. }
  730. wSpeed++;
  731. wSpeed *= 10;
  732. for (i = 0; i < 6; i++)
  733. {
  734. for (j = rgIndex[i]; j < gpScreen->pitch * gpScreen->h; j += 6)
  735. {
  736. ((LPBYTE)(gpScreenBak->pixels))[j] = ((LPBYTE)(gpScreen->pixels))[j];
  737. }
  738. //
  739. // Draw the backup buffer to the screen
  740. //
  741. pTexture = SDL_CreateTextureFromSurface(gpRenderer, gpScreenBak);
  742. SDL_RenderClear(gpRenderer);
  743. SDL_RenderCopy(gpRenderer, pTexture, NULL, NULL);
  744. if (gpTouchOverlay)
  745. {
  746. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  747. }
  748. SDL_RenderPresent(gpRenderer);
  749. SDL_DestroyTexture(pTexture);
  750. UTIL_Delay(wSpeed);
  751. }
  752. #else
  753. int i, j;
  754. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  755. SDL_Rect dstrect;
  756. short offset = 240 - 200;
  757. short screenRealHeight = gpScreenReal->h;
  758. short screenRealY = 0;
  759. if (!bScaleScreen)
  760. {
  761. screenRealHeight -= offset;
  762. screenRealY = offset / 2;
  763. }
  764. wSpeed++;
  765. wSpeed *= 10;
  766. for (i = 0; i < 6; i++)
  767. {
  768. for (j = rgIndex[i]; j < gpScreen->pitch * gpScreen->h; j += 6)
  769. {
  770. ((LPBYTE)(gpScreenBak->pixels))[j] = ((LPBYTE)(gpScreen->pixels))[j];
  771. }
  772. //
  773. // Draw the backup buffer to the screen
  774. //
  775. dstrect.x = 0;
  776. dstrect.y = screenRealY;
  777. dstrect.w = gpScreenReal->w;
  778. dstrect.h = screenRealHeight;
  779. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  780. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  781. UTIL_Delay(wSpeed);
  782. }
  783. #endif
  784. }
  785. VOID
  786. VIDEO_FadeScreen(
  787. WORD wSpeed
  788. )
  789. /*++
  790. Purpose:
  791. Fade from the backup screen buffer to the current screen buffer.
  792. NOTE: This will destroy the backup buffer.
  793. Parameters:
  794. [IN] wSpeed - speed of fading (the larger value, the slower).
  795. Return value:
  796. None.
  797. --*/
  798. {
  799. #if SDL_VERSION_ATLEAST(2,0,0)
  800. int i, j, k;
  801. DWORD time;
  802. BYTE a, b;
  803. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  804. SDL_Rect viewport;
  805. SDL_Texture *pTexture;
  806. if (g_bRenderPaused)
  807. {
  808. return;
  809. }
  810. SDL_RenderGetViewport(gpRenderer, &viewport);
  811. time = SDL_GetTicks();
  812. wSpeed++;
  813. wSpeed *= 10;
  814. for (i = 0; i < 12; i++)
  815. {
  816. for (j = 0; j < 6; j++)
  817. {
  818. PAL_ProcessEvent();
  819. while (SDL_GetTicks() <= time)
  820. {
  821. PAL_ProcessEvent();
  822. SDL_Delay(5);
  823. }
  824. time = SDL_GetTicks() + wSpeed;
  825. //
  826. // Blend the pixels in the 2 buffers, and put the result into the
  827. // backup buffer
  828. //
  829. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  830. {
  831. a = ((LPBYTE)(gpScreen->pixels))[k];
  832. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  833. if (i > 0)
  834. {
  835. if ((a & 0x0F) > (b & 0x0F))
  836. {
  837. b++;
  838. }
  839. else if ((a & 0x0F) < (b & 0x0F))
  840. {
  841. b--;
  842. }
  843. }
  844. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  845. }
  846. //
  847. // Draw the backup buffer to the screen
  848. //
  849. if (g_wShakeTime != 0)
  850. {
  851. //
  852. // Shake the screen
  853. //
  854. SDL_Rect srcrect, dstrect;
  855. srcrect.x = 0;
  856. srcrect.y = 0;
  857. srcrect.w = 320;
  858. srcrect.h = 200 - g_wShakeLevel;
  859. dstrect.x = 0;
  860. dstrect.y = 0;
  861. dstrect.w = 320 * viewport.w / gpScreen->w;
  862. dstrect.h = (200 - g_wShakeLevel) * viewport.h / gpScreen->h;
  863. if (g_wShakeTime & 1)
  864. {
  865. srcrect.y = g_wShakeLevel;
  866. }
  867. else
  868. {
  869. dstrect.y = g_wShakeLevel * viewport.h / gpScreen->h;
  870. }
  871. pTexture = SDL_CreateTextureFromSurface(gpRenderer, gpScreenBak);
  872. SDL_RenderClear(gpRenderer);
  873. SDL_RenderCopy(gpRenderer, pTexture, &srcrect, &dstrect);
  874. if (gpTouchOverlay)
  875. {
  876. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  877. }
  878. SDL_RenderPresent(gpRenderer);
  879. SDL_DestroyTexture(pTexture);
  880. g_wShakeTime--;
  881. }
  882. else
  883. {
  884. pTexture = SDL_CreateTextureFromSurface(gpRenderer, gpScreenBak);
  885. SDL_RenderCopy(gpRenderer, pTexture, NULL, NULL);
  886. if (gpTouchOverlay)
  887. {
  888. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  889. }
  890. SDL_RenderPresent(gpRenderer);
  891. SDL_DestroyTexture(pTexture);
  892. }
  893. }
  894. }
  895. //
  896. // Draw the result buffer to the screen as the final step
  897. //
  898. VIDEO_UpdateScreen(NULL);
  899. #else
  900. int i, j, k;
  901. DWORD time;
  902. BYTE a, b;
  903. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  904. SDL_Rect dstrect;
  905. short offset = 240 - 200;
  906. short screenRealHeight = gpScreenReal->h;
  907. short screenRealY = 0;
  908. //
  909. // Lock surface if needed
  910. //
  911. if (SDL_MUSTLOCK(gpScreenReal))
  912. {
  913. if (SDL_LockSurface(gpScreenReal) < 0)
  914. return;
  915. }
  916. if (!bScaleScreen)
  917. {
  918. screenRealHeight -= offset;
  919. screenRealY = offset / 2;
  920. }
  921. time = SDL_GetTicks();
  922. wSpeed++;
  923. wSpeed *= 10;
  924. for (i = 0; i < 12; i++)
  925. {
  926. for (j = 0; j < 6; j++)
  927. {
  928. PAL_ProcessEvent();
  929. while (SDL_GetTicks() <= time)
  930. {
  931. PAL_ProcessEvent();
  932. SDL_Delay(5);
  933. }
  934. time = SDL_GetTicks() + wSpeed;
  935. //
  936. // Blend the pixels in the 2 buffers, and put the result into the
  937. // backup buffer
  938. //
  939. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  940. {
  941. a = ((LPBYTE)(gpScreen->pixels))[k];
  942. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  943. if (i > 0)
  944. {
  945. if ((a & 0x0F) > (b & 0x0F))
  946. {
  947. b++;
  948. }
  949. else if ((a & 0x0F) < (b & 0x0F))
  950. {
  951. b--;
  952. }
  953. }
  954. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  955. }
  956. //
  957. // Draw the backup buffer to the screen
  958. //
  959. if (g_wShakeTime != 0)
  960. {
  961. //
  962. // Shake the screen
  963. //
  964. SDL_Rect srcrect, dstrect;
  965. srcrect.x = 0;
  966. srcrect.y = 0;
  967. srcrect.w = 320;
  968. srcrect.h = 200 - g_wShakeLevel;
  969. dstrect.x = 0;
  970. dstrect.y = screenRealY;
  971. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  972. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  973. if (g_wShakeTime & 1)
  974. {
  975. srcrect.y = g_wShakeLevel;
  976. }
  977. else
  978. {
  979. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  980. }
  981. SDL_SoftStretch(gpScreenBak, &srcrect, gpScreenReal, &dstrect);
  982. if (g_wShakeTime & 1)
  983. {
  984. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  985. }
  986. else
  987. {
  988. dstrect.y = screenRealY;
  989. }
  990. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  991. SDL_FillRect(gpScreenReal, &dstrect, 0);
  992. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  993. g_wShakeTime--;
  994. }
  995. else
  996. {
  997. dstrect.x = 0;
  998. dstrect.y = screenRealY;
  999. dstrect.w = gpScreenReal->w;
  1000. dstrect.h = screenRealHeight;
  1001. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  1002. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  1003. }
  1004. }
  1005. }
  1006. if (SDL_MUSTLOCK(gpScreenReal))
  1007. {
  1008. SDL_UnlockSurface(gpScreenReal);
  1009. }
  1010. //
  1011. // Draw the result buffer to the screen as the final step
  1012. //
  1013. VIDEO_UpdateScreen(NULL);
  1014. #endif
  1015. }
  1016. #if SDL_VERSION_ATLEAST(2,0,0)
  1017. /*++
  1018. Purpose:
  1019. Set the caption of the window. For compatibility with SDL2 only.
  1020. Parameters:
  1021. [IN] lpszCaption - the new caption of the window.
  1022. [IN] lpReserved - not used, for compatibility only.
  1023. Return value:
  1024. None.
  1025. --*/
  1026. VOID
  1027. SDL_WM_SetCaption(
  1028. LPCSTR lpszCaption,
  1029. LPVOID lpReserved
  1030. )
  1031. {
  1032. if (gpWindow != NULL)
  1033. {
  1034. SDL_SetWindowTitle(gpWindow, lpszCaption);
  1035. }
  1036. }
  1037. #endif