1
0

drawpoly.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include <precomp.h>
  2. #include "drawpoly.h"
  3. #include <bfc/parse/pathparse.h>
  4. #define MAXPOINTS 32
  5. static ARGB32 *bits, color;
  6. static int w, h, across;
  7. struct Point2d {
  8. int X, Y;
  9. };
  10. typedef struct Point2d Point2d; // bleh
  11. static Point2d points[MAXPOINTS];
  12. static int npoints;
  13. void Draw::beginPolygon(ARGB32 *bits, int w, int h, ARGB32 color) {
  14. ::bits = bits;
  15. ::w = w;
  16. ::h = h;
  17. ::color = color;
  18. ::across = w;
  19. npoints = 0;
  20. }
  21. void Draw::addPoint(int x, int y) {
  22. if (npoints >= MAXPOINTS) return;
  23. points[npoints].X = x;
  24. points[npoints].Y = y;
  25. npoints++;
  26. }
  27. static void premultiply(ARGB32 *m_pBits, int nwords) {
  28. for (; nwords > 0; nwords--, m_pBits++) {
  29. unsigned __int8 *pixel = (unsigned __int8 *)m_pBits;
  30. unsigned int alpha = pixel[3];
  31. if (alpha == 255) continue;
  32. pixel[0] = (pixel[0] * alpha) >> 8; // blue
  33. pixel[1] = (pixel[1] * alpha) >> 8; // green
  34. pixel[2] = (pixel[2] * alpha) >> 8; // red
  35. }
  36. }
  37. void Draw::drawPointList(ARGB32 *bits, int w, int h, const wchar_t *pointlist) {
  38. if (pointlist == NULL || *pointlist == '\0') return;
  39. PathParserW outer(pointlist, L"|");
  40. const wchar_t *pl;
  41. for (int i = 0; (pl = outer.enumString(i)) != NULL; i++)
  42. {
  43. PathParserW inner(pl, L"=");
  44. ARGB32 color = WASABI_API_SKIN->parse(inner.enumStringSafe(1, L"255,255,255,255"), L"coloralpha");
  45. int a = color & 0xff000000;
  46. color = _byteswap_ulong(color<<8) | a;
  47. premultiply(&color, 1);
  48. beginPolygon(bits, w, h, color);
  49. PathParserW eener(inner.enumStringSafe(0, L"0,0"), L";");
  50. const wchar_t *cc;
  51. for (int j = 0; (cc = eener.enumString(j)) != NULL; j++) {
  52. PathParserW com(cc, L",");
  53. const wchar_t *xs = com.enumStringSafe(0, L"0");
  54. int x = wcschr(xs, '.') ? (int)floor(WTOF(xs) * w + .5f) : WTOI(xs);
  55. const wchar_t *ys = com.enumStringSafe(1, L"0");
  56. int y = wcschr(ys, '.') ? (int)floor(WTOF(ys) * h + .5f) : WTOI(ys);
  57. addPoint(x, y);
  58. }
  59. endPolygon();
  60. }
  61. }
  62. #define PIXEL ARGB32
  63. // this originally came from Michael Abrash's Zen of Graphics Programming
  64. // been modified a bit
  65. /* DRAWPOLY.H: Header file for polygon-filling code */
  66. /* Describes a single point (used for a single vertex) */
  67. //struct Point2d {
  68. // int X; /* X coordinate */
  69. // int Y; /* Y coordinate */
  70. //};
  71. //typedef struct Point2d Point2d;
  72. typedef struct {
  73. int X, Y;
  74. } Point2dC;
  75. /* Describes a series of points (used to store a list of vertices that
  76. describe a polygon; each vertex is assumed to connect to the two
  77. adjacent vertices, and the last vertex is assumed to connect to the
  78. first) */
  79. struct Point2dListHeader {
  80. int Length; /* # of points */
  81. struct Point2d *Point2dPtr; /* pointer to list of points */
  82. };
  83. typedef struct Point2dListHeader Point2dListHeader;
  84. /* Describes the beginning and ending X coordinates of a single
  85. horizontal line */
  86. struct HLine {
  87. int XStart; /* X coordinate of leftmost pixel in line */
  88. int XEnd; /* X coordinate of rightmost pixel in line */
  89. };
  90. typedef struct {
  91. int XStart, XEnd;
  92. } HLineColor;
  93. /* Describes a Length-long series of horizontal lines, all assumed to
  94. be on contiguous scan lines starting at YStart and proceeding
  95. downward (used to describe a scan-converted polygon to the
  96. low-level hardware-dependent drawing code) */
  97. struct HLineList {
  98. int Length; /* # of horizontal lines */
  99. int YStart; /* Y coordinate of topmost line */
  100. struct HLine * HLinePtr; /* pointer to list of horz lines */
  101. };
  102. static void DrawHorizontalLineList(struct HLineList * HLineListPtr, PIXEL *dest,
  103. PIXEL Color) {
  104. struct HLine *HLinePtr, *ptr;
  105. int Length, Width, c;
  106. PIXEL *ScreenPtr;
  107. /* Point to the start of the first scan line on which to draw */
  108. ScreenPtr = dest + HLineListPtr->YStart * across;
  109. Length = HLineListPtr->Length;
  110. /* Point to the XStart/XEnd descriptor for the first (top)
  111. horizontal line */
  112. HLinePtr = HLineListPtr->HLinePtr;
  113. /* clip left/right */
  114. for (ptr = HLinePtr, c = Length; c; c--) {
  115. if (ptr->XStart < 0) ptr->XStart = 0;
  116. if (ptr->XEnd >= w) ptr->XEnd = w - 1;
  117. ptr++;
  118. }
  119. /* clip top */
  120. if (HLineListPtr->YStart < 0) {
  121. int skip = -HLineListPtr->YStart;
  122. HLineListPtr->YStart = 0;
  123. ScreenPtr += across * skip;
  124. Length -= skip;
  125. HLinePtr += skip;
  126. }
  127. /* clip bottom */
  128. if (HLineListPtr->YStart + Length > h) {
  129. Length -= (HLineListPtr->YStart + Length) - h;
  130. }
  131. /* Draw each horizontal line in turn, starting with the top one and
  132. advancing one line each time */
  133. while (Length-- > 0) {
  134. /* Draw the whole horizontal line if it has a positive width */
  135. if ((Width = HLinePtr->XEnd - HLinePtr->XStart + 1) > 0)
  136. // bmemsetw(ScreenPtr+HLinePtr->XStart, Color, Width);
  137. MEMFILL<PIXEL>(ScreenPtr+HLinePtr->XStart, Color, Width);
  138. HLinePtr++; /* point to next scan line X info */
  139. ScreenPtr += across; /* point to next scan line start */
  140. }
  141. }
  142. /* Scan converts an edge from (X1,Y1) to (X2,Y2), not including the
  143. point at (X2,Y2). If SkipFirst == 1, the point at (X1,Y1) isn't
  144. drawn; if SkipFirst == 0, it is. For each scan line, the pixel
  145. closest to the scanned edge without being to the left of the
  146. scanned edge is chosen. Uses an all-integer approach for speed and
  147. precision
  148. Link with L21-1.C, L21-3.C, and L22-1.C in Compact model.
  149. Tested with Borland C++ 4.02 by Jim Mischel 12/16/94.
  150. */
  151. static void ScanEdge(int X1, int Y1, int X2, int Y2, int SetXStart,
  152. int SkipFirst, struct HLine **EdgePoint2dPtr) {
  153. int DeltaX, Height, Width, AdvanceAmt, ErrorTerm, i;
  154. int ErrorTermAdvance, XMajorAdvanceAmt;
  155. struct HLine *WorkingEdgePoint2dPtr;
  156. WorkingEdgePoint2dPtr = *EdgePoint2dPtr; /* avoid double dereference */
  157. AdvanceAmt = ((DeltaX = X2 - X1) > 0) ? 1 : -1;
  158. /* direction in which X moves (Y2 is
  159. always > Y1, so Y always counts up) */
  160. if ((Height = Y2 - Y1) <= 0) /* Y length of the edge */
  161. return; /* guard against 0-length and horizontal edges */
  162. /* Figure out whether the edge is vertical, diagonal, X-major
  163. (mostly horizontal), or Y-major (mostly vertical) and handle
  164. appropriately */
  165. if ((Width = abs(DeltaX)) == 0) {
  166. /* The edge is vertical; special-case by just storing the same
  167. X coordinate for every scan line */
  168. /* Scan the edge for each scan line in turn */
  169. for (i = Height - SkipFirst; i-- > 0; WorkingEdgePoint2dPtr++) {
  170. /* Store the X coordinate in the appropriate edge list */
  171. if (SetXStart == 1)
  172. WorkingEdgePoint2dPtr->XStart = X1;
  173. else
  174. WorkingEdgePoint2dPtr->XEnd = X1;
  175. }
  176. } else if (Width == Height) {
  177. /* The edge is diagonal; special-case by advancing the X
  178. coordinate 1 pixel for each scan line */
  179. if (SkipFirst) /* skip the first point if so indicated */
  180. X1 += AdvanceAmt; /* move 1 pixel to the left or right */
  181. /* Scan the edge for each scan line in turn */
  182. for (i = Height - SkipFirst; i-- > 0; WorkingEdgePoint2dPtr++) {
  183. /* Store the X coordinate in the appropriate edge list */
  184. if (SetXStart == 1)
  185. WorkingEdgePoint2dPtr->XStart = X1;
  186. else
  187. WorkingEdgePoint2dPtr->XEnd = X1;
  188. X1 += AdvanceAmt; /* move 1 pixel to the left or right */
  189. }
  190. } else if (Height > Width) {
  191. /* Edge is closer to vertical than horizontal (Y-major) */
  192. if (DeltaX >= 0)
  193. ErrorTerm = 0; /* initial error term going left->right */
  194. else
  195. ErrorTerm = -Height + 1; /* going right->left */
  196. if (SkipFirst) { /* skip the first point if so indicated */
  197. /* Determine whether it's time for the X coord to advance */
  198. if ((ErrorTerm += Width) > 0) {
  199. X1 += AdvanceAmt; /* move 1 pixel to the left or right */
  200. ErrorTerm -= Height; /* advance ErrorTerm to next point */
  201. }
  202. }
  203. /* Scan the edge for each scan line in turn */
  204. for (i = Height - SkipFirst; i-- > 0; WorkingEdgePoint2dPtr++) {
  205. /* Store the X coordinate in the appropriate edge list */
  206. if (SetXStart == 1)
  207. WorkingEdgePoint2dPtr->XStart = X1;
  208. else
  209. WorkingEdgePoint2dPtr->XEnd = X1;
  210. /* Determine whether it's time for the X coord to advance */
  211. if ((ErrorTerm += Width) > 0) {
  212. X1 += AdvanceAmt; /* move 1 pixel to the left or right */
  213. ErrorTerm -= Height; /* advance ErrorTerm to correspond */
  214. }
  215. }
  216. } else {
  217. /* Edge is closer to horizontal than vertical (X-major) */
  218. /* Minimum distance to advance X each time */
  219. XMajorAdvanceAmt = (Width / Height) * AdvanceAmt;
  220. /* Error term advance for deciding when to advance X 1 extra */
  221. ErrorTermAdvance = Width % Height;
  222. if (DeltaX >= 0)
  223. ErrorTerm = 0; /* initial error term going left->right */
  224. else
  225. ErrorTerm = -Height + 1; /* going right->left */
  226. if (SkipFirst) { /* skip the first point if so indicated */
  227. X1 += XMajorAdvanceAmt; /* move X minimum distance */
  228. /* Determine whether it's time for X to advance one extra */
  229. if ((ErrorTerm += ErrorTermAdvance) > 0) {
  230. X1 += AdvanceAmt; /* move X one more */
  231. ErrorTerm -= Height; /* advance ErrorTerm to correspond */
  232. }
  233. }
  234. /* Scan the edge for each scan line in turn */
  235. for (i = Height - SkipFirst; i-- > 0; WorkingEdgePoint2dPtr++) {
  236. /* Store the X coordinate in the appropriate edge list */
  237. if (SetXStart == 1)
  238. WorkingEdgePoint2dPtr->XStart = X1;
  239. else
  240. WorkingEdgePoint2dPtr->XEnd = X1;
  241. X1 += XMajorAdvanceAmt; /* move X minimum distance */
  242. /* Determine whether it's time for X to advance one extra */
  243. if ((ErrorTerm += ErrorTermAdvance) > 0) {
  244. X1 += AdvanceAmt; /* move X one more */
  245. ErrorTerm -= Height; /* advance ErrorTerm to correspond */
  246. }
  247. }
  248. }
  249. *EdgePoint2dPtr = WorkingEdgePoint2dPtr; /* advance caller's ptr */
  250. }
  251. /* Color-fills a convex polygon. All vertices are offset by (XOffset,
  252. YOffset). "Convex" means that every horizontal line drawn through
  253. the polygon at any point would cross exactly two active edges
  254. (neither horizontal lines nor zero-length edges count as active
  255. edges; both are acceptable anywhere in the polygon), and that the
  256. right & left edges never cross. (It's OK for them to touch, though,
  257. so long as the right edge never crosses over to the left of the
  258. left edge.) Nonconvex polygons won't be drawn properly. Returns 1
  259. for success, 0 if memory allocation failed.
  260. Compiled with Borland C++ 4.02. Link with L21-3.C.
  261. Checked by Jim Mischel 11/30/94.
  262. */
  263. /* Advances the index by one vertex forward through the vertex list,
  264. wrapping at the end of the list */
  265. #define INDEX_FORWARD(Index) \
  266. Index = (Index + 1) % VertexList->Length;
  267. /* Advances the index by one vertex backward through the vertex list,
  268. wrapping at the start of the list */
  269. #define INDEX_BACKWARD(Index) \
  270. Index = (Index - 1 + VertexList->Length) % VertexList->Length;
  271. /* Advances the index by one vertex either forward or backward through
  272. the vertex list, wrapping at either end of the list */
  273. #define INDEX_MOVE(Index,Direction) \
  274. if (Direction > 0) \
  275. Index = (Index + 1) % VertexList->Length; \
  276. else \
  277. Index = (Index - 1 + VertexList->Length) % VertexList->Length;
  278. int FillConvexPolygon(struct Point2dListHeader *VertexList, PIXEL *dest,
  279. PIXEL Color) {
  280. int i, MinIndexL, MaxIndex, MinIndexR, SkipFirst, Temp;
  281. int MinPoint2d_Y, MaxPoint2d_Y, TopIsFlat, LeftEdgeDir;
  282. int NextIndex, CurrentIndex, PreviousIndex;
  283. int DeltaXN, DeltaYN, DeltaXP, DeltaYP;
  284. struct HLineList WorkingHLineList;
  285. struct HLine *EdgePoint2dPtr;
  286. struct Point2d *VertexPtr;
  287. /* Point to the vertex list */
  288. VertexPtr = VertexList->Point2dPtr;
  289. /* Scan the list to find the top and bottom of the polygon */
  290. if (VertexList->Length == 0)
  291. return(1); /* reject null polygons */
  292. MaxPoint2d_Y = MinPoint2d_Y = VertexPtr[MinIndexL = MaxIndex = 0].Y;
  293. for (i = 1; i < VertexList->Length; i++) {
  294. if (VertexPtr[i].Y < MinPoint2d_Y)
  295. MinPoint2d_Y = VertexPtr[MinIndexL = i].Y; /* new top */
  296. else if (VertexPtr[i].Y > MaxPoint2d_Y)
  297. MaxPoint2d_Y = VertexPtr[MaxIndex = i].Y; /* new bottom */
  298. }
  299. if (MinPoint2d_Y == MaxPoint2d_Y)
  300. return(1); /* polygon is 0-height; avoid infinite loop below */
  301. /* Scan in ascending order to find the last top-edge point */
  302. MinIndexR = MinIndexL;
  303. while (VertexPtr[MinIndexR].Y == MinPoint2d_Y)
  304. INDEX_FORWARD(MinIndexR);
  305. INDEX_BACKWARD(MinIndexR); /* back up to last top-edge point */
  306. /* Now scan in descending order to find the first top-edge point */
  307. while (VertexPtr[MinIndexL].Y == MinPoint2d_Y)
  308. INDEX_BACKWARD(MinIndexL);
  309. INDEX_FORWARD(MinIndexL); /* back up to first top-edge point */
  310. /* Figure out which direction through the vertex list from the top
  311. vertex is the left edge and which is the right */
  312. LeftEdgeDir = -1; /* assume left edge runs down thru vertex list */
  313. if ((TopIsFlat = (VertexPtr[MinIndexL].X !=
  314. VertexPtr[MinIndexR].X) ? 1 : 0) == 1) {
  315. /* If the top is flat, just see which of the ends is leftmost */
  316. if (VertexPtr[MinIndexL].X > VertexPtr[MinIndexR].X) {
  317. LeftEdgeDir = 1; /* left edge runs up through vertex list */
  318. Temp = MinIndexL; /* swap the indices so MinIndexL */
  319. MinIndexL = MinIndexR; /* points to the start of the left */
  320. MinIndexR = Temp; /* edge, similarly for MinIndexR */
  321. }
  322. } else {
  323. /* Point to the downward end of the first line of each of the
  324. two edges down from the top */
  325. NextIndex = MinIndexR;
  326. INDEX_FORWARD(NextIndex);
  327. PreviousIndex = MinIndexL;
  328. INDEX_BACKWARD(PreviousIndex);
  329. /* Calculate X and Y lengths from the top vertex to the end of
  330. the first line down each edge; use those to compare slopes
  331. and see which line is leftmost */
  332. DeltaXN = VertexPtr[NextIndex].X - VertexPtr[MinIndexL].X;
  333. DeltaYN = VertexPtr[NextIndex].Y - VertexPtr[MinIndexL].Y;
  334. DeltaXP = VertexPtr[PreviousIndex].X - VertexPtr[MinIndexL].X;
  335. DeltaYP = VertexPtr[PreviousIndex].Y - VertexPtr[MinIndexL].Y;
  336. if (((long)DeltaXN * DeltaYP - (long)DeltaYN * DeltaXP) < 0L) {
  337. LeftEdgeDir = 1; /* left edge runs up through vertex list */
  338. Temp = MinIndexL; /* swap the indices so MinIndexL */
  339. MinIndexL = MinIndexR; /* points to the start of the left */
  340. MinIndexR = Temp; /* edge, similarly for MinIndexR */
  341. }
  342. }
  343. /* Set the # of scan lines in the polygon, skipping the bottom edge
  344. and also skipping the top vertex if the top isn't flat because
  345. in that case the top vertex has a right edge component, and set
  346. the top scan line to draw, which is likewise the second line of
  347. the polygon unless the top is flat */
  348. if ((WorkingHLineList.Length =
  349. MaxPoint2d_Y - MinPoint2d_Y - 1 + TopIsFlat) <= 0)
  350. return(1); /* there's nothing to draw, so we're done */
  351. //WorkingHLineList.YStart = YOffset + MinPoint2d_Y + 1 - TopIsFlat;
  352. WorkingHLineList.YStart = MinPoint2d_Y + 1 - TopIsFlat;
  353. /* Get memory in which to store the line list we generate */
  354. if ((WorkingHLineList.HLinePtr =
  355. (struct HLine *) (malloc(sizeof(struct HLine) *
  356. WorkingHLineList.Length))) == NULL)
  357. return(0); /* couldn't get memory for the line list */
  358. /* Scan the left edge and store the boundary points in the list */
  359. /* Initial pointer for storing scan converted left-edge coords */
  360. EdgePoint2dPtr = WorkingHLineList.HLinePtr;
  361. /* Start from the top of the left edge */
  362. PreviousIndex = CurrentIndex = MinIndexL;
  363. /* Skip the first point of the first line unless the top is flat;
  364. if the top isn't flat, the top vertex is exactly on a right
  365. edge and isn't drawn */
  366. SkipFirst = TopIsFlat ? 0 : 1;
  367. /* Scan convert each line in the left edge from top to bottom */
  368. do {
  369. INDEX_MOVE(CurrentIndex,LeftEdgeDir);
  370. ScanEdge(VertexPtr[PreviousIndex].X,
  371. VertexPtr[PreviousIndex].Y,
  372. VertexPtr[CurrentIndex].X,
  373. VertexPtr[CurrentIndex].Y, 1, SkipFirst, &EdgePoint2dPtr);
  374. PreviousIndex = CurrentIndex;
  375. SkipFirst = 0; /* scan convert the first point from now on */
  376. } while (CurrentIndex != MaxIndex);
  377. /* Scan the right edge and store the boundary points in the list */
  378. EdgePoint2dPtr = WorkingHLineList.HLinePtr;
  379. PreviousIndex = CurrentIndex = MinIndexR;
  380. SkipFirst = TopIsFlat ? 0 : 1;
  381. /* Scan convert the right edge, top to bottom. X coordinates are
  382. adjusted 1 to the left, effectively causing scan conversion of
  383. the nearest points to the left of but not exactly on the edge */
  384. do {
  385. INDEX_MOVE(CurrentIndex,-LeftEdgeDir);
  386. //ScanEdge(VertexPtr[PreviousIndex].X + XOffset - 1,
  387. ScanEdge(VertexPtr[PreviousIndex].X - 1,
  388. VertexPtr[PreviousIndex].Y,
  389. //VertexPtr[CurrentIndex].X + XOffset - 1,
  390. VertexPtr[CurrentIndex].X - 1,
  391. VertexPtr[CurrentIndex].Y, 0, SkipFirst, &EdgePoint2dPtr);
  392. PreviousIndex = CurrentIndex;
  393. SkipFirst = 0; /* scan convert the first point from now on */
  394. } while (CurrentIndex != MaxIndex);
  395. /* Draw the line list representing the scan converted polygon */
  396. //CUT (*drawfn)(&WorkingHLineList, dest, Color, vc);
  397. DrawHorizontalLineList(&WorkingHLineList, dest, Color);
  398. /* Release the line list's memory and we're successfully done */
  399. free(WorkingHLineList.HLinePtr);
  400. return(1);
  401. }
  402. // done with abrashitude
  403. void Draw::endPolygon() {
  404. if (npoints == 0) return;
  405. struct Point2dListHeader head;
  406. head.Length = npoints;
  407. head.Point2dPtr = &points[0];
  408. FillConvexPolygon(&head, bits, color);
  409. }