video.c 26 KB

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