video.c 28 KB

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