video.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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. SDL_Palette *palette = SDL_AllocPalette(256);
  511. if (palette == NULL)
  512. {
  513. return;
  514. }
  515. SDL_SetPaletteColors(palette, rgPalette, 0, 256);
  516. SDL_SetSurfacePalette(gpScreen, palette);
  517. SDL_SetSurfacePalette(gpScreenBak, palette);
  518. //
  519. // HACKHACK: need to invalidate gpScreen->map otherwise the palette
  520. // would not be effective during blit
  521. //
  522. SDL_SetSurfaceColorMod(gpScreen, 0, 0, 0);
  523. SDL_SetSurfaceColorMod(gpScreen, 0xFF, 0xFF, 0xFF);
  524. SDL_SetSurfaceColorMod(gpScreenBak, 0, 0, 0);
  525. SDL_SetSurfaceColorMod(gpScreenBak, 0xFF, 0xFF, 0xFF);
  526. VIDEO_UpdateScreen(NULL);
  527. // The palette should be freed, or memory leak occurs.
  528. SDL_FreePalette(palette);
  529. #else
  530. SDL_SetPalette(gpScreen, SDL_LOGPAL | SDL_PHYSPAL, rgPalette, 0, 256);
  531. SDL_SetPalette(gpScreenBak, SDL_LOGPAL | SDL_PHYSPAL, rgPalette, 0, 256);
  532. SDL_SetPalette(gpScreenReal, SDL_LOGPAL | SDL_PHYSPAL, rgPalette, 0, 256);
  533. #if (defined (__SYMBIAN32__))
  534. {
  535. static UINT32 time = 0;
  536. if (SDL_GetTicks() - time > 50)
  537. {
  538. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  539. time = SDL_GetTicks();
  540. }
  541. }
  542. #endif
  543. #endif
  544. }
  545. VOID
  546. VIDEO_Resize(
  547. INT w,
  548. INT h
  549. )
  550. /*++
  551. Purpose:
  552. This function is called when user resized the window.
  553. Parameters:
  554. [IN] w - width of the window after resizing.
  555. [IN] h - height of the window after resizing.
  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. // Create the screen surface.
  575. //
  576. flags = gpScreenReal->flags;
  577. SDL_FreeSurface(gpScreenReal);
  578. gpScreenReal = SDL_SetVideoMode(w, h, 8, flags);
  579. if (gpScreenReal == NULL)
  580. {
  581. #ifdef __SYMBIAN32__
  582. #ifdef __S60_5X__
  583. gpScreenReal = SDL_SetVideoMode(640, 360, 8, SDL_SWSURFACE);
  584. #else
  585. gpScreenReal = SDL_SetVideoMode(320, 240, 8, SDL_SWSURFACE);
  586. #endif
  587. #else
  588. //
  589. // Fall back to 640x480 software windowed mode.
  590. //
  591. gpScreenReal = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
  592. #endif
  593. }
  594. SDL_SetPalette(gpScreenReal, SDL_PHYSPAL | SDL_LOGPAL, palette, 0, i);
  595. VIDEO_UpdateScreen(NULL);
  596. #endif
  597. }
  598. SDL_Color *
  599. VIDEO_GetPalette(
  600. VOID
  601. )
  602. /*++
  603. Purpose:
  604. Get the current palette of the screen.
  605. Parameters:
  606. None.
  607. Return value:
  608. Pointer to the current palette.
  609. --*/
  610. {
  611. #if SDL_VERSION_ATLEAST(2,0,0)
  612. return gpScreen->format->palette->colors;
  613. #else
  614. return gpScreenReal->format->palette->colors;
  615. #endif
  616. }
  617. VOID
  618. VIDEO_ToggleScaleScreen(
  619. VOID
  620. )
  621. /*++
  622. Purpose:
  623. Toggle scalescreen mode.
  624. Parameters:
  625. None.
  626. Return value:
  627. None.
  628. --*/
  629. {
  630. #ifdef __SYMBIAN32__
  631. bScaleScreen = !bScaleScreen;
  632. VIDEO_Resize(320, 240);
  633. VIDEO_UpdateScreen(NULL);
  634. #endif
  635. }
  636. VOID
  637. VIDEO_ToggleFullscreen(
  638. VOID
  639. )
  640. /*++
  641. Purpose:
  642. Toggle fullscreen mode.
  643. Parameters:
  644. None.
  645. Return value:
  646. None.
  647. --*/
  648. {
  649. #if SDL_VERSION_ATLEAST(2,0,0)
  650. // TODO
  651. #else
  652. DWORD flags;
  653. PAL_LARGE SDL_Color palette[256];
  654. int i;
  655. //
  656. // Get the original palette.
  657. //
  658. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  659. {
  660. palette[i] = gpScreenReal->format->palette->colors[i];
  661. }
  662. //
  663. // Get the flags of the original screen surface
  664. //
  665. flags = gpScreenReal->flags;
  666. if (flags & SDL_FULLSCREEN)
  667. {
  668. //
  669. // Already in fullscreen mode. Remove the fullscreen flag.
  670. //
  671. flags &= ~SDL_FULLSCREEN;
  672. flags |= SDL_RESIZABLE;
  673. SDL_ShowCursor(TRUE);
  674. }
  675. else
  676. {
  677. //
  678. // Not in fullscreen mode. Set the fullscreen flag.
  679. //
  680. flags |= SDL_FULLSCREEN;
  681. SDL_ShowCursor(FALSE);
  682. }
  683. //
  684. // Free the original screen surface
  685. //
  686. SDL_FreeSurface(gpScreenReal);
  687. //
  688. // ... and create a new one
  689. //
  690. if (g_wInitialWidth == 640 && g_wInitialHeight == 400 && (flags & SDL_FULLSCREEN))
  691. {
  692. gpScreenReal = SDL_SetVideoMode(640, 480, 8, flags);
  693. }
  694. else if (g_wInitialWidth == 640 && g_wInitialHeight == 480 && !(flags & SDL_FULLSCREEN))
  695. {
  696. gpScreenReal = SDL_SetVideoMode(640, 400, 8, flags);
  697. }
  698. else
  699. {
  700. gpScreenReal = SDL_SetVideoMode(g_wInitialWidth, g_wInitialHeight, 8, flags);
  701. }
  702. VIDEO_SetPalette(palette);
  703. //
  704. // Update the screen
  705. //
  706. VIDEO_UpdateScreen(NULL);
  707. #endif
  708. }
  709. VOID
  710. VIDEO_SaveScreenshot(
  711. VOID
  712. )
  713. /*++
  714. Purpose:
  715. Save the screenshot of current screen to a BMP file.
  716. Parameters:
  717. None.
  718. Return value:
  719. None.
  720. --*/
  721. {
  722. int iNumBMP = 0;
  723. FILE *fp;
  724. //
  725. // Find a usable BMP filename.
  726. //
  727. for (iNumBMP = 0; iNumBMP <= 9999; iNumBMP++)
  728. {
  729. fp = fopen(va("%sscrn%.4d.bmp", PAL_PREFIX, iNumBMP), "rb");
  730. if (fp == NULL)
  731. {
  732. break;
  733. }
  734. fclose(fp);
  735. }
  736. if (iNumBMP > 9999)
  737. {
  738. return;
  739. }
  740. //
  741. // Save the screenshot.
  742. //
  743. #if SDL_VERSION_ATLEAST(2,0,0)
  744. SDL_SaveBMP(gpScreen, va("%sscrn%.4d.bmp", PAL_PREFIX, iNumBMP));
  745. #else
  746. SDL_SaveBMP(gpScreenReal, va("%sscrn%.4d.bmp", PAL_PREFIX, iNumBMP));
  747. #endif
  748. }
  749. VOID
  750. VIDEO_BackupScreen(
  751. VOID
  752. )
  753. /*++
  754. Purpose:
  755. Backup the screen buffer.
  756. Parameters:
  757. None.
  758. Return value:
  759. None.
  760. --*/
  761. {
  762. SDL_BlitSurface(gpScreen, NULL, gpScreenBak, NULL);
  763. }
  764. VOID
  765. VIDEO_RestoreScreen(
  766. VOID
  767. )
  768. /*++
  769. Purpose:
  770. Restore the screen buffer which has been saved with VIDEO_BackupScreen().
  771. Parameters:
  772. None.
  773. Return value:
  774. None.
  775. --*/
  776. {
  777. SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);
  778. }
  779. VOID
  780. VIDEO_ShakeScreen(
  781. WORD wShakeTime,
  782. WORD wShakeLevel
  783. )
  784. /*++
  785. Purpose:
  786. Set the screen shake time and level.
  787. Parameters:
  788. [IN] wShakeTime - how many times should we shake the screen.
  789. [IN] wShakeLevel - level of shaking.
  790. Return value:
  791. None.
  792. --*/
  793. {
  794. g_wShakeTime = wShakeTime;
  795. g_wShakeLevel = wShakeLevel;
  796. }
  797. VOID
  798. VIDEO_SwitchScreen(
  799. WORD wSpeed
  800. )
  801. /*++
  802. Purpose:
  803. Switch the screen from the backup screen buffer to the current screen buffer.
  804. NOTE: This will destroy the backup buffer.
  805. Parameters:
  806. [IN] wSpeed - speed of fading (the larger value, the slower).
  807. Return value:
  808. None.
  809. --*/
  810. {
  811. int i, j;
  812. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  813. SDL_Rect dstrect;
  814. short offset = 240 - 200;
  815. short screenRealHeight = gpScreenReal->h;
  816. short screenRealY = 0;
  817. if (!bScaleScreen)
  818. {
  819. screenRealHeight -= offset;
  820. screenRealY = offset / 2;
  821. }
  822. wSpeed++;
  823. wSpeed *= 10;
  824. for (i = 0; i < 6; i++)
  825. {
  826. for (j = rgIndex[i]; j < gpScreen->pitch * gpScreen->h; j += 6)
  827. {
  828. ((LPBYTE)(gpScreenBak->pixels))[j] = ((LPBYTE)(gpScreen->pixels))[j];
  829. }
  830. //
  831. // Draw the backup buffer to the screen
  832. //
  833. dstrect.x = 0;
  834. dstrect.y = screenRealY;
  835. dstrect.w = gpScreenReal->w;
  836. dstrect.h = screenRealHeight;
  837. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  838. #if SDL_VERSION_ATLEAST(2, 0, 0)
  839. SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
  840. SDL_RenderCopy(gpRenderer, gpTexture, NULL, NULL);
  841. if (gpTouchOverlay)
  842. {
  843. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  844. }
  845. #ifdef __WINPHONE__
  846. if (gpBackKeyMessage)
  847. {
  848. extern unsigned int g_uiLastBackKeyTime;
  849. if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
  850. {
  851. SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, NULL);
  852. }
  853. }
  854. #endif
  855. SDL_RenderPresent(gpRenderer);
  856. #else
  857. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  858. #endif
  859. UTIL_Delay(wSpeed);
  860. }
  861. }
  862. VOID
  863. VIDEO_FadeScreen(
  864. WORD wSpeed
  865. )
  866. /*++
  867. Purpose:
  868. Fade from the backup screen buffer to the current screen buffer.
  869. NOTE: This will destroy the backup buffer.
  870. Parameters:
  871. [IN] wSpeed - speed of fading (the larger value, the slower).
  872. Return value:
  873. None.
  874. --*/
  875. {
  876. int i, j, k;
  877. DWORD time;
  878. BYTE a, b;
  879. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  880. SDL_Rect dstrect;
  881. short offset = 240 - 200;
  882. short screenRealHeight = gpScreenReal->h;
  883. short screenRealY = 0;
  884. //
  885. // Lock surface if needed
  886. //
  887. if (SDL_MUSTLOCK(gpScreenReal))
  888. {
  889. if (SDL_LockSurface(gpScreenReal) < 0)
  890. return;
  891. }
  892. if (!bScaleScreen)
  893. {
  894. screenRealHeight -= offset;
  895. screenRealY = offset / 2;
  896. }
  897. time = SDL_GetTicks();
  898. wSpeed++;
  899. wSpeed *= 10;
  900. for (i = 0; i < 12; i++)
  901. {
  902. for (j = 0; j < 6; j++)
  903. {
  904. PAL_ProcessEvent();
  905. while (SDL_GetTicks() <= time)
  906. {
  907. PAL_ProcessEvent();
  908. SDL_Delay(5);
  909. }
  910. time = SDL_GetTicks() + wSpeed;
  911. //
  912. // Blend the pixels in the 2 buffers, and put the result into the
  913. // backup buffer
  914. //
  915. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  916. {
  917. a = ((LPBYTE)(gpScreen->pixels))[k];
  918. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  919. if (i > 0)
  920. {
  921. if ((a & 0x0F) > (b & 0x0F))
  922. {
  923. b++;
  924. }
  925. else if ((a & 0x0F) < (b & 0x0F))
  926. {
  927. b--;
  928. }
  929. }
  930. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  931. }
  932. //
  933. // Draw the backup buffer to the screen
  934. //
  935. if (g_wShakeTime != 0)
  936. {
  937. //
  938. // Shake the screen
  939. //
  940. SDL_Rect srcrect, dstrect;
  941. srcrect.x = 0;
  942. srcrect.y = 0;
  943. srcrect.w = 320;
  944. srcrect.h = 200 - g_wShakeLevel;
  945. dstrect.x = 0;
  946. dstrect.y = screenRealY;
  947. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  948. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  949. if (g_wShakeTime & 1)
  950. {
  951. srcrect.y = g_wShakeLevel;
  952. }
  953. else
  954. {
  955. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  956. }
  957. SDL_SoftStretch(gpScreenBak, &srcrect, gpScreenReal, &dstrect);
  958. if (g_wShakeTime & 1)
  959. {
  960. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  961. }
  962. else
  963. {
  964. dstrect.y = screenRealY;
  965. }
  966. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  967. SDL_FillRect(gpScreenReal, &dstrect, 0);
  968. #if SDL_VERSION_ATLEAST(2, 0, 0)
  969. SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
  970. SDL_RenderCopy(gpRenderer, gpTexture, NULL, NULL);
  971. if (gpTouchOverlay)
  972. {
  973. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  974. }
  975. #ifdef __WINPHONE__
  976. if (gpBackKeyMessage)
  977. {
  978. extern unsigned int g_uiLastBackKeyTime;
  979. if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
  980. {
  981. SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, NULL);
  982. }
  983. }
  984. #endif
  985. SDL_RenderPresent(gpRenderer);
  986. #else
  987. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  988. #endif
  989. g_wShakeTime--;
  990. }
  991. else
  992. {
  993. dstrect.x = 0;
  994. dstrect.y = screenRealY;
  995. dstrect.w = gpScreenReal->w;
  996. dstrect.h = screenRealHeight;
  997. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  998. #if SDL_VERSION_ATLEAST(2, 0, 0)
  999. SDL_UpdateTexture(gpTexture, NULL, gpScreenReal->pixels, gpScreenReal->pitch);
  1000. SDL_RenderCopy(gpRenderer, gpTexture, NULL, NULL);
  1001. if (gpTouchOverlay)
  1002. {
  1003. SDL_RenderCopy(gpRenderer, gpTouchOverlay, NULL, NULL);
  1004. }
  1005. #ifdef __WINPHONE__
  1006. if (gpBackKeyMessage)
  1007. {
  1008. extern unsigned int g_uiLastBackKeyTime;
  1009. if (g_uiLastBackKeyTime != 0 && SDL_GetTicks() - g_uiLastBackKeyTime < 800)
  1010. {
  1011. SDL_RenderCopy(gpRenderer, gpBackKeyMessage, NULL, NULL);
  1012. }
  1013. }
  1014. #endif
  1015. SDL_RenderPresent(gpRenderer);
  1016. #else
  1017. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  1018. #endif
  1019. }
  1020. }
  1021. }
  1022. if (SDL_MUSTLOCK(gpScreenReal))
  1023. {
  1024. SDL_UnlockSurface(gpScreenReal);
  1025. }
  1026. //
  1027. // Draw the result buffer to the screen as the final step
  1028. //
  1029. VIDEO_UpdateScreen(NULL);
  1030. }
  1031. #if SDL_VERSION_ATLEAST(2,0,0)
  1032. /*++
  1033. Purpose:
  1034. Set the caption of the window. For compatibility with SDL2 only.
  1035. Parameters:
  1036. [IN] lpszCaption - the new caption of the window.
  1037. [IN] lpReserved - not used, for compatibility only.
  1038. Return value:
  1039. None.
  1040. --*/
  1041. VOID
  1042. SDL_WM_SetCaption(
  1043. LPCSTR lpszCaption,
  1044. LPVOID lpReserved
  1045. )
  1046. {
  1047. if (gpWindow != NULL)
  1048. {
  1049. SDL_SetWindowTitle(gpWindow, lpszCaption);
  1050. }
  1051. }
  1052. #endif