AGG学习之五----底层Renderer时间:2021-05-10 05:42:20使用agg::renderer_base、agg::renderer_mclip可以支持设置裁剪区域,对裁剪区域不进行绘制操作。 样例代码: #include "agg_rendering_buffer.h" #include "agg_basics.h" #include "platform/agg_platform_support.h" #include "agg_pixfmt_rgb.h" #include "agg_alpha_mask_u8.h" #include "agg_pixfmt_amask_adaptor.h" enum { flip_y = true}; class the_application : public agg::platform_support { public: the_application(agg::pix_format_e format, bool flip_y): agg::platform_support(format,flip_y) {} virtual ~the_application() { } virtual void on_init(){} virtual void on_draw() { unsigned i; agg::rendering_buffer rbuf = this->rbuf_window(); agg::pixfmt_rgb24 pixf(rbuf);//底层像素渲染对象,对渲染内存进行包装了 int w = pixf.width(); int h = pixf.height(); agg::int8u *amask_buf = new agg::int8u[w*h]; //mask的缓存区申请 agg::rendering_buffer amask_rbuf(amask_buf, w, h, w); //mask的rendering buffer object, 对mask buffer进行封装 for (i=0; i<h; ++i) { unsigned val = 255*i/h; memset(amask_rbuf.row_ptr(i), val, w); } agg::amask_no_clip_gray8 amask(amask_rbuf); //创建无裁剪区的mask object typedef agg::pixfmt_amask_adaptor<agg::pixfmt_rgb24, agg::amask_no_clip_gray8> pixfmt_amask; pixfmt_amask pixf_amask(pixf,amask);//创建像素渲染器与掩码的适配器 agg::renderer_base<pixfmt_amask> rclip(pixf_amask);//定义支持裁剪区的renderer rclip.clip_box( 100,100,200,200);//设置裁剪区 //agg::renderer_mclip memset( rbuf.row_ptr(0), 255, w*h*3 ); //设定光谱值 agg::rgba8 *span = new agg::rgba8[w]; agg::int8u *cover = new agg::int8u[w]; for (i=0; i<w; ++i) { agg::rgba c(380.0+400.0*i/w,0.8); span[i] = agg::rgba8(c); } for (i=0; i<h; ++i) { rclip.blend_color_hspan(0,i,w,span,cover);//裁剪区域外,不进行绘制 } delete []span; delete []cover; } }; int agg_main(int argc, char* argv[]) { the_application app(agg::pix_format_bgr24, flip_y); app.caption("My AGG Demo"); if(app.init(320, 200, agg::window_resize )) { app.run(); } return 1; } 效果图: