video.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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)
  472. {
  473. SDL_DestroyTexture(gpTexture);
  474. }
  475. gpTexture = VIDEO_CreateTexture(w, h);
  476. if (gpTexture == NULL)
  477. {
  478. TerminateOnError("Re-creating texture failed on window resize!\n");
  479. }
  480. #else
  481. DWORD flags;
  482. PAL_LARGE SDL_Color palette[256];
  483. int i;
  484. //
  485. // Get the original palette.
  486. //
  487. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  488. {
  489. palette[i] = gpScreenReal->format->palette->colors[i];
  490. }
  491. //
  492. // Create the screen surface.
  493. //
  494. flags = gpScreenReal->flags;
  495. SDL_FreeSurface(gpScreenReal);
  496. gpScreenReal = SDL_SetVideoMode(w, h, 8, flags);
  497. if (gpScreenReal == NULL)
  498. {
  499. //
  500. // Fall back to software windowed mode in default size.
  501. //
  502. gpScreenReal = SDL_SetVideoMode(PAL_DEFAULT_WINDOW_WIDTH, PAL_DEFAULT_WINDOW_HEIGHT, 8, SDL_SWSURFACE);
  503. }
  504. SDL_SetPalette(gpScreenReal, SDL_PHYSPAL | SDL_LOGPAL, palette, 0, i);
  505. VIDEO_UpdateScreen(NULL);
  506. gpPalette = gpScreenReal->format->palette;
  507. #endif
  508. }
  509. SDL_Color *
  510. VIDEO_GetPalette(
  511. VOID
  512. )
  513. /*++
  514. Purpose:
  515. Get the current palette of the screen.
  516. Parameters:
  517. None.
  518. Return value:
  519. Pointer to the current palette.
  520. --*/
  521. {
  522. return gpPalette->colors;
  523. }
  524. VOID
  525. VIDEO_ToggleScaleScreen(
  526. VOID
  527. )
  528. /*++
  529. Purpose:
  530. Toggle scalescreen mode, only used in some platforms.
  531. Parameters:
  532. None.
  533. Return value:
  534. None.
  535. --*/
  536. {
  537. bScaleScreen = !bScaleScreen;
  538. VIDEO_Resize(PAL_DEFAULT_WINDOW_WIDTH, PAL_DEFAULT_WINDOW_HEIGHT);
  539. VIDEO_UpdateScreen(NULL);
  540. }
  541. VOID
  542. VIDEO_ToggleFullscreen(
  543. VOID
  544. )
  545. /*++
  546. Purpose:
  547. Toggle fullscreen mode.
  548. Parameters:
  549. None.
  550. Return value:
  551. None.
  552. --*/
  553. {
  554. #if SDL_VERSION_ATLEAST(2,0,0)
  555. if (gConfig.fFullScreen)
  556. {
  557. SDL_SetWindowFullscreen(gpWindow, 0);
  558. gConfig.fFullScreen = FALSE;
  559. }
  560. else
  561. {
  562. SDL_SetWindowFullscreen(gpWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  563. gConfig.fFullScreen = TRUE;
  564. }
  565. #else
  566. DWORD flags;
  567. PAL_LARGE SDL_Color palette[256];
  568. int i;
  569. //
  570. // Get the original palette.
  571. //
  572. for (i = 0; i < gpScreenReal->format->palette->ncolors; i++)
  573. {
  574. palette[i] = gpScreenReal->format->palette->colors[i];
  575. }
  576. //
  577. // Get the flags of the original screen surface
  578. //
  579. flags = gpScreenReal->flags;
  580. if (flags & SDL_FULLSCREEN)
  581. {
  582. //
  583. // Already in fullscreen mode. Remove the fullscreen flag.
  584. //
  585. flags &= ~SDL_FULLSCREEN;
  586. flags |= SDL_RESIZABLE;
  587. SDL_ShowCursor(TRUE);
  588. }
  589. else
  590. {
  591. //
  592. // Not in fullscreen mode. Set the fullscreen flag.
  593. //
  594. flags |= SDL_FULLSCREEN;
  595. SDL_ShowCursor(FALSE);
  596. }
  597. //
  598. // Free the original screen surface
  599. //
  600. SDL_FreeSurface(gpScreenReal);
  601. //
  602. // ... and create a new one
  603. //
  604. if (gConfig.dwScreenWidth == 640 && gConfig.dwScreenHeight == 400 && (flags & SDL_FULLSCREEN))
  605. {
  606. gpScreenReal = SDL_SetVideoMode(640, 480, 8, flags);
  607. }
  608. else if (gConfig.dwScreenWidth == 640 && gConfig.dwScreenHeight == 480 && !(flags & SDL_FULLSCREEN))
  609. {
  610. gpScreenReal = SDL_SetVideoMode(640, 400, 8, flags);
  611. }
  612. else
  613. {
  614. gpScreenReal = SDL_SetVideoMode(gConfig.dwScreenWidth, gConfig.dwScreenHeight, 8, flags);
  615. }
  616. VIDEO_SetPalette(palette);
  617. //
  618. // Update the screen
  619. //
  620. VIDEO_UpdateScreen(NULL);
  621. #endif
  622. }
  623. VOID
  624. VIDEO_SaveScreenshot(
  625. VOID
  626. )
  627. /*++
  628. Purpose:
  629. Save the screenshot of current screen to a BMP file.
  630. Parameters:
  631. None.
  632. Return value:
  633. None.
  634. --*/
  635. {
  636. char filename[1024];
  637. #ifdef _WIN32
  638. SYSTEMTIME st;
  639. GetLocalTime(&st);
  640. 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);
  641. #else
  642. struct timeval tv;
  643. struct tm *ptm;
  644. gettimeofday(&tv, NULL);
  645. ptm = localtime(&tv.tv_sec);
  646. 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));
  647. #endif
  648. //
  649. // Save the screenshot.
  650. //
  651. #if SDL_VERSION_ATLEAST(2,0,0)
  652. SDL_SaveBMP(gpScreen, filename);
  653. #else
  654. SDL_SaveBMP(gpScreenReal, filename);
  655. #endif
  656. }
  657. VOID
  658. VIDEO_ShakeScreen(
  659. WORD wShakeTime,
  660. WORD wShakeLevel
  661. )
  662. /*++
  663. Purpose:
  664. Set the screen shake time and level.
  665. Parameters:
  666. [IN] wShakeTime - how many times should we shake the screen.
  667. [IN] wShakeLevel - level of shaking.
  668. Return value:
  669. None.
  670. --*/
  671. {
  672. g_wShakeTime = wShakeTime;
  673. g_wShakeLevel = wShakeLevel;
  674. }
  675. VOID
  676. VIDEO_SwitchScreen(
  677. WORD wSpeed
  678. )
  679. /*++
  680. Purpose:
  681. Switch the screen from the backup screen buffer to the current screen buffer.
  682. NOTE: This will destroy the backup buffer.
  683. Parameters:
  684. [IN] wSpeed - speed of fading (the larger value, the slower).
  685. Return value:
  686. None.
  687. --*/
  688. {
  689. int i, j;
  690. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  691. SDL_Rect dstrect;
  692. short offset = 240 - 200;
  693. short screenRealHeight = gpScreenReal->h;
  694. short screenRealY = 0;
  695. if (!bScaleScreen)
  696. {
  697. screenRealHeight -= offset;
  698. screenRealY = offset / 2;
  699. }
  700. wSpeed++;
  701. wSpeed *= 10;
  702. for (i = 0; i < 6; i++)
  703. {
  704. for (j = rgIndex[i]; j < gpScreen->pitch * gpScreen->h; j += 6)
  705. {
  706. ((LPBYTE)(gpScreenBak->pixels))[j] = ((LPBYTE)(gpScreen->pixels))[j];
  707. }
  708. //
  709. // Draw the backup buffer to the screen
  710. //
  711. dstrect.x = 0;
  712. dstrect.y = screenRealY;
  713. dstrect.w = gpScreenReal->w;
  714. dstrect.h = screenRealHeight;
  715. if (SDL_MUSTLOCK(gpScreenReal))
  716. {
  717. if (SDL_LockSurface(gpScreenReal) < 0)
  718. return;
  719. }
  720. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  721. #if SDL_VERSION_ATLEAST(2, 0, 0)
  722. VIDEO_RenderCopy();
  723. #else
  724. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  725. #endif
  726. if (SDL_MUSTLOCK(gpScreenReal))
  727. {
  728. SDL_UnlockSurface(gpScreenReal);
  729. }
  730. UTIL_Delay(wSpeed);
  731. }
  732. }
  733. VOID
  734. VIDEO_FadeScreen(
  735. WORD wSpeed
  736. )
  737. /*++
  738. Purpose:
  739. Fade from the backup screen buffer to the current screen buffer.
  740. NOTE: This will destroy the backup buffer.
  741. Parameters:
  742. [IN] wSpeed - speed of fading (the larger value, the slower).
  743. Return value:
  744. None.
  745. --*/
  746. {
  747. int i, j, k;
  748. DWORD time;
  749. BYTE a, b;
  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. //
  756. // Lock surface if needed
  757. //
  758. if (SDL_MUSTLOCK(gpScreenReal))
  759. {
  760. if (SDL_LockSurface(gpScreenReal) < 0)
  761. return;
  762. }
  763. if (!bScaleScreen)
  764. {
  765. screenRealHeight -= offset;
  766. screenRealY = offset / 2;
  767. }
  768. time = SDL_GetTicks();
  769. wSpeed++;
  770. wSpeed *= 10;
  771. for (i = 0; i < 12; i++)
  772. {
  773. for (j = 0; j < 6; j++)
  774. {
  775. PAL_ProcessEvent();
  776. while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
  777. {
  778. PAL_ProcessEvent();
  779. SDL_Delay(5);
  780. }
  781. time = SDL_GetTicks() + wSpeed;
  782. //
  783. // Blend the pixels in the 2 buffers, and put the result into the
  784. // backup buffer
  785. //
  786. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  787. {
  788. a = ((LPBYTE)(gpScreen->pixels))[k];
  789. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  790. if (i > 0)
  791. {
  792. if ((a & 0x0F) > (b & 0x0F))
  793. {
  794. b++;
  795. }
  796. else if ((a & 0x0F) < (b & 0x0F))
  797. {
  798. b--;
  799. }
  800. }
  801. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  802. }
  803. //
  804. // Draw the backup buffer to the screen
  805. //
  806. if (g_wShakeTime != 0)
  807. {
  808. //
  809. // Shake the screen
  810. //
  811. SDL_Rect srcrect, dstrect;
  812. srcrect.x = 0;
  813. srcrect.y = 0;
  814. srcrect.w = 320;
  815. srcrect.h = 200 - g_wShakeLevel;
  816. dstrect.x = 0;
  817. dstrect.y = screenRealY;
  818. dstrect.w = 320 * gpScreenReal->w / gpScreen->w;
  819. dstrect.h = (200 - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  820. if (g_wShakeTime & 1)
  821. {
  822. srcrect.y = g_wShakeLevel;
  823. }
  824. else
  825. {
  826. dstrect.y = (screenRealY + g_wShakeLevel) * screenRealHeight / gpScreen->h;
  827. }
  828. SDL_SoftStretch(gpScreenBak, &srcrect, gpScreenReal, &dstrect);
  829. if (g_wShakeTime & 1)
  830. {
  831. dstrect.y = (screenRealY + screenRealHeight - g_wShakeLevel) * screenRealHeight / gpScreen->h;
  832. }
  833. else
  834. {
  835. dstrect.y = screenRealY;
  836. }
  837. dstrect.h = g_wShakeLevel * screenRealHeight / gpScreen->h;
  838. SDL_FillRect(gpScreenReal, &dstrect, 0);
  839. #if SDL_VERSION_ATLEAST(2, 0, 0)
  840. VIDEO_RenderCopy();
  841. #else
  842. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  843. #endif
  844. g_wShakeTime--;
  845. }
  846. else
  847. {
  848. dstrect.x = 0;
  849. dstrect.y = screenRealY;
  850. dstrect.w = gpScreenReal->w;
  851. dstrect.h = screenRealHeight;
  852. SDL_SoftStretch(gpScreenBak, NULL, gpScreenReal, &dstrect);
  853. #if SDL_VERSION_ATLEAST(2, 0, 0)
  854. VIDEO_RenderCopy();
  855. #else
  856. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  857. #endif
  858. }
  859. }
  860. }
  861. if (SDL_MUSTLOCK(gpScreenReal))
  862. {
  863. SDL_UnlockSurface(gpScreenReal);
  864. }
  865. //
  866. // Draw the result buffer to the screen as the final step
  867. //
  868. VIDEO_UpdateScreen(NULL);
  869. }
  870. void
  871. VIDEO_SetWindowTitle(
  872. const char* pszTitle
  873. )
  874. /*++
  875. Purpose:
  876. Set the caption of the window.
  877. Parameters:
  878. [IN] pszTitle - the new caption of the window.
  879. Return value:
  880. None.
  881. --*/
  882. {
  883. #if SDL_VERSION_ATLEAST(2,0,0)
  884. SDL_SetWindowTitle(gpWindow, pszTitle);
  885. #else
  886. SDL_WM_SetCaption(pszTitle, NULL);
  887. #endif
  888. }
  889. SDL_Surface *
  890. VIDEO_CreateCompatibleSurface(
  891. SDL_Surface *pSource
  892. )
  893. {
  894. return VIDEO_CreateCompatibleSizedSurface(pSource, NULL);
  895. }
  896. SDL_Surface *
  897. VIDEO_CreateCompatibleSizedSurface(
  898. SDL_Surface *pSource,
  899. const SDL_Rect *pSize
  900. )
  901. /*++
  902. Purpose:
  903. Create a surface that compatible with the source surface.
  904. Parameters:
  905. [IN] pSource - the source surface from which attributes are taken.
  906. [IN] pSize - the size (width & height) of the created surface.
  907. Return value:
  908. None.
  909. --*/
  910. {
  911. //
  912. // Create the surface
  913. //
  914. SDL_Surface *dest = SDL_CreateRGBSurface(pSource->flags,
  915. pSize ? pSize->w : pSource->w,
  916. pSize ? pSize->h : pSource->h,
  917. pSource->format->BitsPerPixel,
  918. pSource->format->Rmask, pSource->format->Gmask,
  919. pSource->format->Bmask, pSource->format->Amask);
  920. if (dest)
  921. {
  922. VIDEO_UpdateSurfacePalette(dest);
  923. }
  924. return dest;
  925. }
  926. SDL_Surface *
  927. VIDEO_DuplicateSurface(
  928. SDL_Surface *pSource,
  929. const SDL_Rect *pRect
  930. )
  931. /*++
  932. Purpose:
  933. Duplicate the selected area from the source surface into new surface.
  934. Parameters:
  935. [IN] pSource - the source surface.
  936. [IN] pRect - the area to be duplicated, NULL for entire surface.
  937. Return value:
  938. None.
  939. --*/
  940. {
  941. SDL_Surface* dest = VIDEO_CreateCompatibleSizedSurface(pSource, pRect);
  942. if (dest)
  943. {
  944. VIDEO_CopySurface(pSource, pRect, dest, NULL);
  945. }
  946. return dest;
  947. }
  948. void
  949. VIDEO_UpdateSurfacePalette(
  950. SDL_Surface *pSurface
  951. )
  952. /*++
  953. Purpose:
  954. Use the global palette to update the palette of pSurface.
  955. Parameters:
  956. [IN] pSurface - the surface whose palette should be updated.
  957. Return value:
  958. None.
  959. --*/
  960. {
  961. #if SDL_VERSION_ATLEAST(2, 0, 0)
  962. SDL_SetSurfacePalette(pSurface, gpPalette);
  963. #else
  964. SDL_SetPalette(pSurface, SDL_PHYSPAL | SDL_LOGPAL, gpPalette->colors, 0, 256);
  965. #endif
  966. }
  967. VOID
  968. VIDEO_DrawSurfaceToScreen(
  969. SDL_Surface *pSurface
  970. )
  971. /*++
  972. Purpose:
  973. Draw a surface directly to screen.
  974. Parameters:
  975. [IN] pSurface - the surface which needs to be drawn to screen.
  976. Return value:
  977. None.
  978. --*/
  979. {
  980. #if SDL_VERSION_ATLEAST(2, 0, 0)
  981. //
  982. // Draw the surface to screen.
  983. //
  984. SDL_BlitScaled(pSurface, NULL, gpScreenReal, NULL);
  985. VIDEO_RenderCopy();
  986. #else
  987. SDL_Surface *pCompatSurface;
  988. SDL_Rect rect;
  989. rect.x = rect.y = 0;
  990. rect.w = pSurface->w;
  991. rect.h = pSurface->h;
  992. pCompatSurface = VIDEO_CreateCompatibleSizedSurface(gpScreenReal, &rect);
  993. //
  994. // First convert the surface to compatible format.
  995. //
  996. SDL_BlitSurface(pSurface, NULL, pCompatSurface, NULL);
  997. //
  998. // Draw the surface to screen.
  999. //
  1000. SDL_SoftStretch(pCompatSurface, NULL, gpScreenReal, NULL);
  1001. SDL_UpdateRect(gpScreenReal, 0, 0, gpScreenReal->w, gpScreenReal->h);
  1002. SDL_FreeSurface(pCompatSurface);
  1003. #endif
  1004. }
  1005. BOOL
  1006. VIDEO_GetWindowInfo(
  1007. SDL_SysWMinfo *pInfo
  1008. )
  1009. {
  1010. if (NULL == pInfo) return FALSE;
  1011. SDL_VERSION(&pInfo->version);
  1012. #if SDL_VERSION_ATLEAST(2, 0, 0)
  1013. return SDL_GetWindowWMInfo(gpWindow, pInfo);
  1014. #else
  1015. return SDL_GetWMInfo(pInfo);
  1016. #endif
  1017. }