video.c 24 KB

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