palette.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. SDL_Color *
  24. PAL_GetPalette(
  25. INT iPaletteNum,
  26. BOOL fNight
  27. )
  28. /*++
  29. Purpose:
  30. Get the specified palette in pat.mkf file.
  31. Parameters:
  32. [IN] iPaletteNum - number of the palette.
  33. [IN] fNight - whether use the night palette or not.
  34. Return value:
  35. Pointer to the palette. NULL if failed.
  36. --*/
  37. {
  38. static SDL_Color palette[256];
  39. PAL_LARGE BYTE buf[1536];
  40. INT i;
  41. FILE *fp;
  42. fp = UTIL_OpenRequiredFile("pat.mkf");
  43. //
  44. // Read the palette data from the pat.mkf file
  45. //
  46. i = PAL_MKFReadChunk(buf, 1536, iPaletteNum, fp);
  47. fclose(fp);
  48. if (i < 0)
  49. {
  50. //
  51. // Read failed
  52. //
  53. return NULL;
  54. }
  55. else if (i <= 256 * 3)
  56. {
  57. //
  58. // There is no night colors in the palette
  59. //
  60. fNight = FALSE;
  61. }
  62. for (i = 0; i < 256; i++)
  63. {
  64. palette[i].r = buf[(fNight ? 256 * 3 : 0) + i * 3] << 2;
  65. palette[i].g = buf[(fNight ? 256 * 3 : 0) + i * 3 + 1] << 2;
  66. palette[i].b = buf[(fNight ? 256 * 3 : 0) + i * 3 + 2] << 2;
  67. #if 0
  68. palette[i].r += (255 - palette[i].r) / 5;
  69. palette[i].g += (255 - palette[i].g) / 5;
  70. palette[i].b += (255 - palette[i].b) / 5;
  71. #endif
  72. }
  73. return palette;
  74. }
  75. VOID
  76. PAL_SetPalette(
  77. INT iPaletteNum,
  78. BOOL fNight
  79. )
  80. /*++
  81. Purpose:
  82. Set the screen palette to the specified one.
  83. Parameters:
  84. [IN] iPaletteNum - number of the palette.
  85. [IN] fNight - whether use the night palette or not.
  86. Return value:
  87. None.
  88. --*/
  89. {
  90. SDL_Color *p = PAL_GetPalette(iPaletteNum, fNight);
  91. if (p != NULL)
  92. {
  93. VIDEO_SetPalette(p);
  94. }
  95. }
  96. VOID
  97. PAL_FadeOut(
  98. INT iDelay
  99. )
  100. /*++
  101. Purpose:
  102. Fadeout screen to black from the specified palette.
  103. Parameters:
  104. [IN] iPaletteNum - number of the palette.
  105. [IN] fNight - whether use the night palette or not.
  106. [IN] iDelay - delay time for each step.
  107. Return value:
  108. None.
  109. --*/
  110. {
  111. int i, j;
  112. UINT time;
  113. PAL_LARGE SDL_Color palette[256];
  114. PAL_LARGE SDL_Color newpalette[256];
  115. //
  116. // Get the original palette...
  117. //
  118. for (i = 0; i < 256; i++)
  119. {
  120. palette[i] = VIDEO_GetPalette()[i];
  121. }
  122. //
  123. // Start fading out...
  124. //
  125. time = SDL_GetTicks() + iDelay * 10 * 60;
  126. while (TRUE)
  127. {
  128. //
  129. // Set the current palette...
  130. //
  131. j = (int)(time - SDL_GetTicks()) / iDelay / 10;
  132. if (j < 0)
  133. {
  134. break;
  135. }
  136. for (i = 0; i < 256; i++)
  137. {
  138. newpalette[i].r = (palette[i].r * j) >> 6;
  139. newpalette[i].g = (palette[i].g * j) >> 6;
  140. newpalette[i].b = (palette[i].b * j) >> 6;
  141. }
  142. VIDEO_SetPalette(newpalette);
  143. UTIL_Delay(10);
  144. }
  145. memset(newpalette, 0, sizeof(newpalette));
  146. VIDEO_SetPalette(newpalette);
  147. }
  148. VOID
  149. PAL_FadeIn(
  150. INT iPaletteNum,
  151. BOOL fNight,
  152. INT iDelay
  153. )
  154. /*++
  155. Purpose:
  156. Fade in the screen to the specified palette.
  157. Parameters:
  158. [IN] iPaletteNum - number of the palette.
  159. [IN] fNight - whether use the night palette or not.
  160. [IN] iDelay - delay time for each step.
  161. Return value:
  162. None.
  163. --*/
  164. {
  165. int i, j;
  166. UINT time;
  167. SDL_Color *palette;
  168. PAL_LARGE SDL_Color newpalette[256];
  169. //
  170. // Get the new palette...
  171. //
  172. palette = PAL_GetPalette(iPaletteNum, fNight);
  173. //
  174. // Start fading in...
  175. //
  176. time = SDL_GetTicks() + iDelay * 10 * 60;
  177. while (TRUE)
  178. {
  179. //
  180. // Set the current palette...
  181. //
  182. j = (int)(time - SDL_GetTicks()) / iDelay / 10;
  183. if (j < 0)
  184. {
  185. break;
  186. }
  187. j = 60 - j;
  188. for (i = 0; i < 256; i++)
  189. {
  190. newpalette[i].r = (palette[i].r * j) >> 6;
  191. newpalette[i].g = (palette[i].g * j) >> 6;
  192. newpalette[i].b = (palette[i].b * j) >> 6;
  193. }
  194. VIDEO_SetPalette(newpalette);
  195. UTIL_Delay(10);
  196. }
  197. VIDEO_SetPalette(palette);
  198. }
  199. VOID
  200. PAL_SceneFade(
  201. INT iPaletteNum,
  202. BOOL fNight,
  203. INT iStep
  204. )
  205. /*++
  206. Purpose:
  207. Fade in or fade out the screen. Update the scene during the process.
  208. Parameters:
  209. [IN] iPaletteNum - number of the palette.
  210. [IN] fNight - whether use the night palette or not.
  211. [IN] iStep - positive to fade in, nagative to fade out.
  212. Return value:
  213. None.
  214. --*/
  215. {
  216. SDL_Color *palette, newpalette[256];
  217. int i, j;
  218. DWORD time;
  219. palette = PAL_GetPalette(iPaletteNum, fNight);
  220. if (palette == NULL)
  221. {
  222. return;
  223. }
  224. if (iStep == 0)
  225. {
  226. iStep = 1;
  227. }
  228. gpGlobals->fNeedToFadeIn = FALSE;
  229. if (iStep > 0)
  230. {
  231. for (i = 0; i < 64; i += iStep)
  232. {
  233. time = SDL_GetTicks() + 100;
  234. //
  235. // Generate the scene
  236. //
  237. PAL_ClearKeyState();
  238. g_InputState.dir = kDirUnknown;
  239. g_InputState.prevdir = kDirUnknown;
  240. PAL_GameUpdate(FALSE);
  241. PAL_MakeScene();
  242. VIDEO_UpdateScreen(NULL);
  243. //
  244. // Calculate the current palette...
  245. //
  246. for (j = 0; j < 256; j++)
  247. {
  248. newpalette[j].r = (palette[j].r * i) >> 6;
  249. newpalette[j].g = (palette[j].g * i) >> 6;
  250. newpalette[j].b = (palette[j].b * i) >> 6;
  251. }
  252. VIDEO_SetPalette(newpalette);
  253. PAL_ProcessEvent();
  254. while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
  255. {
  256. PAL_ProcessEvent();
  257. SDL_Delay(5);
  258. }
  259. }
  260. }
  261. else
  262. {
  263. for (i = 63; i >= 0; i += iStep)
  264. {
  265. time = SDL_GetTicks() + 100;
  266. //
  267. // Generate the scene
  268. //
  269. PAL_ClearKeyState();
  270. g_InputState.dir = kDirUnknown;
  271. g_InputState.prevdir = kDirUnknown;
  272. PAL_GameUpdate(FALSE);
  273. PAL_MakeScene();
  274. VIDEO_UpdateScreen(NULL);
  275. //
  276. // Calculate the current palette...
  277. //
  278. for (j = 0; j < 256; j++)
  279. {
  280. newpalette[j].r = (palette[j].r * i) >> 6;
  281. newpalette[j].g = (palette[j].g * i) >> 6;
  282. newpalette[j].b = (palette[j].b * i) >> 6;
  283. }
  284. VIDEO_SetPalette(newpalette);
  285. PAL_ProcessEvent();
  286. while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
  287. {
  288. PAL_ProcessEvent();
  289. SDL_Delay(5);
  290. }
  291. }
  292. }
  293. }
  294. VOID
  295. PAL_PaletteFade(
  296. INT iPaletteNum,
  297. BOOL fNight,
  298. BOOL fUpdateScene
  299. )
  300. /*++
  301. Purpose:
  302. Fade from the current palette to the specified one.
  303. Parameters:
  304. [IN] iPaletteNum - number of the palette.
  305. [IN] fNight - whether use the night palette or not.
  306. [IN] fUpdateScene - TRUE if update the scene in the progress.
  307. Return value:
  308. None.
  309. --*/
  310. {
  311. int i, j;
  312. UINT time;
  313. SDL_Color *newpalette = PAL_GetPalette(iPaletteNum, fNight);
  314. PAL_LARGE SDL_Color palette[256];
  315. PAL_LARGE SDL_Color t[256];
  316. if (newpalette == NULL)
  317. {
  318. return;
  319. }
  320. for (i = 0; i < 256; i++)
  321. {
  322. palette[i] = VIDEO_GetPalette()[i];
  323. }
  324. //
  325. // Start fading...
  326. //
  327. for (i = 0; i < 32; i++)
  328. {
  329. time = SDL_GetTicks() + (fUpdateScene ? FRAME_TIME : FRAME_TIME / 4);
  330. for (j = 0; j < 256; j++)
  331. {
  332. t[j].r =
  333. (BYTE)(((INT)(palette[j].r) * (31 - i) + (INT)(newpalette[j].r) * i) / 31);
  334. t[j].g =
  335. (BYTE)(((INT)(palette[j].g) * (31 - i) + (INT)(newpalette[j].g) * i) / 31);
  336. t[j].b =
  337. (BYTE)(((INT)(palette[j].b) * (31 - i) + (INT)(newpalette[j].b) * i) / 31);
  338. }
  339. VIDEO_SetPalette(t);
  340. if (fUpdateScene)
  341. {
  342. PAL_ClearKeyState();
  343. g_InputState.dir = kDirUnknown;
  344. g_InputState.prevdir = kDirUnknown;
  345. PAL_GameUpdate(FALSE);
  346. PAL_MakeScene();
  347. VIDEO_UpdateScreen(NULL);
  348. }
  349. PAL_ProcessEvent();
  350. while (!SDL_TICKS_PASSED(SDL_GetTicks(), time))
  351. {
  352. PAL_ProcessEvent();
  353. SDL_Delay(5);
  354. }
  355. }
  356. }
  357. VOID
  358. PAL_ColorFade(
  359. INT iDelay,
  360. BYTE bColor,
  361. BOOL fFrom
  362. )
  363. /*++
  364. Purpose:
  365. Fade the palette from/to the specified color.
  366. Parameters:
  367. [IN] iDelay - the delay time of each step.
  368. [IN] bColor - the color to fade from/to.
  369. [IN] fFrom - if TRUE then fade from bColor, else fade to bColor.
  370. Return value:
  371. None.
  372. --*/
  373. {
  374. SDL_Color *palette;
  375. PAL_LARGE SDL_Color newpalette[256];
  376. int i, j;
  377. palette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  378. iDelay *= 10;
  379. if (iDelay == 0)
  380. {
  381. iDelay = 10;
  382. }
  383. if (fFrom)
  384. {
  385. for (i = 0; i < 256; i++)
  386. {
  387. newpalette[i] = palette[bColor];
  388. }
  389. for (i = 0; i < 64; i++)
  390. {
  391. for (j = 0; j < 256; j++)
  392. {
  393. if (newpalette[j].r > palette[j].r)
  394. {
  395. newpalette[j].r -= 4;
  396. }
  397. else if (newpalette[j].r < palette[j].r)
  398. {
  399. newpalette[j].r += 4;
  400. }
  401. if (newpalette[j].g > palette[j].g)
  402. {
  403. newpalette[j].g -= 4;
  404. }
  405. else if (newpalette[j].g < palette[j].g)
  406. {
  407. newpalette[j].g += 4;
  408. }
  409. if (newpalette[j].b > palette[j].b)
  410. {
  411. newpalette[j].b -= 4;
  412. }
  413. else if (newpalette[j].b < palette[j].b)
  414. {
  415. newpalette[j].b += 4;
  416. }
  417. }
  418. VIDEO_SetPalette(newpalette);
  419. UTIL_Delay(iDelay);
  420. }
  421. VIDEO_SetPalette(palette);
  422. }
  423. else
  424. {
  425. memcpy(newpalette, palette, sizeof(newpalette));
  426. for (i = 0; i < 64; i++)
  427. {
  428. for (j = 0; j < 256; j++)
  429. {
  430. if (newpalette[j].r > palette[bColor].r)
  431. {
  432. newpalette[j].r -= 4;
  433. }
  434. else if (newpalette[j].r < palette[bColor].r)
  435. {
  436. newpalette[j].r += 4;
  437. }
  438. if (newpalette[j].g > palette[bColor].g)
  439. {
  440. newpalette[j].g -= 4;
  441. }
  442. else if (newpalette[j].g < palette[bColor].g)
  443. {
  444. newpalette[j].g += 4;
  445. }
  446. if (newpalette[j].b > palette[bColor].b)
  447. {
  448. newpalette[j].b -= 4;
  449. }
  450. else if (newpalette[j].b < palette[bColor].b)
  451. {
  452. newpalette[j].b += 4;
  453. }
  454. }
  455. VIDEO_SetPalette(newpalette);
  456. UTIL_Delay(iDelay);
  457. }
  458. for (i = 0; i < 256; i++)
  459. {
  460. newpalette[i] = palette[bColor];
  461. }
  462. VIDEO_SetPalette(newpalette);
  463. }
  464. }
  465. VOID
  466. PAL_FadeToRed(
  467. VOID
  468. )
  469. /*++
  470. Purpose:
  471. Fade the whole screen to red color.
  472. Parameters:
  473. None.
  474. Return value:
  475. None.
  476. --*/
  477. {
  478. SDL_Color *palette;
  479. PAL_LARGE SDL_Color newpalette[256];
  480. int i, j;
  481. BYTE color;
  482. palette = PAL_GetPalette(gpGlobals->wNumPalette, gpGlobals->fNightPalette);
  483. memcpy(newpalette, palette, sizeof(newpalette));
  484. for (i = 0; i < gpScreen->pitch * gpScreen->h; i++)
  485. {
  486. if (((LPBYTE)(gpScreen->pixels))[i] == 0x4F)
  487. {
  488. ((LPBYTE)(gpScreen->pixels))[i] = 0x4E; // HACKHACK
  489. }
  490. }
  491. VIDEO_UpdateScreen(NULL);
  492. for (i = 0; i < 32; i++)
  493. {
  494. for (j = 0; j < 256; j++)
  495. {
  496. if (j == 0x4F)
  497. {
  498. continue; // so that texts will not be affected
  499. }
  500. color = ((INT)palette[j].r + (INT)palette[j].g + (INT)palette[j].b) / 4 + 64;
  501. if (newpalette[j].r > color)
  502. {
  503. newpalette[j].r -= (newpalette[j].r - color > 8 ? 8 : newpalette[j].r - color);
  504. }
  505. else if (newpalette[j].r < color)
  506. {
  507. newpalette[j].r += (color - newpalette[j].r > 8 ? 8 : color - newpalette[j].r);
  508. }
  509. if (newpalette[j].g > 0)
  510. {
  511. newpalette[j].g -= (newpalette[j].g > 8 ? 8 : newpalette[j].g);
  512. }
  513. if (newpalette[j].b > 0)
  514. {
  515. newpalette[j].b -= (newpalette[j].b > 8 ? 8 : newpalette[j].b);
  516. }
  517. }
  518. VIDEO_SetPalette(newpalette);
  519. UTIL_Delay(75);
  520. }
  521. }