POLY.C 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. /*
  14. * $Source: f:/miner/source/2d/rcs/poly.c $
  15. * $Revision: 1.5 $
  16. * $Author: john $
  17. * $Date: 1994/11/13 13:03:43 $
  18. *
  19. * Graphical routines for drawing polygons.
  20. *
  21. * $Log: poly.c $
  22. * Revision 1.5 1994/11/13 13:03:43 john
  23. * Added paged out bit in bitmap structure. Commented out the
  24. * poly code that is never used.
  25. *
  26. * Revision 1.4 1994/03/14 16:56:13 john
  27. * Changed grs_bitmap structure to include bm_flags.
  28. *
  29. * Revision 1.3 1993/10/15 16:23:14 john
  30. * y
  31. *
  32. * Revision 1.2 1993/10/08 14:30:39 john
  33. * *** empty log message ***
  34. *
  35. * Revision 1.1 1993/09/08 11:44:13 john
  36. * Initial revision
  37. *
  38. *
  39. */
  40. #include "mem.h"
  41. #include "gr.h"
  42. #include "grdef.h"
  43. //#define USE_POLY_CODE 1
  44. #define MAX_SCAN_LINES 1200
  45. #ifdef USE_POLY_CODE
  46. int y_edge_list[MAX_SCAN_LINES];
  47. void gr_upoly(int nverts, int *vert )
  48. {
  49. int temp;
  50. int startx, stopx; // X coordinates of both ends of current edge.
  51. int firstx, firsty; // Saved copy of the first vertex to connect later.
  52. int dx_dy; // Slope of current edge.
  53. int miny, maxy;
  54. int starty, stopy; // Y coordinates of both ends of current edge.
  55. int x1, x2, i;
  56. // Find the min and max rows to clear out the minimun y_edge_list.
  57. // (Is it worth it?)
  58. maxy = vert[1];
  59. miny = vert[1];
  60. for (i=3; i<(nverts*2); i+=2 )
  61. {
  62. if (vert[i]>maxy) maxy=vert[i];
  63. if (vert[i]<miny) miny=vert[i];
  64. }
  65. miny >>= 16;
  66. miny--; // -1 to be safe
  67. maxy >>= 16;
  68. maxy++; // +1 to be safe
  69. // Clear only the part of the y_edge_list that w will be using
  70. if (miny < 0) miny = 0;
  71. if (maxy > MAX_SCAN_LINES) maxy = MAX_SCAN_LINES;
  72. for (i=miny; i<maxy; i++ )
  73. y_edge_list[i] = -1;
  74. // Save the first vertex so that we can connect to it at the end.
  75. firstx = vert[0];
  76. firsty = vert[1] >> 16;
  77. do
  78. {
  79. nverts--;
  80. // Get the beginning coordinates of the current edge.
  81. startx = vert[0];
  82. starty = vert[1] >> 16;
  83. // Get the ending coordinates of the current edge.
  84. if (nverts > 0 ) {
  85. stopx = vert[2];
  86. stopy = vert[3] >> 16;
  87. vert += 2;
  88. } else {
  89. stopx = firstx; // Last edge, uses first vertex as endpoint
  90. stopy = firsty;
  91. }
  92. if (stopy < starty ) {
  93. temp = stopy;
  94. stopy = starty;
  95. starty = temp;
  96. temp = stopx;
  97. stopx = startx;
  98. startx = temp;
  99. }
  100. if (stopy == starty )
  101. {
  102. // Draw a edge going horizontally across screen
  103. x1 = startx>>16;
  104. x2 = stopx>>16;
  105. if (x2 > x1 )
  106. //gr_uscanline( x1, x2-1, stopy );
  107. gr_uscanline( x1, x2, stopy );
  108. else
  109. //gr_uscanline( x2, x1-1, stopy );
  110. gr_uscanline( x2, x1, stopy );
  111. } else {
  112. dx_dy = (stopx - startx) / (stopy - starty);
  113. for (; starty < stopy; starty++ )
  114. {
  115. if (y_edge_list[starty]==-1)
  116. y_edge_list[starty] = startx;
  117. else {
  118. x1 = y_edge_list[starty]>>16;
  119. x2 = startx>>16;
  120. if (x2 > x1 )
  121. //gr_uscanline( x1, x2-1, starty );
  122. gr_uscanline( x1, x2, starty );
  123. else
  124. //gr_uscanline( x2, x1-1, starty );
  125. gr_uscanline( x2, x1, starty );
  126. }
  127. startx += dx_dy;
  128. }
  129. }
  130. } while (nverts > 0);
  131. }
  132. void gr_poly(int nverts, int *vert )
  133. {
  134. int temp;
  135. int startx, stopx; // X coordinates of both ends of current edge.
  136. int firstx, firsty; // Saved copy of the first vertex to connect later.
  137. int dx_dy; // Slope of current edge.
  138. int miny, maxy;
  139. int starty, stopy; // Y coordinates of both ends of current edge.
  140. int x1, x2, i, j;
  141. // Find the min and max rows to clear out the minimun y_edge_list.
  142. // (Is it worth it?)
  143. maxy = vert[1];
  144. miny = vert[1];
  145. j = 0;
  146. for (i=3; i<(nverts*2); i+=2 )
  147. {
  148. if (vert[i]>maxy) {
  149. if ((maxy=vert[i]) > MAXY) j++;
  150. //if (j>1) break;
  151. }
  152. if (vert[i]<miny) {
  153. if ((miny=vert[i]) < MINY) j++;
  154. //if (j>1) break;
  155. }
  156. }
  157. miny >>= 16;
  158. miny--; // -1 to be safe
  159. maxy >>= 16;
  160. maxy++; // +1 to be safe
  161. if (miny < MINY) miny = MINY;
  162. if (maxy > MAXY) maxy = MAXY+1;
  163. // Clear only the part of the y_edge_list that w will be using
  164. for (i=miny; i<maxy; i++ )
  165. y_edge_list[i] = -1;
  166. // Save the first vertex so that we can connect to it at the end.
  167. firstx = vert[0];
  168. firsty = vert[1] >> 16;
  169. do
  170. {
  171. nverts--;
  172. // Get the beginning coordinates of the current edge.
  173. startx = vert[0];
  174. starty = vert[1] >> 16;
  175. // Get the ending coordinates of the current edge.
  176. if (nverts > 0 ) {
  177. stopx = vert[2];
  178. stopy = vert[3] >> 16;
  179. vert += 2;
  180. } else {
  181. stopx = firstx; // Last edge, uses first vertex as endpoint
  182. stopy = firsty;
  183. }
  184. if (stopy < starty ) {
  185. temp = stopy;
  186. stopy = starty;
  187. starty = temp;
  188. temp = stopx;
  189. stopx = startx;
  190. startx = temp;
  191. }
  192. if (stopy == starty )
  193. {
  194. // Draw a edge going horizontally across screen
  195. if ((stopy >= MINY) && (stopy <=MAXY )) {
  196. x1 = startx>>16;
  197. x2 = stopx>>16;
  198. if (x1 > x2 ) {
  199. temp = x2;
  200. x2 = x1;
  201. x1 = temp;
  202. }
  203. if ((x1 <= MAXX ) && (x2 >= MINX))
  204. {
  205. if (x1 < MINX ) x1 = MINX;
  206. if (x2 > MAXX ) x2 = MAXX+1;
  207. //gr_uscanline( x1, x2-1, stopy );
  208. gr_scanline( x1, x2, stopy );
  209. }
  210. }
  211. } else {
  212. dx_dy = (stopx - startx) / (stopy - starty);
  213. if (starty < MINY ) {
  214. startx = dx_dy*(MINY-starty)+startx;
  215. starty = MINY;
  216. }
  217. if (stopy > MAXY ) {
  218. stopx = dx_dy*(MAXY-starty)+startx;
  219. stopy = MAXY+1;
  220. }
  221. for (; starty < stopy; starty++ )
  222. { if (y_edge_list[starty]==-1)
  223. y_edge_list[starty] = startx;
  224. else {
  225. x1 = y_edge_list[starty]>>16;
  226. x2 = startx>>16;
  227. if (x1 > x2 ) {
  228. temp = x2;
  229. x2 = x1;
  230. x1 = temp;
  231. }
  232. if ((x1 <= MAXX ) && (x2 >= MINX))
  233. {
  234. if (x1 < MINX ) x1 = MINX;
  235. if (x2 > MAXX ) x2 = MAXX+1;
  236. //gr_uscanline( x1, x2-1, starty );
  237. gr_scanline( x1, x2, starty );
  238. }
  239. }
  240. startx += dx_dy;
  241. }
  242. }
  243. } while (nverts > 0);
  244. }
  245. #endif
  246.