SPRITE.CPP 9.8 KB

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