line.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <memdraw.h>
  5. #include <memlayer.h>
  6. struct Lline
  7. {
  8. Point p0;
  9. Point p1;
  10. Point delta;
  11. int end0;
  12. int end1;
  13. int radius;
  14. Point sp;
  15. Memlayer *dstlayer;
  16. Memimage *src;
  17. int op;
  18. };
  19. static void llineop(Memimage*, Rectangle, Rectangle, void*, int);
  20. static
  21. void
  22. _memline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, Rectangle clipr, int op)
  23. {
  24. Rectangle r;
  25. struct Lline ll;
  26. Point d;
  27. int srcclipped;
  28. Memlayer *dl;
  29. if(radius < 0)
  30. return;
  31. if(src->layer) /* can't draw line with layered source */
  32. return;
  33. srcclipped = 0;
  34. Top:
  35. dl = dst->layer;
  36. if(dl == nil){
  37. _memimageline(dst, p0, p1, end0, end1, radius, src, sp, clipr, op);
  38. return;
  39. }
  40. if(!srcclipped){
  41. d = subpt(sp, p0);
  42. if(rectclip(&clipr, rectsubpt(src->clipr, d)) == 0)
  43. return;
  44. if((src->flags&Frepl)==0 && rectclip(&clipr, rectsubpt(src->r, d))==0)
  45. return;
  46. srcclipped = 1;
  47. }
  48. /* dst is known to be a layer */
  49. p0.x += dl->delta.x;
  50. p0.y += dl->delta.y;
  51. p1.x += dl->delta.x;
  52. p1.y += dl->delta.y;
  53. clipr.min.x += dl->delta.x;
  54. clipr.min.y += dl->delta.y;
  55. clipr.max.x += dl->delta.x;
  56. clipr.max.y += dl->delta.y;
  57. if(dl->clear){
  58. dst = dst->layer->screen->image;
  59. goto Top;
  60. }
  61. /* XXX */
  62. /* this is not the correct set of tests */
  63. // if(log2[dst->depth] != log2[src->depth] || log2[dst->depth]!=3)
  64. // return;
  65. /* can't use sutherland-cohen clipping because lines are wide */
  66. r = memlinebbox(p0, p1, end0, end1, radius);
  67. /*
  68. * r is now a bounding box for the line;
  69. * use it as a clipping rectangle for subdivision
  70. */
  71. if(rectclip(&r, clipr) == 0)
  72. return;
  73. ll.p0 = p0;
  74. ll.p1 = p1;
  75. ll.end0 = end0;
  76. ll.end1 = end1;
  77. ll.sp = sp;
  78. ll.dstlayer = dst->layer;
  79. ll.src = src;
  80. ll.radius = radius;
  81. ll.delta = dl->delta;
  82. ll.op = op;
  83. _memlayerop(llineop, dst, r, r, &ll);
  84. }
  85. static
  86. void
  87. llineop(Memimage *dst, Rectangle screenr, Rectangle clipr, void *etc, int insave)
  88. {
  89. struct Lline *ll;
  90. Point p0, p1;
  91. USED(screenr.min.x);
  92. ll = etc;
  93. if(insave && ll->dstlayer->save==nil)
  94. return;
  95. if(!rectclip(&clipr, screenr))
  96. return;
  97. if(insave){
  98. p0 = subpt(ll->p0, ll->delta);
  99. p1 = subpt(ll->p1, ll->delta);
  100. clipr = rectsubpt(clipr, ll->delta);
  101. }else{
  102. p0 = ll->p0;
  103. p1 = ll->p1;
  104. }
  105. _memline(dst, p0, p1, ll->end0, ll->end1, ll->radius, ll->src, ll->sp, clipr, ll->op);
  106. }
  107. void
  108. memline(Memimage *dst, Point p0, Point p1, int end0, int end1, int radius, Memimage *src, Point sp, int op)
  109. {
  110. _memline(dst, p0, p1, end0, end1, radius, src, sp, dst->clipr, op);
  111. }