天天看點

AGG第二十三課 ctrl目錄下agg_slider_ctrl基本使用

引言:

    在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,如需轉載請自行聯系原作者

繼續閱讀