palette.c 13 KB

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