linedraw.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <windows.h>
  25. #include "r_defs.h"
  26. #include <math.h>
  27. #define SWAP(x,y,temp) ( temp ) = ( x ); ( x ) = ( y ); ( y ) = ( temp )
  28. #define ABS(x) (( x ) < 0 ? - ( x ) : ( x ))
  29. void line(int *fb, int x1,int y1,int x2,int y2, int width, int height, int color, int lw)
  30. {
  31. int dy = ABS(y2-y1);
  32. int dx = ABS(x2-x1);
  33. #ifdef LASER
  34. lw=1;
  35. #define BLEND_LINE(fb,color) (*(fb)=BLEND(*(fb),(color)))
  36. #else
  37. if (lw<1) lw=1;
  38. else if (lw>255)lw=255;
  39. #endif
  40. int lw2=lw/2;
  41. if (!dx) // optimize vertical draw
  42. {
  43. x1-=lw2;
  44. if (x1+lw >= 0 && x1 < width)
  45. {
  46. int d=max(min(y1,y2),0);
  47. int ye=min(max(y1,y2),height-1);
  48. if (x1<0)
  49. {
  50. lw+=x1;
  51. x1=0;
  52. }
  53. if (x1+lw >= width) lw=width-x1;
  54. fb += d*width+x1;
  55. width-=lw;
  56. if (lw>0) while (d++ < ye)
  57. {
  58. int x=lw;
  59. while (x--) { BLEND_LINE(fb, color); fb++; }
  60. fb+=width;
  61. }
  62. }
  63. return;
  64. }
  65. if (y1==y2) // optimize horizontal draw.
  66. {
  67. y1-=lw2;
  68. if (y1+lw >= 0 && y1 < height)
  69. {
  70. int d=max(min(x1,x2),0);
  71. int xe=min(max(x1,x2),width-1);
  72. if (y1<0)
  73. {
  74. lw+=y1;
  75. y1=0;
  76. }
  77. if (y1+lw >= height) lw=height-y1;
  78. fb+=y1*width+d;
  79. width-=xe-d;
  80. int y=lw;
  81. while (y--)
  82. {
  83. int lt=d;
  84. while (lt++<xe) { BLEND_LINE(fb,color); fb++; }
  85. fb+=width;
  86. }
  87. }
  88. return;
  89. }
  90. if (dy <= dx) // x major, low slope
  91. {
  92. // first things first for better line drawing, let's see if we can't get the
  93. // width calculated right.
  94. // lw is the width in pixels. if dy = 0, then lw=lw. if dy=dx, then:
  95. /*
  96. __________ C=30d
  97. |\
  98. | \ lw
  99. | \_____ A=90d
  100. ? | /
  101. | / pc2
  102. |/ ____ B = 60d
  103. tan(C) = sin(C)/cos(C) = (dy / dx)
  104. cos(C) = lw / ?
  105. sin(C)/(lw / ? ) = (dy / dx)
  106. sin(C) * ? / lw = (dy/dx)
  107. sin(C) * ? = lw * dy/dx;
  108. ? = lw * dy/dx / sin(C)
  109. cos(C) = lw / ? === ? = lw / cos(C)
  110. tan(C) = dy / dx;
  111. C = atan2(dy,dx);
  112. ? = lw / cos(C)
  113. */
  114. #if 0// lame
  115. if (lw>1 && (GetAsyncKeyState(VK_SHIFT)&0x8000)){
  116. double d=atan2((double)dy,(double)dx);
  117. lw = (int) (lw / cos(d));
  118. if (lw<1)lw=1;
  119. }
  120. #endif
  121. if (x2 < x1)
  122. {
  123. int temp;
  124. SWAP(x1,x2,temp);
  125. SWAP(y1,y2,temp);
  126. }
  127. int yincr = y2>y1?1:-1;
  128. int offsincr= y2>y1?width:-width;
  129. y1-=lw2;
  130. int offs = y1 * width + x1;
  131. int d = dy + dy - dx;
  132. int Eincr = dy + dy;
  133. int NEincr = d - dx;
  134. if (x2 >= 0 && x1 < width)
  135. {
  136. if (x1<0)
  137. {
  138. int v;
  139. v=yincr * -x1;
  140. if (dx) v= (v * dy)/dx;
  141. y1 += v;
  142. offs += v*width - x1;
  143. x1=0;
  144. }
  145. if (x2 > width) x2=width;
  146. while (x1<x2)
  147. {
  148. int yp=y1;
  149. int ype=y1+lw;
  150. int *newfb=fb+offs;
  151. if (yp < 0)
  152. {
  153. newfb-=yp*width;
  154. yp=0;
  155. }
  156. if (ype>height) ype=height;
  157. while (yp++ < ype)
  158. {
  159. BLEND_LINE(newfb,color);
  160. newfb+=width;
  161. }
  162. if (d < 0) d += Eincr;
  163. else
  164. {
  165. d += NEincr;
  166. y1 += yincr;
  167. offs += offsincr;
  168. }
  169. offs++;
  170. x1++;
  171. }
  172. }
  173. }
  174. else
  175. {
  176. #if 0//lame
  177. if (lw>1 && (GetAsyncKeyState(VK_SHIFT)&0x8000)){
  178. double d=atan2((double)dx,(double)dy);
  179. lw = (int) (lw / cos(d));
  180. if (lw<1)lw=1;
  181. }
  182. #endif
  183. if (y2 < y1)
  184. {
  185. int temp;
  186. SWAP(x1,x2,temp);
  187. SWAP(y1,y2,temp);
  188. }
  189. int yincr=(x2>x1)?1:-1;
  190. int d = dx + dx - dy;
  191. int Eincr = dx + dx;
  192. int NEincr = d - dy;
  193. x1-=lw2;
  194. int offs = y1 * width + x1;
  195. if (y2 >= 0 && y1 < height)
  196. {
  197. if (y1<0)
  198. {
  199. int v;
  200. v=yincr * -y1;
  201. if (dy) v= (v * dx)/dy;
  202. x1 += v;
  203. offs += v - y1*width;
  204. y1=0;
  205. }
  206. if (y2 > height) y2 = height;
  207. while (y1 < y2)
  208. {
  209. int xp=x1;
  210. int xpe=x1+lw;
  211. int *newfb=fb+offs;
  212. if (xp<0)
  213. {
  214. newfb-=xp;
  215. xp=0;
  216. }
  217. if (xpe > width) xpe=width;
  218. while (xp++ < xpe) { BLEND_LINE(newfb, color); newfb++; }
  219. if (d < 0) d += Eincr;
  220. else
  221. {
  222. d += NEincr;
  223. x1 += yincr;
  224. offs += yincr;
  225. }
  226. offs += width;
  227. y1++;
  228. }
  229. }
  230. }
  231. }