PALETTE.CPP 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***************************************************************************
  15. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  16. ***************************************************************************
  17. * *
  18. * Project Name : WWLIB *
  19. * *
  20. * File Name : PALETTE.C *
  21. * *
  22. * Programmer : BILL STOKES *
  23. * *
  24. * Start Date : 6/20/91 *
  25. * *
  26. * Last Update : August 2, 1994 [SKB] *
  27. * *
  28. *-------------------------------------------------------------------------*
  29. * Note: This module contains dependencies upon the video system, *
  30. * specifically Get_Video_Mode(). *
  31. *-------------------------------------------------------------------------*
  32. * Functions: *
  33. * Set_Palette -- sets the current palette *
  34. * Set_Palette_Color -- Set a color number in a palette to the data. *
  35. * Fade_Palette_To -- Fades the current palette into another *
  36. * Determine_Bump_Rate -- determines desired bump rate for fading *
  37. * Bump_Palette -- increments the palette one step, for fading *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. /*
  40. ********************************* Includes **********************************
  41. */
  42. //#include <mem.h>
  43. #include "palette.h"
  44. #include "timer.h"
  45. #include "wwstd.h"
  46. /*
  47. ********************************* Constants *********************************
  48. */
  49. /*
  50. ********************************** Globals **********************************
  51. */
  52. extern "C" extern unsigned char CurrentPalette[]; /* in pal.asm */
  53. /*
  54. ******************************** Prototypes *********************************
  55. */
  56. PRIVATE void __cdecl Determine_Bump_Rate(void *palette, int delay, short *ticks, short *rate);
  57. PRIVATE BOOL __cdecl Bump_Palette(void *palette1, unsigned int step);
  58. /*
  59. ******************************** Code *********************************
  60. */
  61. /***************************************************************************
  62. * Set_Palette -- sets the current palette *
  63. * *
  64. * INPUT: *
  65. * void *palette - palette to set *
  66. * *
  67. * OUTPUT: *
  68. * none *
  69. * *
  70. * WARNINGS: *
  71. * *
  72. * HISTORY: *
  73. * 04/25/1994 SKB : Created. *
  74. * 04/27/1994 BR : Converted to 32-bit *
  75. *=========================================================================*/
  76. void __cdecl Set_Palette(void *palette)
  77. {
  78. #if(IBM)
  79. Set_Palette_Range(palette);
  80. #else
  81. Copy_Palette(palette,CurrentPalette);
  82. LoadRGB4(&Main_Screen->ViewPort,palette,32L);
  83. LoadRGB4(AltVPort,palette,32L);
  84. #endif
  85. } /* end of Set_Palette */
  86. /***************************************************************************
  87. * Set_Palette_Color -- Set a color number in a palette to the data. *
  88. * *
  89. * *
  90. * INPUT: *
  91. * void *palette - palette to set color in *
  92. * int color - which color index to set *
  93. * void *data - RGB data for color *
  94. * *
  95. * OUTPUT: *
  96. * none *
  97. * *
  98. * WARNINGS: *
  99. * *
  100. * HISTORY: *
  101. * 04/25/1994 SKB : Created. *
  102. * 04/27/1994 BR : Converted to 32-bit *
  103. *=========================================================================*/
  104. void __cdecl Set_Palette_Color(void *palette, int color, void *data)
  105. {
  106. /*
  107. ---------------------- Return if 'palette' is NULL -----------------------
  108. */
  109. if (!palette) return;
  110. /*
  111. ------------------- Change the color & set the palette -------------------
  112. */
  113. #if(IBM)
  114. memcpy(&((unsigned char *)palette)[color * RGB_BYTES], data, RGB_BYTES);
  115. Set_Palette_Range(palette);
  116. #else
  117. palette[color] = *(unsigned short*)data;
  118. Set_Palette(palette);
  119. #endif
  120. } /* end of Set_Palette */
  121. /***************************************************************************
  122. * Fade_Palette_To -- Fades the current palette into another *
  123. * *
  124. * This will allow the palette to fade from current palette into the *
  125. * palette that was passed in. This can be used to fade in and fade out. *
  126. * *
  127. * INPUT: *
  128. * char *palette1 - this is the palette to fade to. *
  129. * unsigned int delay - fade with this timer count down *
  130. * void *callback - user-defined callback function *
  131. * *
  132. * OUTPUT: none *
  133. * *
  134. * WARNINGS: none *
  135. * *
  136. * HISTORY: *
  137. * 06/20/1991 BS : Created. *
  138. *=========================================================================*/
  139. void Fade_Palette_To(void *palette1, unsigned int delay, void (*callback)() )
  140. {
  141. BOOL changed; // Flag that palette has changed this tick.
  142. short jump; // Gun values to jump per palette set.
  143. unsigned long timer; // Tick count timer used for timing.
  144. short ticksper; // The ticks (fixed point) per bit jump.
  145. int tickaccum;
  146. extern void (*cb_ptr)(void); // callback function pointer
  147. // (void *)cb_ptr = callback;
  148. cb_ptr = callback;
  149. /*
  150. ---------------------- Return if 'palette1' is NULL ----------------------
  151. */
  152. if (!palette1)
  153. return;
  154. /*
  155. --------------------------- Get the bump rate ----------------------------
  156. */
  157. Determine_Bump_Rate(palette1, delay, &ticksper, &jump);
  158. tickaccum = 0; // init accumulated elapsed time
  159. timer = TickCount.Time(); // timer = current time
  160. do {
  161. changed = FALSE;
  162. tickaccum += ticksper; // tickaccum = time of next change * 256
  163. timer += (tickaccum >> 8); // timer = time of next change (rounded)
  164. tickaccum &= 0x0FF; // shave off high byte, keep roundoff bits
  165. changed = Bump_Palette(palette1, jump); // increment palette
  166. /*
  167. .................. Wait for time increment to elapse ..................
  168. */
  169. if (changed) {
  170. while (TickCount.Time() < (int)timer) {
  171. /*
  172. ................. Update callback while waiting .................
  173. */
  174. if (callback) {
  175. #if LIB_EXTERNS_RESOLVED
  176. Sound_Callback(); // should be removed!
  177. #endif
  178. (*cb_ptr)();
  179. }
  180. }
  181. }
  182. #if LIB_EXTERNS_RESOLVED
  183. Sound_Callback(); // should be removed!
  184. #endif
  185. if (callback) {
  186. (*cb_ptr)();
  187. }
  188. } while (changed);
  189. } /* end of Fade_Palette_To */
  190. /***************************************************************************
  191. * Determine_Bump_Rate -- determines desired bump rate for fading *
  192. * *
  193. * INPUT: *
  194. * unsigned char *palette - palette to fade to *
  195. * int delay - desired time delay in 60ths of a second *
  196. * short *ticks - output: loop ticks per color jump *
  197. * short *rate - output: color gun increment rate *
  198. * *
  199. * OUTPUT: *
  200. * none *
  201. * *
  202. * WARNINGS: *
  203. * *
  204. * HISTORY: *
  205. * 04/27/1994 BR : Converted to 32-bit *
  206. * 08/02/1994 SKB : Made private *
  207. *=========================================================================*/
  208. PRIVATE void __cdecl Determine_Bump_Rate(void *palette, int delay, short *ticks,
  209. short *rate)
  210. {
  211. int gun1; // Palette 1 gun value.
  212. int gun2; // Palette 2 gun value.
  213. int diff; // Maximum color gun difference.
  214. int tp; // Temporary tick accumulator.
  215. int index; // Color gun working index.
  216. long t; // Working tick intermediate value.
  217. int adiff; // Absolute difference between guns.
  218. /*
  219. ------------------------ Find max gun difference -------------------------
  220. */
  221. diff = 0;
  222. for (index = 0; index < PALETTE_BYTES; index++) {
  223. gun1 = ((unsigned char *)palette)[index];
  224. gun2 = CurrentPalette[index];
  225. adiff = ABS(gun1-gun2);
  226. diff = MAX(diff, adiff);
  227. }
  228. /*------------------------------------------------------------------------
  229. ticks = (total time delay ) / (max gun diff)
  230. The value is computed based on (delay * 256), for fixed-point math;
  231. the lower bits represent the leftover from the division; 'ticks' is
  232. returned still shifted, so the low bits can be used to accumulate the
  233. time more accurately; the caller must shift the accumulated value down
  234. 8 bits to determine the actual elapsed time!
  235. ------------------------------------------------------------------------*/
  236. t = ((long)delay) << 8;
  237. if (diff) {
  238. t /= diff;
  239. t = MIN((long)t, (long)0x7FFF);
  240. }
  241. *ticks = (short)t;
  242. /*------------------------------------------------------------------------
  243. Adjust the color gun rate value if the time to fade is faster than can
  244. reasonably be performed given the palette change, ie if (ticks>>8)==0,
  245. and thus less than 1/60 of a second
  246. ------------------------------------------------------------------------*/
  247. tp = *ticks;
  248. *rate = 1;
  249. while (*rate <= diff && *ticks < 256) {
  250. *ticks += tp;
  251. *rate += 1;
  252. }
  253. } /* end of Determine_Bump_Rate */
  254. /***************************************************************************
  255. * Bump_Palette -- increments the palette one step, for fading *
  256. * *
  257. * INPUT: *
  258. * palette1 - palette to fade towards *
  259. * step - max step amount, determined by Determine_Bump_Rate *
  260. * *
  261. * OUTPUT: *
  262. * FALSE = no change, TRUE = changed *
  263. * *
  264. * WARNINGS: *
  265. * *
  266. * HISTORY: *
  267. * 04/27/1994 BR : Created. *
  268. * 08/02/1994 SKB : Made private *
  269. *=========================================================================*/
  270. #if(IBM)
  271. PRIVATE BOOL __cdecl Bump_Palette(void *palette1, unsigned int step)
  272. {
  273. BOOL changed=FALSE; // Flag that palette has changed this tick.
  274. int index; // Index to DAC register gun.
  275. int gun1,gun2; // Palette 1 gun value.
  276. unsigned char palette[PALETTE_BYTES]; // copy of current palette
  277. /*
  278. ---------------------- Return if 'palette1' is NULL ----------------------
  279. */
  280. if (!palette1)
  281. return (FALSE);
  282. /*
  283. ------------------------ Copy the current palette ------------------------
  284. */
  285. memcpy(palette, CurrentPalette, 768);
  286. /*
  287. ----------------------- Loop through palette bytes -----------------------
  288. */
  289. for (index = 0; index < PALETTE_BYTES; index++) {
  290. gun1 = ((unsigned char *)palette1)[index];
  291. gun2 = palette[index];
  292. /*
  293. ............. If the colors match, go on to the next one ..............
  294. */
  295. if (gun1 == gun2) continue;
  296. changed = TRUE;
  297. /*
  298. .................. Increment current palette's color ..................
  299. */
  300. if (gun2 < gun1) {
  301. gun2 += step;
  302. gun2 = MIN(gun2, gun1); // make sure we didn't overshoot it
  303. }
  304. /*
  305. .................. Decrement current palette's color ..................
  306. */
  307. else {
  308. gun2 -= step;
  309. gun2 = MAX(gun2, gun1); // make sure we didn't overshoot it
  310. }
  311. palette[index] = (unsigned char)gun2;
  312. }
  313. /*
  314. ----------------- Set current palette to the new palette -----------------
  315. */
  316. if (changed) {
  317. Set_Palette(&palette[0]);
  318. }
  319. return (changed);
  320. } /* end of Bump_Palette */
  321. #else
  322. /* This is already implemented in asm on the Amiga */
  323. #endif
  324. void (*cb_ptr)(void); // callback function pointer
  325. /**************************** End of palette.cpp ***************************/
  326.