convert.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. ** Command & Conquer Renegade(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 S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /G/wwlib/Convert.cpp $*
  25. * *
  26. * $Author:: Eric_c $*
  27. * *
  28. * $Modtime:: 2/19/99 11:51a $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "always.h"
  36. #include "blitblit.h"
  37. #include "convert.h"
  38. #include "dsurface.h"
  39. #include "hsv.h"
  40. #include "rlerle.h"
  41. ConvertClass::ConvertClass(PaletteClass const & artpalette, PaletteClass const & screenpalette, Surface const & surface) :
  42. BBP(surface.Bytes_Per_Pixel()),
  43. PlainBlitter(NULL),
  44. TransBlitter(NULL),
  45. ShadowBlitter(NULL),
  46. RemapBlitter(NULL),
  47. Translucent1Blitter(NULL),
  48. Translucent2Blitter(NULL),
  49. Translucent3Blitter(NULL),
  50. RLETransBlitter(NULL),
  51. RLEShadowBlitter(NULL),
  52. RLERemapBlitter(NULL),
  53. RLETranslucent1Blitter(NULL),
  54. RLETranslucent2Blitter(NULL),
  55. RLETranslucent3Blitter(NULL),
  56. Translator(NULL),
  57. ShadowTable(NULL),
  58. RemapTable(NULL)
  59. {
  60. /*
  61. ** The draw data initialization is greatly dependant upon the pixel format
  62. ** of the display surface. Check the pixel format and set the values accordingly.
  63. */
  64. if (BBP == 1) {
  65. /*
  66. ** Build the shadow table by creating a slightly darker version of
  67. ** the color and then finding the closest match to it.
  68. */
  69. ShadowTable = new unsigned char [256];
  70. ShadowTable[0] = 0;
  71. for (int shadow = 1; shadow < 256; shadow++) {
  72. HSVClass hsv = artpalette[shadow];
  73. hsv.Set_Value((unsigned char)(hsv.Get_Value() / 2));
  74. ShadowTable[shadow] = (unsigned char)artpalette.Closest_Color(hsv);
  75. }
  76. /*
  77. ** The translator table is created by finding the closest color
  78. ** in the display palette from each color in the source art
  79. ** palette.
  80. */
  81. unsigned char * trans = new unsigned char [256];
  82. trans[0] = 0;
  83. for (int index = 1; index < 256; index++) {
  84. trans[index] = (unsigned char)screenpalette.Closest_Color(artpalette[index]);
  85. }
  86. Translator = (void *)trans;
  87. /*
  88. ** Construct all the blitter objects necessary to support the functionality
  89. ** required for the draw permutations.
  90. */
  91. PlainBlitter = new BlitPlainXlat<unsigned char>((unsigned char const *)Translator);
  92. TransBlitter = new BlitTransXlat<unsigned char>((unsigned char const *)Translator);
  93. RemapBlitter = new BlitTransZRemapXlat<unsigned char>(&RemapTable, (unsigned char const *)Translator);
  94. ShadowBlitter = new BlitTransRemapDest<unsigned char>(ShadowTable);
  95. Translucent1Blitter = new BlitTransRemapXlat<unsigned char>(ShadowTable, (unsigned char const *)Translator);
  96. Translucent2Blitter = new BlitTransRemapXlat<unsigned char>(ShadowTable, (unsigned char const *)Translator);
  97. Translucent3Blitter = new BlitTransRemapXlat<unsigned char>(ShadowTable, (unsigned char const *)Translator);
  98. /*
  99. ** Create the RLE aware blitter objects.
  100. */
  101. RLETransBlitter = new RLEBlitTransXlat<unsigned char>((unsigned char const *)Translator);
  102. RLERemapBlitter = new RLEBlitTransZRemapXlat<unsigned char>(&RemapTable, (unsigned char const *)Translator);
  103. RLEShadowBlitter = new RLEBlitTransRemapDest<unsigned char>(ShadowTable);
  104. RLETranslucent1Blitter = new RLEBlitTransRemapXlat<unsigned char>(ShadowTable, (unsigned char const *)Translator);
  105. RLETranslucent2Blitter = new RLEBlitTransRemapXlat<unsigned char>(ShadowTable, (unsigned char const *)Translator);
  106. RLETranslucent3Blitter = new RLEBlitTransRemapXlat<unsigned char>(ShadowTable, (unsigned char const *)Translator);
  107. } else {
  108. /*
  109. ** The hicolor translation table is constructed according to the pixel
  110. ** format of the display and the source art palette.
  111. */
  112. //assert(surface.Is_Direct_Draw());
  113. Translator = new unsigned short [256];
  114. ((DSurface &)surface).Build_Remap_Table((unsigned short *)Translator, artpalette);
  115. /*
  116. ** Fetch the pixel mask values to be used for the various algorithmic
  117. ** pixel processing performed for hicolor displays.
  118. */
  119. int maskhalf = ((DSurface &)surface).Get_Halfbright_Mask();
  120. int maskquarter = ((DSurface &)surface).Get_Quarterbright_Mask();
  121. /*
  122. ** Construct all the blitter objects necessary to support the functionality
  123. ** required for the draw permutations.
  124. */
  125. PlainBlitter = new BlitPlainXlat<unsigned short>((unsigned short const *)Translator);
  126. TransBlitter = new BlitTransXlat<unsigned short>((unsigned short const *)Translator);
  127. RemapBlitter = new BlitTransZRemapXlat<unsigned short>(&RemapTable, (unsigned short const *)Translator);
  128. ShadowBlitter = new BlitTransDarken<unsigned short>((unsigned short)maskhalf);
  129. Translucent1Blitter = new BlitTransLucent75<unsigned short>((unsigned short const *)Translator, (unsigned short)maskquarter);
  130. Translucent2Blitter = new BlitTransLucent50<unsigned short>((unsigned short const *)Translator, (unsigned short)maskhalf);
  131. Translucent3Blitter = new BlitTransLucent25<unsigned short>((unsigned short const *)Translator, (unsigned short)maskquarter);
  132. /*
  133. ** Create the RLE aware blitter objects.
  134. */
  135. RLETransBlitter = new RLEBlitTransXlat<unsigned short>((unsigned short const *)Translator);
  136. RLERemapBlitter = new RLEBlitTransZRemapXlat<unsigned short>(&RemapTable, (unsigned short const *)Translator);
  137. RLEShadowBlitter = new RLEBlitTransDarken<unsigned short>((unsigned short)maskhalf);
  138. RLETranslucent1Blitter = new RLEBlitTransLucent75<unsigned short>((unsigned short const *)Translator, (unsigned short)maskquarter);
  139. RLETranslucent2Blitter = new RLEBlitTransLucent50<unsigned short>((unsigned short const *)Translator, (unsigned short)maskhalf);
  140. RLETranslucent3Blitter = new RLEBlitTransLucent25<unsigned short>((unsigned short const *)Translator, (unsigned short)maskquarter);
  141. }
  142. }
  143. ConvertClass::~ConvertClass(void)
  144. {
  145. delete PlainBlitter;
  146. PlainBlitter = NULL;
  147. delete TransBlitter;
  148. TransBlitter = NULL;
  149. delete ShadowBlitter;
  150. ShadowBlitter = NULL;
  151. delete RemapBlitter;
  152. RemapBlitter = NULL;
  153. delete Translucent1Blitter;
  154. Translucent1Blitter = NULL;
  155. delete Translucent2Blitter;
  156. Translucent2Blitter = NULL;
  157. delete Translucent3Blitter;
  158. Translucent3Blitter = NULL;
  159. delete [] Translator;
  160. Translator = NULL;
  161. delete [] ShadowTable;
  162. ShadowTable = NULL;
  163. delete RLETransBlitter;
  164. RLETransBlitter = NULL;
  165. delete RLEShadowBlitter;
  166. RLEShadowBlitter = NULL;
  167. delete RLERemapBlitter;
  168. RLERemapBlitter = NULL;
  169. delete RLETranslucent1Blitter;
  170. RLETranslucent1Blitter = NULL;
  171. delete RLETranslucent2Blitter;
  172. RLETranslucent2Blitter = NULL;
  173. delete RLETranslucent3Blitter;
  174. RLETranslucent3Blitter = NULL;
  175. }
  176. Blitter const * ConvertClass::Blitter_From_Flags(ShapeFlags_Type flags) const
  177. {
  178. if (flags & SHAPE_REMAP) return(RemapBlitter);
  179. /*
  180. ** Quick check to see if this is a translucent operation. If so, then no
  181. ** further examination of the flags is necessary.
  182. */
  183. switch (flags & (SHAPE_TRANSLUCENT25 | SHAPE_TRANSLUCENT50 | SHAPE_TRANSLUCENT75)) {
  184. case SHAPE_TRANSLUCENT25:
  185. return(Translucent3Blitter);
  186. case SHAPE_TRANSLUCENT50:
  187. return(Translucent2Blitter);
  188. case SHAPE_TRANSLUCENT75:
  189. return(Translucent1Blitter);
  190. }
  191. if (flags & SHAPE_DARKEN) return(ShadowBlitter);
  192. if (flags & SHAPE_NOTRANS) return(PlainBlitter);
  193. return(TransBlitter);
  194. }
  195. RLEBlitter const * ConvertClass::RLEBlitter_From_Flags(ShapeFlags_Type flags) const
  196. {
  197. if (flags & SHAPE_REMAP) return(RLERemapBlitter);
  198. /*
  199. ** Quick check to see if this is a translucent operation. If so, then no
  200. ** further examination of the flags is necessary.
  201. */
  202. switch (flags & (SHAPE_TRANSLUCENT25 | SHAPE_TRANSLUCENT50 | SHAPE_TRANSLUCENT75)) {
  203. case SHAPE_TRANSLUCENT25:
  204. return(RLETranslucent3Blitter);
  205. case SHAPE_TRANSLUCENT50:
  206. return(RLETranslucent2Blitter);
  207. case SHAPE_TRANSLUCENT75:
  208. return(RLETranslucent1Blitter);
  209. }
  210. if (flags & SHAPE_DARKEN) return(RLEShadowBlitter);
  211. // This should be fixed to return the RLEPlainBlitter when one is available
  212. // but if you need to use this in the mean time just don't RLE compress the
  213. // shape (since it only compresses transparent pixels and the reason we compress
  214. // them is so we can skip them easily.)
  215. if (flags & SHAPE_NOTRANS) return(RLETransBlitter);
  216. return(RLETransBlitter);
  217. }