引言:
在agg/examples目录下,提供的很多例子都携带滚动条的,并且通过滚动条,可以动态看到图像色彩的渐变过程。因此一下agg::slider_ctrl工具类。
图形:
/*
*(x2,y2)
* *
* *
* *
* *
* *
* *
* *
(x1,y1)**************************************(x2,y1)
*/
函数:
构造函数:slider_ctrl(double x1, double y1, doublex2, double y2, bool flip_y=false)
m_slider1.range(1, 100);//设置滚动条的取值范围
m_slider1.num_steps(10);//设置滚动条的步宽,
m_slider1.value(1.0);//设置初始值
m_slider1.label("Pixel size=%1.0f");//显示当前滚动条的值
m_slider1.no_transform();//不允许缩放
int nScaleX =m_slider1.value();//获取当前值
1)添加头文件
#include "agg/include/ctrl/agg_slider_ctrl.h"
2)定义:
agg::slider_ctrl<agg::rgba8> m_slider1;
3)添加到事件响应列表
添加到windows platform例子中:
add_ctrl(m_slider1);
/*
通过上面的函数,将控件元素添加到事件监听列表中,当鼠标或者键盘发生事件的时候
,会传递给该控件,由该控件判断是否是属于自己的事件(是否正在滑动滚动条)。
通过注释该函数,作者可体会到,滑动进度条,没有任何的反映。
*/
4)渲染控件
agg::render_ctrl(ras, sl, renb, m_slider1);
5)由于是把代码放在on_draw,需要强行刷新
force_redraw();
简单的应用例子如下:
#include "agg/include/agg_basics.h"
#include "agg/include/agg_conv_curve.h"
#include "agg/include/agg_trans_perspective.h"
#include "agg/include/agg_renderer_scanline.h"
#include "agg/include/agg_rendering_buffer.h"
#include "agg/include/agg_rasterizer_scanline_aa.h"
#include "agg/include/agg_scanline_u.h"
#include "agg/include/agg_pixfmt_rgb.h"
#include "agg/include/platform/agg_platform_support.h"
#include "agg/include/agg_ellipse.h"
#include "agg/include/agg_conv_contour.h"
#include "agg/include/agg_conv_stroke.h"
#include "agg/include/agg_renderer_base.h"
#include "agg/include/agg_path_storage.h"
#include "agg/include/ctrl/agg_slider_ctrl.h"
class the_application:public agg::platform_support
{
public:
the_application(agg::pix_format_e format, bool flip_y):agg::platform_support(format,flip_y),pix_fmt(rbuf_window()),ren_bas(pix_fmt),
m_slider1(80, 250, 600-10, 276, !flip_y),
{
add_ctrl(m_slider1);
m_slider1.range(1.0, 100.0);
m_slider1.num_steps(10);
m_slider1.value(1.0);
m_slider1.label("Pixel size=%1.0f");
m_slider1.no_transform();
}
void draw_ellipse()
//Rendering Buffer渲染的内存块,就是即将显示界面的颜色内存块,其中agg::rendering_buffer
//类提供了一系列直接操作某一个坐标点颜色的函数,例如row_ptr();
agg::rendering_buffer &rbuf = rbuf_window();
agg::pixfmt_bgr24 pixf(rbuf);
agg::rgba8 color(255,0,0);
agg::rgba c(344,1);
//Renderers渲染器,我们可以看成是油漆,颜料,
typedef agg::renderer_base<agg::pixfmt_bgr24> renderer_base_type;
renderer_base_type renb(pixf);
//Scanline Rasterizer光栅化,说她是画册,描述了所有我们即将描绘图案,颜色,线条,
//但是并不切确,因为她只是一个容器,一个记载线段,标志,详细参数的规格说明书,
//连草图都不是,她更像是一张菜谱,里面记录了食材,记录了烹饪的过程,仅此而已。
agg::rasterizer_scanline_aa<> ras;
agg::scanline_u8 sl;
ren_bas.clear(agg::rgba8(255,255,255));
int nScaleX = 0;
int nScaleY = 0;
agg::path_storage ps;
ps.move_to(300,300);
ps.line_to(320,300);
ps.line_to(320,320);
ps.line_to(300,320);
ps.line_to(300,300);
nScaleX = int(m_slider1.value())/10;
nScaleY = int(m_slider2.value())/10;
agg::trans_affine mtx;
mtx.scale(nScaleX,nScaleY); //x轴缩小到原来的一半
// mtx.rotate(agg::deg2rad(40));//旋转30度
mtx.translate(-320*(nScaleX-1) ,-320*(nScaleY-1));//X,Y坐标分别平移100
typedef agg::conv_transform<agg::path_storage> ell_ct_type;
ell_ct_type ctell(ps,mtx); //矩阵变换
typedef agg::conv_stroke<ell_ct_type> ell_cc_cs_type;
ell_cc_cs_type csccell(ctell);
ras.add_path(csccell);
agg::render_scanlines_aa_solid(ras,sl,renb,agg::rgba8(255,0,0));
ras.reset();
// Render the controls
agg::render_ctrl(ras, sl, renb, m_slider1);
return;
virtual void on_draw()
draw_ellipse();
virtual void on_mouse_button_down(int x, int y, unsigned flags)
if (flags == agg::mouse_left)
{
char str[50];
sprintf(str,"Mouse location:(%d,%d)", x, y);
message(str);
}
private:
agg::slider_ctrl<agg::rgba8> m_slider1;
agg::pixfmt_rgb24 pix_fmt;
agg::renderer_base<agg::pixfmt_rgb24> ren_bas;
};
int agg_main(int argc, char* argv[])
the_application app(agg::pix_format_bgr24,false);
app.caption("AGG Example.Anti_Aliasing Demo");
if (app.init(1800, 1800, agg::window_resize))
return app.run();
return -1;
}
本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1962143,如需转载请自行联系原作者