IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    AGG入门(二) - 平台支持

    Shihira发表于 2012-07-24 08:28:00
    love 0

    一、先看看下面的代码,并试着编译下:

    #include <platform/agg_platform_support.h>
    #include
    #include
    #include

    #include

    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) //初始化渲染器
    {
    }

    virtual void on_draw()
    {
    ren_bas.reset_clipping(true);
    ren_bas.clear(agg::rgba8(255, 255, 255));
    }

    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);
    }
    }

    virtual void on_key(int x, int y, unsigned key, unsigned flags)
    {
    if(key == agg::key_return && flags == agg::kbd_shift) {
    unsigned img = 0, states;
    states = create_img(0, 500, 500);
    states = load_img(img, "Steve-and-Bill.bmp");
    copy_img_to_window(img);
    update_window();
    }
    }

    private:
    agg::pixfmt_rgb24 pix_fmt;
    agg::renderer_base ren_bas;

    };

    int agg_main(int argc, char* argv[])
    {
    the_application app(agg::pix_format_rgb24, true);
    app.caption("AGG Test");

    if(app.init(500, 500, agg::window_resize)) {
    return app.run();
    }
    return -1;
    }
    如果不出意外,在窗口中点击鼠标左键将会出现对话框提示当前鼠标的位置,而按下Shift+Enter将会在窗口中显示在工作目录下的位图“Steve-and-Bill.bmp”;

    二、解释

    先看看头文件:platform/agg_platform_support.h,它里边定义了一个platform_support类——它允许你建立一个窗口来测试你的图形,并用鼠标键盘去控制它。

    类型

    • class platform_support

    主要成员函数

    • platform_support(pix_format_e, bool) : 构造函数。设置窗口风格和y轴是否上下翻转;
    • width() :返回窗口的宽;
    • height() :返回窗口的高;
    • caption([ const char* ]) :设置标题或返回标题字符串;
    • format() : 返回窗口风格;
    • message(const char*) :弹出对话框(没有风格可选);
    • run() :运行窗口;
    • force_redraw() :重绘窗口,调用on_draw();
    • update_window() :更新窗口,既是把渲染缓存中已有的内容写入窗口,不调用on_draw()。
    • platform_support为我们提供了一个很好地绘图平台,它有一系列的函数可用于操作位图(BMP或PPM):
      • create_img():创建一个编号为idx的位图;
      • save_img():将位图idx保存到文件中;
      • load_img():从文件中加载位图到idx中;
      • copy_img_to_window():把idx拷贝到渲染缓存里;
      • copy_img_to_img():把idx拷贝到另一幅位图里;
      • copy_window_to_img() :将渲染缓存里的内容拷贝到位图。

    虚函数(一般都是些消息,要覆盖它以让消息循环调用)

    • on_init() :窗口初始化时调用;
    • on_resize(int, int) :改变大小时调用;
    • on_idle() :空闲时调用;
    • on_mouse_move(int, int, unsigned) :鼠标移动时调用;
    • on_mouse_button_down(int, int, unsigned) :鼠标按下时调用;
    • on_mouse_button_up(int, int, unsigned) :鼠标弹起时调用;
    • on_key(int, int, unsigned, unsigned) :键盘打字时调用;
    • on_draw() :窗口重绘时调用。

    三、结语

    platform_support 的功能不仅仅是这么多,除此之外,他还能使用控件,等等。但很多时候,成熟的应用是不会使用它的,因为它封装了太多,虽然保证了跨平台性,却缺乏了自由性。platform_support 的主要作用是测试图像和修改图像,方便工作和移植……还有,方便初学者入门……


    Shihira 2012-07-24 16:28 发表评论


沪ICP备19023445号-2号
友情链接