FLTK简单动画 [英] FLTK simple animation

查看:351
本文介绍了FLTK简单动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以成功编译并运行Hello World代码。
现在我想做动画之类的事情。

I can successfully compiled and run the Hello World code. Now I want to do something like animation.

我首先创建一个矩形类来实现Fl :: widget

I first create a rectangle class to implement draw() from Fl::widget

class myRect: public Fl_Widget {
private:
    Fl_Color color;
    void draw(){
        fl_color(color);
        fl_rectf(x(),y(),w(),h(),color);
    }
public:
    myRect(int X,int Y,int W,int H, Fl_Color c) : Fl_Widget(X,Y,W,H),color(c) {}
};



int main (int argc, char ** argv)
{
    Fl_Window *window = new Fl_Window (300, 180, "FLTK Test");

    vector<myRect*> allRect;
    for(int i=0; i<10; ++i){
        allRect.push_back(new myRect ((i*10)%100,100,50,50,i%256));
    }
    window->end();
    window->show();

    return Fl::run();
}

上面的代码可以按预期运行。
但是,现在我想以一定的时间间隔(例如1秒)一一显示矩形。
使其像动画一样。

The code above can run as I expected. But Now I want to show the rectangles one by one, with some time interval such as 1 second. Make it just like animation.

我已经阅读了官方文档,但我对此一无所知。
请给我一些信息。谢谢!!

I have read the official document but I still have no idead about that. Please give me some information. Thanks !!

感谢DejanLekic,我将代码修改如下:

Thanks to DejanLekic, I revised my code as below:

#include <iostream>
#include <vector>
#include <FL/Fl.H>
#include <FL/Fl_Widget.H>
#include <FL/Fl_Double_Window.H>
#include <FL/fl_draw.H>

using namespace std;

class myRect: public Fl_Widget {
private:
    Fl_Color color;
    void draw(){
    fl_color(color);
    fl_rectf(x(),y(),w(),h(),color);
}

public:
    myRect(int X,int Y,int W,int H, Fl_Color c)
        :Fl_Widget(X,Y,W,H),color(c) {}
};

vector<myRect*> allRect;

void winUpdate(void *data)
{
    static unsigned i = 0;
    Fl_Double_Window *o = (Fl_Double_Window*)data;
    if(i < allRect.size()){
        o->add(allRect[i]);
        if(i>=3) o->remove(allRect[i-3]);
        o->redraw();
        Fl::add_timeout(0.5,winUpdate,data);
        ++i;
    }
}

int main (int argc, char ** argv)
{
    for(int i=0; i<8; ++i){
        allRect.push_back(new myRect(i*30,i*30,50,50,i));
    }
    Fl_Double_Window *window = new Fl_Double_Window (400, 400, "FLTK Test");
    Fl::add_timeout(2,winUpdate,window);
    window->end();
    Fl::visual(FL_DOUBLE|FL_INDEX);
    window->show();
    return Fl::run();
}

似乎运行良好,但我不确定是否正确或不。
如果有任何问题,请告诉我。谢谢。

It seems to run well, but I'm not sure whether it is correct or not. If there is any problem, please let me know. Thanks.

推荐答案

好,您走对了路。

下面是一个完整的示例,说明如何使用FLTK的绘图功能制作简单的2D动画: http:// seriss.com/people/erco/fltk/#AnimateDrawing

Here is a complete example how to do a simple 2D animation using FLTK's drawing capabilities: http://seriss.com/people/erco/fltk/#AnimateDrawing

使用OpenGL的类似方法: http://seriss.com/people/erco/fltk/#OpenGlInterp

Similar thing using OpenGL: http://seriss.com/people/erco/fltk/#OpenGlInterp

The键,在两个示例中都位于 Fl :: add_timeout(0.25,Timer_CB,(void *)this); 行,以及 Timer_CB( )静态(回调)方法。这两个例子都很好,很简单,我相信您会立即理解它们。

The key, in both examples is in the Fl::add_timeout(0.25, Timer_CB, (void*)this); line, and in the Timer_CB() static (callback) method. Both examples are nice and easy and I am confident you will understand them instantly.

这篇关于FLTK简单动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆