POLY.C 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
  12. */
  13. #include "mem.h"
  14. #include "gr.h"
  15. #include "grdef.h"
  16. //#define USE_POLY_CODE 1
  17. #define MAX_SCAN_LINES 1200
  18. #ifdef USE_POLY_CODE
  19. int y_edge_list[MAX_SCAN_LINES];
  20. void gr_upoly(int nverts, int *vert )
  21. {
  22. int temp;
  23. int startx, stopx; // X coordinates of both ends of current edge.
  24. int firstx, firsty; // Saved copy of the first vertex to connect later.
  25. int dx_dy; // Slope of current edge.
  26. int miny, maxy;
  27. int starty, stopy; // Y coordinates of both ends of current edge.
  28. int x1, x2, i;
  29. // Find the min and max rows to clear out the minimun y_edge_list.
  30. // (Is it worth it?)
  31. maxy = vert[1];
  32. miny = vert[1];
  33. for (i=3; i<(nverts*2); i+=2 )
  34. {
  35. if (vert[i]>maxy) maxy=vert[i];
  36. if (vert[i]<miny) miny=vert[i];
  37. }
  38. miny >>= 16;
  39. miny--; // -1 to be safe
  40. maxy >>= 16;
  41. maxy++; // +1 to be safe
  42. // Clear only the part of the y_edge_list that w will be using
  43. if (miny < 0) miny = 0;
  44. if (maxy > MAX_SCAN_LINES) maxy = MAX_SCAN_LINES;
  45. for (i=miny; i<maxy; i++ )
  46. y_edge_list[i] = -1;
  47. // Save the first vertex so that we can connect to it at the end.
  48. firstx = vert[0];
  49. firsty = vert[1] >> 16;
  50. do
  51. {
  52. nverts--;
  53. // Get the beginning coordinates of the current edge.
  54. startx = vert[0];
  55. starty = vert[1] >> 16;
  56. // Get the ending coordinates of the current edge.
  57. if (nverts > 0 ) {
  58. stopx = vert[2];
  59. stopy = vert[3] >> 16;
  60. vert += 2;
  61. } else {
  62. stopx = firstx; // Last edge, uses first vertex as endpoint
  63. stopy = firsty;
  64. }
  65. if (stopy < starty ) {
  66. temp = stopy;
  67. stopy = starty;
  68. starty = temp;
  69. temp = stopx;
  70. stopx = startx;
  71. startx = temp;
  72. }
  73. if (stopy == starty )
  74. {
  75. // Draw a edge going horizontally across screen
  76. x1 = startx>>16;
  77. x2 = stopx>>16;
  78. if (x2 > x1 )
  79. //gr_uscanline( x1, x2-1, stopy );
  80. gr_uscanline( x1, x2, stopy );
  81. else
  82. //gr_uscanline( x2, x1-1, stopy );
  83. gr_uscanline( x2, x1, stopy );
  84. } else {
  85. dx_dy = (stopx - startx) / (stopy - starty);
  86. for (; starty < stopy; starty++ )
  87. {
  88. if (y_edge_list[starty]==-1)
  89. y_edge_list[starty] = startx;
  90. else {
  91. x1 = y_edge_list[starty]>>16;
  92. x2 = startx>>16;
  93. if (x2 > x1 )
  94. //gr_uscanline( x1, x2-1, starty );
  95. gr_uscanline( x1, x2, starty );
  96. else
  97. //gr_uscanline( x2, x1-1, starty );
  98. gr_uscanline( x2, x1, starty );
  99. }
  100. startx += dx_dy;
  101. }
  102. }
  103. } while (nverts > 0);
  104. }
  105. void gr_poly(int nverts, int *vert )
  106. {
  107. int temp;
  108. int startx, stopx; // X coordinates of both ends of current edge.
  109. int firstx, firsty; // Saved copy of the first vertex to connect later.
  110. int dx_dy; // Slope of current edge.
  111. int miny, maxy;
  112. int starty, stopy; // Y coordinates of both ends of current edge.
  113. int x1, x2, i, j;
  114. // Find the min and max rows to clear out the minimun y_edge_list.
  115. // (Is it worth it?)
  116. maxy = vert[1];
  117. miny = vert[1];
  118. j = 0;
  119. for (i=3; i<(nverts*2); i+=2 )
  120. {
  121. if (vert[i]>maxy) {
  122. if ((maxy=vert[i]) > MAXY) j++;
  123. //if (j>1) break;
  124. }
  125. if (vert[i]<miny) {
  126. if ((miny=vert[i]) < MINY) j++;
  127. //if (j>1) break;
  128. }
  129. }
  130. miny >>= 16;
  131. miny--; // -1 to be safe
  132. maxy >>= 16;
  133. maxy++; // +1 to be safe
  134. if (miny < MINY) miny = MINY;
  135. if (maxy > MAXY) maxy = MAXY+1;
  136. // Clear only the part of the y_edge_list that w will be using
  137. for (i=miny; i<maxy; i++ )
  138. y_edge_list[i] = -1;
  139. // Save the first vertex so that we can connect to it at the end.
  140. firstx = vert[0];
  141. firsty = vert[1] >> 16;
  142. do
  143. {
  144. nverts--;
  145. // Get the beginning coordinates of the current edge.
  146. startx = vert[0];
  147. starty = vert[1] >> 16;
  148. // Get the ending coordinates of the current edge.
  149. if (nverts > 0 ) {
  150. stopx = vert[2];
  151. stopy = vert[3] >> 16;
  152. vert += 2;
  153. } else {
  154. stopx = firstx; // Last edge, uses first vertex as endpoint
  155. stopy = firsty;
  156. }
  157. if (stopy < starty ) {
  158. temp = stopy;
  159. stopy = starty;
  160. starty = temp;
  161. temp = stopx;
  162. stopx = startx;
  163. startx = temp;
  164. }
  165. if (stopy == starty )
  166. {
  167. // Draw a edge going horizontally across screen
  168. if ((stopy >= MINY) && (stopy <=MAXY )) {
  169. x1 = startx>>16;
  170. x2 = stopx>>16;
  171. if (x1 > x2 ) {
  172. temp = x2;
  173. x2 = x1;
  174. x1 = temp;
  175. }
  176. if ((x1 <= MAXX ) && (x2 >= MINX))
  177. {
  178. if (x1 < MINX ) x1 = MINX;
  179. if (x2 > MAXX ) x2 = MAXX+1;
  180. //gr_uscanline( x1, x2-1, stopy );
  181. gr_scanline( x1, x2, stopy );
  182. }
  183. }
  184. } else {
  185. dx_dy = (stopx - startx) / (stopy - starty);
  186. if (starty < MINY ) {
  187. startx = dx_dy*(MINY-starty)+startx;
  188. starty = MINY;
  189. }
  190. if (stopy > MAXY ) {
  191. stopx = dx_dy*(MAXY-starty)+startx;
  192. stopy = MAXY+1;
  193. }
  194. for (; starty < stopy; starty++ )
  195. { if (y_edge_list[starty]==-1)
  196. y_edge_list[starty] = startx;
  197. else {
  198. x1 = y_edge_list[starty]>>16;
  199. x2 = startx>>16;
  200. if (x1 > x2 ) {
  201. temp = x2;
  202. x2 = x1;
  203. x1 = temp;
  204. }
  205. if ((x1 <= MAXX ) && (x2 >= MINX))
  206. {
  207. if (x1 < MINX ) x1 = MINX;
  208. if (x2 > MAXX ) x2 = MAXX+1;
  209. //gr_uscanline( x1, x2-1, starty );
  210. gr_scanline( x1, x2, starty );
  211. }
  212. }
  213. startx += dx_dy;
  214. }
  215. }
  216. } while (nverts > 0);
  217. }
  218. #endif