video.c 28 KB

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