MORPHPAL.CPP 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 S T U D I O S **
  16. ***************************************************************************
  17. * *
  18. * Project Name : wwlib32 *
  19. * *
  20. * File Name : PALTOPAL.CPP *
  21. * *
  22. * Programmer : Bill Randolph *
  23. * *
  24. * Start Date : May 2, 1994 *
  25. * *
  26. * Last Update : May 2, 1994 [BR] *
  27. * *
  28. *-------------------------------------------------------------------------*
  29. * Functions: *
  30. * Morph_Palette -- morphs a palette from source to destination *
  31. * Palette_To_Palette -- morph src palette to a dst palette *
  32. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33. /*
  34. ********************************* Includes **********************************
  35. */
  36. #include "wwstd.h"
  37. #include "palette.h"
  38. #include "timer.h"
  39. /*
  40. ********************************* Constants *********************************
  41. */
  42. #define SCALE(a,b,c) (((((long)(a)<<8) / (long)(b) ) * (unsigned long)(c)) >>8)
  43. /*
  44. ********************************** Globals **********************************
  45. */
  46. /*
  47. ******************************** Prototypes *********************************
  48. */
  49. PRIVATE int __cdecl Palette_To_Palette(void *src_palette, void *dst_palette, unsigned long current_time, unsigned long delay);
  50. /***************************************************************************
  51. * Morph_Palette -- morphs a palette from source to destination *
  52. * *
  53. * INPUT: *
  54. * void *src_pal - starting palette *
  55. * void *dst_pal - ending palette *
  56. * unsigned int delay - time delay in 60ths of a second *
  57. * void *callback - user-defined callback, NULL if none *
  58. * *
  59. * OUTPUT: *
  60. * none. *
  61. * *
  62. * WARNINGS: *
  63. * *
  64. * HISTORY: *
  65. * 05/02/1994 BR : Created. *
  66. *=========================================================================*/
  67. void cdecl Morph_Palette (void *src_pal, void *dst_pal, unsigned int delay,
  68. void (*callback) (void) )
  69. {
  70. int result;
  71. unsigned long pal_start = TickCount.Time();
  72. extern void (*cb_ptr) ( void ) ; // callback function pointer
  73. // (void *)cb_ptr = callback;
  74. cb_ptr = callback ;
  75. /*===================================================================*/
  76. /* Make sure that we don't go too fast but also make sure we keep */
  77. /* processing the morph palette if we have one. */
  78. /*===================================================================*/
  79. while (1) {
  80. if (src_pal && dst_pal) {
  81. result = Palette_To_Palette (src_pal, dst_pal,
  82. (TickCount.Time() - pal_start), (unsigned long)delay);
  83. if (!result)
  84. break;
  85. if (callback) {
  86. (*cb_ptr)();
  87. }
  88. }
  89. }
  90. return;
  91. } /* end of Morph_Palette */
  92. /***************************************************************************
  93. * Palette_To_Palette -- morph src palette to a dst palette *
  94. * *
  95. * Creates & sets a palette that's in-between 'src_palette' & *
  96. * 'dst_palette'; how close it is to dst_palette is based on how close *
  97. * 'current_time' is to 'delay'. 'current_time' & 'delay' are based on *
  98. * 0 being the start time. *
  99. * *
  100. * INPUT: void *src_palette = palette we want to morph from *
  101. * void *dst_palette = palette we want to morph to *
  102. * long current_time = time we started morph pal *
  103. * long delay = time we want the morph to take*
  104. * *
  105. * OUTPUT: int if the time had elapsed and no chages were *
  106. * necessary this routine returns FALSE *
  107. * otherwise it will always return TRUE (this *
  108. * was necessary to detect the end of the ice *
  109. * effect. *
  110. * *
  111. * HISTORY: *
  112. * 05/24/1993 MC : Created. *
  113. *=========================================================================*/
  114. PRIVATE int cdecl Palette_To_Palette(void *src_palette, void *dst_palette,
  115. unsigned long current_time, unsigned long delay)
  116. {
  117. char colour;
  118. char diff;
  119. int chgval;
  120. int lp;
  121. int change;
  122. static char palette[768];
  123. char *src_pal = (char*)src_palette;
  124. char *dst_pal = (char*)dst_palette;
  125. /*======================================================================*/
  126. /* Loop through each RGB value attempting to change it to the correct */
  127. /* color. */
  128. /*======================================================================*/
  129. for (change = lp = 0; lp < 768; lp++) {
  130. if (current_time < delay ) {
  131. diff = dst_pal[lp] & (char)63;
  132. diff -= src_pal[lp] & (char)63;
  133. if (diff)
  134. change = TRUE;
  135. chgval = SCALE(diff, delay, current_time);
  136. colour = src_pal[lp] & (char)63;
  137. colour +=(char)chgval;
  138. }
  139. else {
  140. colour = dst_pal[lp] & (char)63;
  141. change = FALSE;
  142. }
  143. palette[lp] = colour;
  144. }
  145. /*======================================================================*/
  146. /* Set the palette to the color that we created. */
  147. /*======================================================================*/
  148. Set_Palette(palette);
  149. return(change);
  150. } /* end of Palette_To_Palette */
  151. /*************************** End of morphpal.cpp ***************************/
  152.