video.c 26 KB

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