layerop.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "lib9.h"
  2. #include "draw.h"
  3. #include "memdraw.h"
  4. #include "memlayer.h"
  5. #define RECUR(a,b,c,d) _layerop(fn, i, Rect(a.x, b.y, c.x, d.y), clipr, etc, front->layer->rear);
  6. static void
  7. _layerop(
  8. void (*fn)(Memimage*, Rectangle, Rectangle, void*, int),
  9. Memimage *i,
  10. Rectangle r,
  11. Rectangle clipr,
  12. void *etc,
  13. Memimage *front)
  14. {
  15. Rectangle fr;
  16. Top:
  17. if(front == i){
  18. /* no one is in front of this part of window; use the screen */
  19. fn(i->layer->screen->image, r, clipr, etc, 0);
  20. return;
  21. }
  22. fr = front->layer->screenr;
  23. if(rectXrect(r, fr) == 0){
  24. /* r doesn't touch this window; continue on next rearmost */
  25. /* assert(front && front->layer && front->layer->screen && front->layer->rear); */
  26. front = front->layer->rear;
  27. goto Top;
  28. }
  29. if(fr.max.y < r.max.y){
  30. RECUR(r.min, fr.max, r.max, r.max);
  31. r.max.y = fr.max.y;
  32. }
  33. if(r.min.y < fr.min.y){
  34. RECUR(r.min, r.min, r.max, fr.min);
  35. r.min.y = fr.min.y;
  36. }
  37. if(fr.max.x < r.max.x){
  38. RECUR(fr.max, r.min, r.max, r.max);
  39. r.max.x = fr.max.x;
  40. }
  41. if(r.min.x < fr.min.x){
  42. RECUR(r.min, r.min, fr.min, r.max);
  43. r.min.x = fr.min.x;
  44. }
  45. /* r is covered by front, so put in save area */
  46. (*fn)(i->layer->save, r, clipr, etc, 1);
  47. }
  48. /*
  49. * Assumes incoming rectangle has already been clipped to i's logical r and clipr
  50. */
  51. void
  52. _memlayerop(
  53. void (*fn)(Memimage*, Rectangle, Rectangle, void*, int),
  54. Memimage *i,
  55. Rectangle screenr, /* clipped to window boundaries */
  56. Rectangle clipr, /* clipped also to clipping rectangles of hierarchy */
  57. void *etc)
  58. {
  59. Memlayer *l;
  60. Rectangle r, scr;
  61. l = i->layer;
  62. if(!rectclip(&screenr, l->screenr))
  63. return;
  64. if(l->clear){
  65. fn(l->screen->image, screenr, clipr, etc, 0);
  66. return;
  67. }
  68. r = screenr;
  69. scr = l->screen->image->clipr;
  70. /*
  71. * Do the piece on the screen
  72. */
  73. if(rectclip(&screenr, scr))
  74. _layerop(fn, i, screenr, clipr, etc, l->screen->frontmost);
  75. if(rectinrect(r, scr))
  76. return;
  77. /*
  78. * Do the piece off the screen
  79. */
  80. if(!rectXrect(r, scr)){
  81. /* completely offscreen; easy */
  82. fn(l->save, r, clipr, etc, 1);
  83. return;
  84. }
  85. if(r.min.y < scr.min.y){
  86. /* above screen */
  87. fn(l->save, Rect(r.min.x, r.min.y, r.max.x, scr.min.y), clipr, etc, 1);
  88. r.min.y = scr.min.y;
  89. }
  90. if(r.max.y > scr.max.y){
  91. /* below screen */
  92. fn(l->save, Rect(r.min.x, scr.max.y, r.max.x, r.max.y), clipr, etc, 1);
  93. r.max.y = scr.max.y;
  94. }
  95. if(r.min.x < scr.min.x){
  96. /* left of screen */
  97. fn(l->save, Rect(r.min.x, r.min.y, scr.min.x, r.max.y), clipr, etc, 1);
  98. r.min.x = scr.min.x;
  99. }
  100. if(r.max.x > scr.max.x){
  101. /* right of screen */
  102. fn(l->save, Rect(scr.max.x, r.min.y, r.max.x, r.max.y), clipr, etc, 1);
  103. }
  104. }