agg_renderer_primitives.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // Permission to copy, use, modify, sell and distribute this software
  6. // is granted provided this copyright notice appears in all copies.
  7. // This software is provided "as is" without express or implied
  8. // warranty, and with no claim as to its suitability for any purpose.
  9. //
  10. //----------------------------------------------------------------------------
  11. // Contact: mcseem@antigrain.com
  12. // mcseemagg@yahoo.com
  13. // http://www.antigrain.com
  14. //----------------------------------------------------------------------------
  15. //
  16. // class renderer_primitives
  17. //
  18. //----------------------------------------------------------------------------
  19. #ifndef AGG_RENDERER_PRIMITIVES_INCLUDED
  20. #define AGG_RENDERER_PRIMITIVES_INCLUDED
  21. #include "agg_basics.h"
  22. #include "agg_renderer_base.h"
  23. #include "agg_dda_line.h"
  24. #include "agg_ellipse_bresenham.h"
  25. namespace agg
  26. {
  27. //-----------------------------------------------------renderer_primitives
  28. template<class BaseRenderer> class renderer_primitives
  29. {
  30. public:
  31. typedef BaseRenderer base_ren_type;
  32. typedef typename base_ren_type::color_type color_type;
  33. //--------------------------------------------------------------------
  34. explicit renderer_primitives(base_ren_type& ren) :
  35. m_ren(&ren),
  36. m_fill_color(),
  37. m_line_color(),
  38. m_curr_x(0),
  39. m_curr_y(0)
  40. {}
  41. void attach(base_ren_type& ren) { m_ren = &ren; }
  42. //--------------------------------------------------------------------
  43. static int coord(double c)
  44. {
  45. return iround(c * line_bresenham_interpolator::subpixel_scale);
  46. }
  47. //--------------------------------------------------------------------
  48. void fill_color(const color_type& c) { m_fill_color = c; }
  49. void line_color(const color_type& c) { m_line_color = c; }
  50. const color_type& fill_color() const { return m_fill_color; }
  51. const color_type& line_color() const { return m_line_color; }
  52. //--------------------------------------------------------------------
  53. void rectangle(int x1, int y1, int x2, int y2)
  54. {
  55. m_ren->blend_hline(x1, y1, x2-1, m_line_color, cover_full);
  56. m_ren->blend_vline(x2, y1, y2-1, m_line_color, cover_full);
  57. m_ren->blend_hline(x1+1, y2, x2, m_line_color, cover_full);
  58. m_ren->blend_vline(x1, y1+1, y2, m_line_color, cover_full);
  59. }
  60. //--------------------------------------------------------------------
  61. void solid_rectangle(int x1, int y1, int x2, int y2)
  62. {
  63. m_ren->blend_bar(x1, y1, x2, y2, m_fill_color, cover_full);
  64. }
  65. //--------------------------------------------------------------------
  66. void outlined_rectangle(int x1, int y1, int x2, int y2)
  67. {
  68. rectangle(x1, y1, x2, y2);
  69. m_ren->blend_bar(x1+1, y1+1, x2-1, y2-1, m_fill_color, cover_full);
  70. }
  71. //--------------------------------------------------------------------
  72. void ellipse(int x, int y, int rx, int ry)
  73. {
  74. ellipse_bresenham_interpolator ei(rx, ry);
  75. int dx = 0;
  76. int dy = -ry;
  77. do
  78. {
  79. dx += ei.dx();
  80. dy += ei.dy();
  81. m_ren->blend_pixel(x + dx, y + dy, m_line_color, cover_full);
  82. m_ren->blend_pixel(x + dx, y - dy, m_line_color, cover_full);
  83. m_ren->blend_pixel(x - dx, y - dy, m_line_color, cover_full);
  84. m_ren->blend_pixel(x - dx, y + dy, m_line_color, cover_full);
  85. ++ei;
  86. }
  87. while(dy < 0);
  88. }
  89. //--------------------------------------------------------------------
  90. void solid_ellipse(int x, int y, int rx, int ry)
  91. {
  92. ellipse_bresenham_interpolator ei(rx, ry);
  93. int dx = 0;
  94. int dy = -ry;
  95. int dy0 = dy;
  96. int dx0 = dx;
  97. do
  98. {
  99. dx += ei.dx();
  100. dy += ei.dy();
  101. if(dy != dy0)
  102. {
  103. m_ren->blend_hline(x-dx0, y+dy0, x+dx0, m_fill_color, cover_full);
  104. m_ren->blend_hline(x-dx0, y-dy0, x+dx0, m_fill_color, cover_full);
  105. }
  106. dx0 = dx;
  107. dy0 = dy;
  108. ++ei;
  109. }
  110. while(dy < 0);
  111. m_ren->blend_hline(x-dx0, y+dy0, x+dx0, m_fill_color, cover_full);
  112. }
  113. //--------------------------------------------------------------------
  114. void outlined_ellipse(int x, int y, int rx, int ry)
  115. {
  116. ellipse_bresenham_interpolator ei(rx, ry);
  117. int dx = 0;
  118. int dy = -ry;
  119. do
  120. {
  121. dx += ei.dx();
  122. dy += ei.dy();
  123. m_ren->blend_pixel(x + dx, y + dy, m_line_color, cover_full);
  124. m_ren->blend_pixel(x + dx, y - dy, m_line_color, cover_full);
  125. m_ren->blend_pixel(x - dx, y - dy, m_line_color, cover_full);
  126. m_ren->blend_pixel(x - dx, y + dy, m_line_color, cover_full);
  127. if(ei.dy() && dx)
  128. {
  129. m_ren->blend_hline(x-dx+1, y+dy, x+dx-1, m_fill_color, cover_full);
  130. m_ren->blend_hline(x-dx+1, y-dy, x+dx-1, m_fill_color, cover_full);
  131. }
  132. ++ei;
  133. }
  134. while(dy < 0);
  135. }
  136. //--------------------------------------------------------------------
  137. void line(int x1, int y1, int x2, int y2, bool last=false)
  138. {
  139. line_bresenham_interpolator li(x1, y1, x2, y2);
  140. unsigned len = li.len();
  141. if(len == 0)
  142. {
  143. if(last)
  144. {
  145. m_ren->blend_pixel(li.line_lr(x1), li.line_lr(y1), m_line_color, cover_full);
  146. }
  147. return;
  148. }
  149. if(last) ++len;
  150. if(li.is_ver())
  151. {
  152. do
  153. {
  154. m_ren->blend_pixel(li.x2(), li.y1(), m_line_color, cover_full);
  155. li.vstep();
  156. }
  157. while(--len);
  158. }
  159. else
  160. {
  161. do
  162. {
  163. m_ren->blend_pixel(li.x1(), li.y2(), m_line_color, cover_full);
  164. li.hstep();
  165. }
  166. while(--len);
  167. }
  168. }
  169. //--------------------------------------------------------------------
  170. void move_to(int x, int y)
  171. {
  172. m_curr_x = x;
  173. m_curr_y = y;
  174. }
  175. //--------------------------------------------------------------------
  176. void line_to(int x, int y, bool last=false)
  177. {
  178. line(m_curr_x, m_curr_y, x, y, last);
  179. m_curr_x = x;
  180. m_curr_y = y;
  181. }
  182. //--------------------------------------------------------------------
  183. const base_ren_type& ren() const { return *m_ren; }
  184. base_ren_type& ren() { return *m_ren; }
  185. //--------------------------------------------------------------------
  186. const rendering_buffer& rbuf() const { return m_ren->rbuf(); }
  187. rendering_buffer& rbuf() { return m_ren->rbuf(); }
  188. private:
  189. base_ren_type* m_ren;
  190. color_type m_fill_color;
  191. color_type m_line_color;
  192. int m_curr_x;
  193. int m_curr_y;
  194. };
  195. }
  196. #endif