SPRITE.CPP 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /* $Header: /CounterStrike/SPRITE.CPP 1 3/03/97 10:25a Joe_bostic $ */
  15. /**********************************************************************
  16. Sprite.cpp
  17. Dec 28,1995
  18. GraphicBufferClass member functions for blitting, scaling,
  19. and rotating bitmaps
  20. **********************************************************************/
  21. #ifndef WIN32
  22. class BitmapClass
  23. {
  24. public:
  25. BitmapClass(int w, int h, unsigned char * data) :
  26. Width(w), Height(h), Data(data) {};
  27. int Width;
  28. int Height;
  29. unsigned char * Data;
  30. };
  31. class TPoint2D
  32. {
  33. public:
  34. TPoint2D(int xx, int yy) : x(xx), y(yy) {};
  35. TPoint2D(void) : x(0), y(0) {};
  36. int x;
  37. int y;
  38. };
  39. #endif
  40. //#include "function.h"
  41. #define FILE_H
  42. #define RAWFILE_H
  43. #define WWMEM_H
  44. #define WWFILE_Hx
  45. #include <wwlib32.h>
  46. #include <stdio.h>
  47. //#include "gbuffer.h"
  48. //#include "math.h"
  49. long _SineTab[256] = {
  50. 0 ,6 ,12 , 18 , 25 , 31 , 37 , 43 , 49 , 56 , 62 , 68 , 74 , 80 , 86 ,
  51. 92 , 97 , 103 , 109 , 115 , 120 , 126 , 131 , 136 , 142 , 147 , 152 ,
  52. 157 , 162 , 167 , 171 , 176 , 180 , 185 , 189 , 193 , 197 , 201 , 205 ,
  53. 209 , 212 , 216 , 219 , 222 , 225 , 228 , 231 , 233 , 236 , 238 , 240 ,
  54. 243 , 244 , 246 , 248 , 249 , 251 , 252 , 253 , 254 , 254 , 255 , 255 ,
  55. 255 , 255 , 255 , 255 , 255 , 254 , 254 , 253 , 252 , 251 , 249 , 248 ,
  56. 246 , 245 , 243 , 241 , 238 , 236 , 234 , 231 , 228 , 225 , 222 , 219 ,
  57. 216 , 213 , 209 , 205 , 201 , 198 , 194 , 189 , 185 , 181 , 176 , 172 ,
  58. 167 , 162 , 157 , 152 , 147 , 142 , 137 , 131 , 126 , 120 , 115 , 109 ,
  59. 104 , 98 , 92 , 86 , 80 , 74 , 68 , 62 , 56 , 50 , 44 , 37 , 31 , 25 ,
  60. 19 , 12 , 6 , 0 , -5 , -12 , -18 , -24 , -30 , -37 , -43 , -49 , -55 ,
  61. -61 , -67 , -73 , -79 , -85 , -91 , -97 , -103 , -109 , -114 , -120 ,
  62. -125 , -131 , -136 , -141 , -147 , -152 , -157 , -162 , -166 , -171 ,
  63. -176 , -180 , -185 , -189 , -193 , -197 , -201 , -205 , -208 , -212 ,
  64. -215 , -219 , -222 , -225 , -228 , -231 , -233 , -236 , -238 , -240 ,
  65. -242 , -244 , -246 , -248 , -249 , -250 , -252 , -253 , -254 , -254 ,
  66. -255 , -255 , -255 , -255 , -255 , -255 , -255 , -254 , -254 , -253 ,
  67. -252 , -251 , -249 , -248 , -246 , -245 , -243 , -241 , -239 , -236 ,
  68. -234 , -231 , -228 , -226 , -223 , -219 , -216 , -213 , -209 , -206 ,
  69. -202 , -198 , -194 , -190 , -185 , -181 , -177 , -172 , -167 , -162 ,
  70. -158 , -153 , -148 , -142 , -137 , -132 , -126 , -121 , -115 , -110 ,
  71. -104 , -98 , -92 , -86 , -81 , -75 , -69 , -62 , -56 , -50 , -44 ,
  72. -38 , -32 , -25 , -19 , -13 , -7 ,
  73. };
  74. long _CosineTab[256] = {
  75. 256 , 255 , 255 , 255 , 254 , 254 , 253 , 252 , 251 , 249 , 248 , 246 ,
  76. 244 , 243 , 241 , 238 , 236 , 234 , 231 , 228 , 225 , 222 , 219 , 216 ,
  77. 212 , 209 , 205 , 201 , 197 , 193 , 189 , 185 , 181 , 176 , 171 , 167 ,
  78. 162 , 157 , 152 , 147 , 142 , 137 , 131 , 126 , 120 , 115 , 109 , 103 ,
  79. 98 , 92 , 86 , 80 , 74 , 68 , 62 , 56 , 50 , 43 , 37 , 31 , 25 , 19 ,
  80. 12 , 6 , 0 , -6 , -12 , -18 , -24 , -31 , -37 , -43 , -49 , -55 , -61 ,
  81. -68 , -74 , -80 , -86 , -91 , -97 , -103 , -109 , -114 , -120 , -125 , -131 ,
  82. -136 , -141 , -147 , -152 , -157 , -162 , -166 , -171 , -176 , -180 ,
  83. -185 , -189 , -193 , -197 , -201 , -205 , -209 , -212 , -216 , -219 , -222 ,
  84. -225 , -228 , -231 , -233 , -236 , -238 , -240 , -242 , -244 , -246 ,
  85. -248 , -249 , -251 , -252 , -253 , -254 , -254 , -255 , -255 , -255 ,
  86. -255 , -255 , -255 , -255 , -254 , -254 , -253 , -252 , -251 , -249 , -248 ,
  87. -246 , -245 , -243 , -241 , -239 , -236 , -234 , -231 , -228 , -225 ,
  88. -222 , -219 , -216 , -213 , -209 , -205 , -202 , -198 , -194 , -190 , -185 ,
  89. -181 , -176 , -172 , -167 , -162 , -157 , -152 , -147 , -142 , -137 , -132 ,
  90. -126 , -121 , -115 , -109 , -104 , -98 , -92 , -86 , -80 , -74 , -68 ,
  91. -62 , -56 , -50 , -44 , -38 , -31 , -25 , -19 , -13 , -6 , 0 , 5 , 11 ,
  92. 18 , 24 , 30 , 36 , 43 , 49 , 55 , 61 , 67 , 73 , 79 , 85 , 91 , 97 , 103 ,
  93. 108 , 114 , 120 , 125 , 131 , 136 , 141 , 146 , 151 , 156 , 161 , 166 ,
  94. 171 , 176 , 180 , 184 , 189 , 193 , 197 , 201 , 205 , 208 , 212 , 215 ,
  95. 219 , 222 , 225 , 228 , 231 , 233 , 236 , 238 , 240 , 242 , 244 , 246 ,
  96. 248 , 249 , 250 , 252 , 253 , 253 , 254 , 255 , 255 , 255 ,
  97. };
  98. /***************************************************************
  99. *
  100. * Scale_Rotate
  101. *
  102. * FUNCTION:
  103. *
  104. * Using Bi-Linear Interpolation, draws a scaled and rotated
  105. * bitmap onto the buffer. No clipping is performed so beware.
  106. *
  107. * INPUTS
  108. *
  109. * bmp - bitmap to draw
  110. * pt - desired position of the center
  111. * scale - 24.8 fixed point scale factor
  112. * angle - 8bit angle (0=0deg, 255=360deg)
  113. *
  114. ***************************************************************/
  115. void GraphicBufferClass::Scale_Rotate(BitmapClass &bmp,TPoint2D const &pt,long scale,unsigned char angle)
  116. {
  117. unsigned int scrpos;
  118. unsigned int temp;
  119. int i,j; // counter vars
  120. int pxerror =0; // these three vars will be used in an
  121. int pyerror =0; // integer difference alg to keep track
  122. int pixpos =0; // of what pixel to draw.
  123. unsigned char pixel;
  124. TPoint2D p0; // "upper left" corner of the rectangle
  125. TPoint2D p1; // "upper right" corner of the rectangle
  126. TPoint2D p2; // "lower left" corner of the rectangle
  127. /*-------------------------------------------------
  128. Compute three corner points of the rectangle
  129. -------------------------------------------------*/
  130. {
  131. angle &= 0x0FF;
  132. long c = _CosineTab[angle];
  133. long s = _SineTab[angle];
  134. long W = (scale*bmp.Width)>>1;
  135. long L = (scale*bmp.Height)>>1;
  136. p0.x = (pt.x + ((( (L*c) >> 8)-((W*s) >> 8)) >> 8));
  137. p0.y = (pt.y + (((-(L*s) >> 8)-((W*c) >> 8)) >> 8));
  138. p1.x = (pt.x + ((( (L*c) >> 8)+((W*s) >> 8)) >> 8));
  139. p1.y = (pt.y + (((-(L*s) >> 8)+((W*c) >> 8)) >> 8));
  140. p2.x = (pt.x + (((-(L*c) >> 8)-((W*s) >> 8)) >> 8));
  141. p2.y = (pt.y + ((( (L*s) >> 8)-((W*c) >> 8)) >> 8));
  142. }
  143. /*-----------------------------------
  144. Initialize Breshnam constants
  145. -----------------------------------*/
  146. // This breshnam line goes across the FRONT of the rectangle
  147. // In the bitmap, this will step from left to right
  148. int f_deltax =(p1.x-p0.x);
  149. int f_deltay =(p1.y-p0.y);
  150. int f_error =0;
  151. int f_xstep =1;
  152. int f_ystep =Width;
  153. // This breshnam line goes down the SIDE of the rectangle
  154. // In the bitmap, this line will step from top to bottom
  155. int s_deltax =(p2.x-p0.x);
  156. int s_deltay =(p2.y-p0.y);
  157. int s_error =0;
  158. int s_xstep =1;
  159. int s_ystep =Width;
  160. /*--------------------------------
  161. fixup deltas and step values
  162. --------------------------------*/
  163. if (f_deltay<0) {
  164. f_deltay=-f_deltay;
  165. f_ystep=-Width;
  166. };
  167. if (f_deltax<0) {
  168. f_deltax=-f_deltax;
  169. f_xstep=-1;
  170. };
  171. if (s_deltay<0) {
  172. s_deltay=-s_deltay;
  173. s_ystep=-Width;
  174. };
  175. if (s_deltax<0) {
  176. s_deltax=-s_deltax;
  177. s_xstep=-1;
  178. };
  179. scrpos=p0.x+Width*p0.y; //address of initial screen pos.
  180. temp=scrpos;
  181. /*---------------------------------------------------------------------
  182. Now all of the differences, errors, and steps are set up so we can
  183. begin drawing the bitmap...
  184. There are two cases here,
  185. 1 - the "Front" line has a slope of < 1.0 (45 degrees)
  186. 2 - the "Front" line has a slope of >= 1.0
  187. For case 1, we step along the X direction, for case 2, step in y
  188. ---------------------------------------------------------------------*/
  189. if (f_deltax>f_deltay) { //CASE 1, step front in X, side in Y
  190. // outer loop steps from top to bottom of the rectangle
  191. for (j=0;j<s_deltay;j++)
  192. {
  193. temp=scrpos;
  194. // The inner loop steps across the rectangle
  195. for (i=0;i<f_deltax;i++)
  196. {
  197. pixel=bmp.Data[pixpos]; //read pixel
  198. if (pixel) ((unsigned char *)Get_Buffer())[scrpos]=pixel; //draw if not transparent
  199. // if (pixel) Data[scrpos]=pixel; //draw if not transparent
  200. pxerror+=bmp.Width; //update position in bitmap
  201. while (pxerror>f_deltax)
  202. {
  203. pixpos++;
  204. pxerror-=f_deltax;
  205. };
  206. scrpos+=f_xstep; //step to next screen pos
  207. f_error+=f_deltay;
  208. if (f_error>f_deltax)
  209. {
  210. if (pixel) ((unsigned char *)Get_Buffer())[scrpos]=pixel;
  211. f_error-=f_deltax;
  212. scrpos+=f_ystep;
  213. };
  214. };
  215. pxerror=0;
  216. pixpos-=bmp.Width-1;
  217. pyerror+=bmp.Height;
  218. while (pyerror>s_deltay)
  219. {
  220. pixpos+=bmp.Width;
  221. pyerror-=s_deltay;
  222. };
  223. f_error=0;
  224. scrpos=temp;
  225. scrpos+=s_ystep;
  226. s_error+=s_deltax;
  227. if (s_error>s_deltay)
  228. {
  229. s_error-=s_deltay;
  230. scrpos+=s_xstep;
  231. };
  232. }
  233. } else { // CASE 2, Step front line in X, side line in Y
  234. // outer loop steps from top to bottom of the rectangle
  235. for (j=0;j<s_deltax;j++)
  236. {
  237. temp=scrpos;
  238. // The inner loop steps across the rectangle
  239. for (i=0;i<f_deltay;i++)
  240. {
  241. pixel=bmp.Data[pixpos]; //read pixel
  242. if (pixel) ((unsigned char *)Get_Buffer())[scrpos]=pixel; //draw if not transparent
  243. pxerror+=bmp.Width; //update position in bitmap
  244. while (pxerror>f_deltay)
  245. {
  246. pixpos++;
  247. pxerror-=f_deltay;
  248. };
  249. scrpos+=f_ystep; //step to next screen pos
  250. f_error+=f_deltax;
  251. if (f_error>f_deltay)
  252. {
  253. if (pixel) ((unsigned char *)Get_Buffer())[scrpos]=pixel;
  254. f_error-=f_deltay;
  255. scrpos+=f_xstep;
  256. };
  257. };
  258. pxerror=0;
  259. pixpos-=bmp.Width-1;
  260. pyerror+=bmp.Height;
  261. while (pyerror>s_deltax)
  262. {
  263. pixpos+=bmp.Width;
  264. pyerror-=s_deltax;
  265. };
  266. scrpos=temp;
  267. scrpos+=s_xstep;
  268. s_error+=s_deltay;
  269. f_error=0;
  270. if (s_error>s_deltax)
  271. {
  272. s_error-=s_deltax;
  273. scrpos+=s_ystep;
  274. };
  275. }
  276. }
  277. }