1
0

laserline.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifdef LASER
  25. #include <windows.h>
  26. #include <math.h>
  27. #include "../r_defs.h"
  28. extern "C" {
  29. #include "ld32.h"
  30. };
  31. static int fix(int a)
  32. {
  33. return ((a&0xff0000)>>16)|(a&0xff00)|((a&0xff)<<16);
  34. }
  35. static int dist(int x1, int y1, int x2, int y2)
  36. {
  37. x1-=x2;
  38. y1-=y2;
  39. return x1*x1+y1*y1;
  40. }
  41. static double actdist(double x1, double y1, double x2, double y2)
  42. {
  43. x1-=x2;
  44. y1-=y2;
  45. return sqrt(x1*x1+y1*y1);
  46. }
  47. static __inline int getval(int x1, int y1, int x2, int y2, int xv)
  48. {
  49. if (x1==x2) return y1;
  50. return y1+MulDiv(xv-x1,y2-y1,x2-x1);
  51. }
  52. static void doclip(int &x1, int &y1, int x2, int y2)
  53. {
  54. if (x1 > 8000 && x2 <= 8000)
  55. {
  56. y1=getval(x1,y1,x2,y2,8000);
  57. x1=8000;
  58. }
  59. if (x1 < -8000 && x2 >= -8000)
  60. {
  61. y1=getval(x1,y1,x2,y2,-8000);
  62. x1=-8000;
  63. }
  64. }
  65. void LineDrawList(C_LineListBase *list, int *fb, int w, int h)
  66. {
  67. LineType *ll;
  68. static struct
  69. {
  70. FRAMESTRUCTEX frame;
  71. PTSTRUCT points[32768];
  72. } d;
  73. memset(&d.frame,0,sizeof(d.frame));
  74. int cp=0;
  75. int lastendx=-10000,lastendy=-10000;
  76. int w2=w/2;
  77. int h2=h/2;
  78. int numl=list->GetUsedLines();
  79. ll=list->GetLineList();
  80. while (numl-->0 && cp < sizeof(d.points)/sizeof(PTSTRUCT)-2)
  81. {
  82. int x1,y1,x2,y2;
  83. // draw to screen
  84. {
  85. x1=(int) (ll->x1 * w2) + w2;
  86. x2=(int) (ll->x2 * w2) + w2;
  87. y1=(int) (ll->y1 * h2) + h2;
  88. y2=(int) (ll->y2 * h2) + h2;
  89. if (ll->mode==0)
  90. {
  91. line(fb,x1,y1,x2,y2,w,h,ll->color);
  92. }
  93. else
  94. {
  95. if (x1 >= 0 && x1 < w && y1 >= 0 && y1 < h)
  96. {
  97. int o=x1+y1*w;
  98. fb[o]=BLEND(fb[o],ll->color);
  99. }
  100. }
  101. }
  102. x1=(int) (ll->x1*8000.0);
  103. x2=(int) (ll->x2*8000.0);
  104. y1=(int) (ll->y1*-8000.0);
  105. y2=(int) (ll->y2*-8000.0);
  106. if (ll->mode==0)
  107. {
  108. doclip(x1,y1,x2,y2);
  109. doclip(x2,y2,x1,y1);
  110. doclip(y1,x1,y2,x2);
  111. doclip(y2,x2,y1,x1);
  112. if (x1 >= -8000 && x1 <= 8000 && y1 >= -8000 && y1 <= 8000 &&
  113. x2 >= -8000 && x2 <= 8000 && y2 >= -8000 && y2 <= 8000)
  114. {
  115. // x1, y1 is the new, far point
  116. if (cp && dist(x1,y1,lastendx,lastendy) < dist(x2,y2,lastendx,lastendy))
  117. {
  118. int t;
  119. t=x1; x1=x2; x2=t;
  120. t=y1; y1=y2; y2=t;
  121. }
  122. // if new start point is too far away, blank to that point
  123. if (dist(x2,y2,lastendx,lastendy) > 400*400 || !cp)
  124. {
  125. memset(&d.points[cp],0,sizeof(d.points[0]));
  126. if (cp)
  127. {
  128. d.points[cp-1].Status=4096;
  129. }
  130. d.points[cp].Status=0;
  131. d.points[cp].XCoord=x2;
  132. d.points[cp].YCoord=y2;
  133. cp++;
  134. }
  135. else if (cp>1 && d.points[cp-1].RGBValue)
  136. {
  137. double a1=atan2(d.points[cp-2].XCoord-x2,d.points[cp-2].YCoord-y2);
  138. double a2=atan2(x2-x1,y2-y1);
  139. if (fabs(a1-a2) >= 1.0*3.14159/180.0)
  140. {
  141. d.points[cp-1].Status=4096;
  142. }
  143. }
  144. lastendx=x1;
  145. lastendy=y1;
  146. memset(&d.points[cp],0,sizeof(d.points[0]));
  147. d.points[cp].RGBValue=fix(ll->color);
  148. d.points[cp].Status=0;
  149. d.points[cp].XCoord=x1;
  150. d.points[cp].YCoord=y1;
  151. cp++;
  152. }
  153. }
  154. else
  155. {
  156. if (x1 > -8000 && x1 < 8000 && y1 > -8000 && y1 < 8000)
  157. {
  158. if (dist(x1,y1,lastendx,lastendy) > 30*30)
  159. {
  160. memset(&d.points[cp],0,sizeof(d.points[0]));
  161. d.points[cp].Status=0;
  162. d.points[cp].XCoord=x1;
  163. d.points[cp].YCoord=y1;
  164. cp++;
  165. }
  166. memset(&d.points[cp],0,2*sizeof(d.points[0]));
  167. d.points[cp].RGBValue=fix(ll->color);
  168. d.points[cp].Status=4096;
  169. d.points[cp].XCoord=x1;
  170. d.points[cp].YCoord=y1;
  171. cp++;
  172. d.points[cp].Status=4096;
  173. d.points[cp].XCoord=x1;
  174. d.points[cp].YCoord=y1;
  175. cp++;
  176. lastendx=x1;
  177. lastendy=y1;
  178. }
  179. }
  180. ll++;
  181. }
  182. if (cp)
  183. {
  184. d.points[cp-1].Status=4096;
  185. }
  186. memset(&d.frame,0,sizeof(d.frame));
  187. d.frame.VectorFlag=1;
  188. d.frame.NumPoints=max(cp,1);
  189. d.frame.ScanRate=100;
  190. memcpy(d.frame.FrameNote,"AVS/Laser Frame ",24);
  191. laser_sendframe(&d,sizeof(d.frame)+sizeof(PTSTRUCT)*max(cp,16));
  192. }
  193. #endif