PALETTE.CPP 15 KB

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