ROTBMP.CPP 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/ROTBMP.CPP 1 3/03/97 10:25a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : ROTBMP.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/05/96 *
  26. * *
  27. * Last Update : July 5, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #if (0)//PG
  33. //#include "function.h"
  34. #include "watcom.h"
  35. #include "rotbmp.h"
  36. #define FILE_H
  37. #define WWMEM_H
  38. #include <wwlib32.h>
  39. int Rotate_Bitmap(GraphicViewPortClass * srcvp, GraphicViewPortClass * destvp, int angle);
  40. struct WPPOINT {
  41. int x;
  42. int y;
  43. };
  44. /***************************************************************************
  45. * Rotate_bitmap -- rotate a bitmap from srcvp to destvp *
  46. * *
  47. * INPUT: note that angles are 0 - 255 (best if used in increments of 4!) *
  48. * *
  49. * OUTPUT: *
  50. * *
  51. * WARNINGS: *
  52. * destvp should be a square. width and height should be equal to : *
  53. * MAX(srcvp->width,srcvp->height) * 1.7 (square root of 2!!) *
  54. * *
  55. * *
  56. * HISTORY: *
  57. * 01/02/1996 BP : Created. *
  58. *=========================================================================*/
  59. Rotate_Bitmap(GraphicViewPortClass * srcvp, GraphicViewPortClass * destvp, int a)
  60. {
  61. int shift = 7;
  62. int fixpoint1 = 1 << shift; // this is a fixed point 1
  63. int Deltax;
  64. int Deltay;
  65. int sx,sy,dx,dy;
  66. int Error = 0;
  67. int Decimal = 0;
  68. // this is used if I walk in Y
  69. int buffwidth = srcvp->Get_Width() + srcvp->Get_XAdd();
  70. int buffwid2 = destvp->Get_Width() + destvp->Get_XAdd();
  71. char * dest;
  72. char * src;
  73. int sa,ca;
  74. int t;
  75. int x,y;
  76. int rx,ry;
  77. int w,h;
  78. int dw = destvp->Get_Width();
  79. int dh = destvp->Get_Height();
  80. WPPOINT sp[4];
  81. WPPOINT dp[4];
  82. sa = Sin256[a];
  83. ca = Cos256[a];
  84. // get rectangle size
  85. x = 0;
  86. y = 0;
  87. w = srcvp->Get_Width();
  88. h = srcvp->Get_Height();
  89. int halfws = w >> 1;
  90. int halfhs = h >> 1;
  91. int halfwd = dw >> 1;
  92. int halfhd = dh >> 1;
  93. // make the src rectangle
  94. sp[0].x = x;
  95. sp[0].y = y;
  96. sp[1].x = x + w;
  97. sp[1].y = y;
  98. // now calculate the rotated rectangle
  99. dp[0].x = ( ((sp[0].x - halfws) * ca) - ((sp[0].y - halfhs) * sa) ) >> shift;
  100. dp[0].x += halfwd;
  101. dp[0].y = ( ((sp[0].x - halfws) * sa) + ((sp[0].y - halfhs) * ca) ) >> shift;
  102. dp[0].y += halfhd;
  103. dp[1].x = ( ((sp[1].x - halfws) * ca) - ((sp[1].y - halfhs) * sa) ) >> shift;
  104. dp[1].x += halfwd;
  105. dp[1].y = ( ((sp[1].x - halfws) * sa) + ((sp[1].y - halfhs) * ca) ) >> shift;
  106. dp[1].y += halfhd;
  107. rx = dp[0].x;
  108. ry = dp[0].y;
  109. // now calculate slope
  110. // diff from new to old x
  111. Deltax = (dp[1].x - dp[0].x);
  112. // diff from new to old y
  113. Deltay = (dp[1].y - dp[0].y);
  114. // handle the easy cases
  115. // no change in x
  116. int r;
  117. if (!Deltax) { // must be 90 or 270 degree transpose!
  118. if (Deltay < 0) {
  119. // walk across source in the x + dir
  120. // walk across dest in the y - dir
  121. x = 0;
  122. dy = 0;
  123. dx = 0;
  124. for (t = 0; t< h; t++) {
  125. x = 0;
  126. src = MAKE_PTR(srcvp, x, y);
  127. dest = MAKE_PTR(destvp, rx + dx, ry - dy);
  128. for (r = 0; r< w; r++) {
  129. // transparency
  130. if (* src)
  131. *dest = *src;
  132. src++;
  133. dest -= buffwid2;
  134. }
  135. y++;
  136. dx++;
  137. }
  138. } else {
  139. // walk across source in the x + dir
  140. // walk across dest in the y + dir
  141. x = 0;
  142. dy = 0;
  143. dx = 0;
  144. for (t = 0; t< h; t++) {
  145. x = 0;
  146. src = MAKE_PTR(srcvp, x, y);
  147. dest = MAKE_PTR(destvp, rx - dx, ry + dy);
  148. for (r = 0; r< w; r++) {
  149. // transparency
  150. if (*src)
  151. *dest = *src;
  152. src++;
  153. dest += buffwid2;
  154. }
  155. y++;
  156. dx++;
  157. }
  158. }
  159. return 0;
  160. }
  161. // no change in y
  162. if (!Deltay) { // must be 0 or 180 degree transpose !
  163. if (Deltax < 0) {
  164. y = 0;
  165. for (y = 0; y< h; y++) {
  166. x = 0;
  167. src = MAKE_PTR(srcvp, x, y);
  168. dest = MAKE_PTR(destvp, rx - x , ry - y);
  169. for (x = 0 ; x< w; x++) {
  170. // transparency
  171. if (*src)
  172. *dest = *src;
  173. dest--;
  174. src++;
  175. }
  176. }
  177. } else {
  178. for (y = 0; y< h; y++) {
  179. x = 0;
  180. src = MAKE_PTR(srcvp, x, y);
  181. dest = MAKE_PTR(destvp, rx + x, ry + y);
  182. for (x = 0 ; x< w; x++) {
  183. // transparency
  184. if (*src)
  185. *dest = *src;
  186. dest++;
  187. src++;
  188. }
  189. }
  190. }
  191. return 0;
  192. }
  193. // ok now the hard part
  194. // make them 16.16
  195. if ( ABS(Deltax) < ABS(Deltay)) { // ok this means we want to walk in y
  196. // walk in + x in the src and
  197. // walk in + y in the dest
  198. Error = 0;
  199. // start at left top corner in src and dest
  200. sx = 0;
  201. sy = 0;
  202. dx = 0;
  203. dy = 0;
  204. dest = MAKE_PTR(destvp, rx + dx, ry + dy);
  205. src = MAKE_PTR(srcvp, x + sx, y + sy);
  206. // this gets added to error each inc of x
  207. // when error is > 1 then inc y!
  208. int xinc = 1;
  209. if (Deltax < 0) {
  210. Deltax = -Deltax;
  211. xinc = -1;
  212. }
  213. int yinc = 1;
  214. int yinc1 = 1;
  215. if (Deltay < 0) {
  216. Deltay = - Deltay;
  217. buffwid2 = - buffwid2;
  218. }
  219. Decimal = ( Deltax << shift) / Deltay ;
  220. // walk in X
  221. int Deltax2 = Deltax << shift;
  222. int Deltay2 = Deltay << shift;
  223. // this is the ratio between the source height and the dest height
  224. // as the rectangle rotates the height and width change
  225. int DeltaH = (w << shift) / Deltay;
  226. int Error2 = 0;
  227. sy = 0;
  228. for (int r = 0; r< h ;r++) {
  229. // now we walk across the top calculating each rotated point
  230. // along the side
  231. // the use delta formula to walk in x and y in destination space
  232. // always walking in the x in the source!
  233. // figure out rotated location to start in dest
  234. rx = ( ( (sx - halfws) * ca) - ( (sy - halfhs) * sa) ) >> shift;
  235. rx += halfwd;
  236. ry = ( ( (sx - halfws) * sa) + ( (sy - halfhs) * ca) ) >> shift;
  237. ry += halfhd;
  238. // this is the end point of the line
  239. int y2 = ( ( ((w) - halfws) * sa) + ( (sy - halfhs) * ca) ) >> shift;
  240. y2 += halfhd;
  241. // length of line
  242. int diff = ABS(y2 - ry);
  243. // if walking backwards reveres diff to reflect sign
  244. src = MAKE_PTR(srcvp, x, y + sy);
  245. dest = MAKE_PTR(destvp, rx, ry);
  246. Error = 0;
  247. Error2 = 0;
  248. char * baseptr = src;
  249. // while walking line
  250. while (diff--) {
  251. char c = *src;
  252. // transparency
  253. if (c)
  254. *dest = *src;
  255. Error2 += DeltaH;
  256. dest += buffwid2;
  257. Error += Decimal;
  258. src = baseptr + (Error2 >> shift) ;
  259. // this is time to inc x in src y in dest
  260. if (Error >= fixpoint1) {
  261. Error -= fixpoint1;
  262. if (*src)
  263. *dest = *src;
  264. dest += xinc;
  265. }
  266. }
  267. sy += yinc;
  268. }
  269. return 0;
  270. } else { // else we walk in X
  271. int lasterror = 0;
  272. Error = 0;
  273. // start at left top corner in src and dest
  274. sx = 0;
  275. sy = 0;
  276. dx = 0;
  277. dy = 0;
  278. dest = MAKE_PTR(destvp, rx + dx, ry + dy);
  279. src = MAKE_PTR(srcvp, x + sx, y + sy);
  280. // this gets added to error each inc of x
  281. // when error is > 1 then inc y!
  282. int xinc = 1;
  283. if (Deltax < 0) {
  284. Deltax = -Deltax;
  285. xinc = -1;
  286. }
  287. int yinc = 1;
  288. if (Deltay < 0) {
  289. Deltay = - Deltay;
  290. buffwid2 = - buffwid2;
  291. sy = sy + h - 1;
  292. yinc = -1;
  293. }
  294. Decimal = ( Deltay << shift) / Deltax ;
  295. // walk in X
  296. int Deltax2 = Deltax << shift;
  297. int Deltay2 = Deltay << shift;
  298. // this is the ratios between the source width and the dest width
  299. // as the rectangle rotates the actual size changes!
  300. int DeltaW = (w << shift) / Deltax;
  301. int Error2 = 0;
  302. for (int r = 0; r< h ;r++) {
  303. sx = 0;
  304. // now we walk across the side calculating each rotated point
  305. // along the side
  306. // the use delta formula to walk in x and y in destination space
  307. // always walking in the x in the source!
  308. // figure out rotated location to start
  309. rx = ( ( (sx - halfws) * ca) - ( (sy - halfhs) * sa) ) >> shift;
  310. rx += halfwd;
  311. ry = ( ( (sx - halfws) * sa) + ( (sy - halfhs) * ca) ) >> shift;
  312. ry += halfhd;
  313. // this is the other side of the box
  314. int x2 = ( ( ((sx + w) - halfws) * ca) - ( (sy - halfhs) * sa) ) >> shift;
  315. x2 += halfwd;
  316. int diff = x2 - rx;
  317. dx = 0;
  318. dy = 0;
  319. if (xinc == -1) {
  320. diff = -diff;
  321. }
  322. src = MAKE_PTR(srcvp, x + sx, y + sy);
  323. dest = MAKE_PTR(destvp, rx + dx, ry + dy);
  324. Error = 0;
  325. Error2 = 0;
  326. char * baseptr = src;
  327. while (diff--) {
  328. char c = *src;
  329. // transparency
  330. if (c)
  331. *dest = *src;
  332. Error2 += DeltaW;
  333. rx++;
  334. dest += xinc;
  335. Error += Decimal;
  336. src = baseptr + (Error2 >> shift);
  337. if (Error >= fixpoint1) {
  338. Error -= fixpoint1;
  339. if (*src)
  340. *dest = *src;
  341. dest += buffwid2;
  342. }
  343. }
  344. sy += yinc;
  345. }
  346. }
  347. return 0;
  348. }
  349. #endif