Ok, so here's the final code using the previous posts.This one works:
Now here is the version in which the animation should stop after 10 frames ( 10
rectangles):
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.h>
#include "random_ab.h"
#include <ctime>
int n_frames = 0; // variable that counts the number of frames
class Canvas : public Fl_Box // deriving from Fl_Box a drawable widget
{
public:
Canvas( int x, int y, int w, int h, const char *l); // constructor
~Canvas() {}; // destructor
int XC; // coordonates of the corner of the rectangle
int YC; // these coordonates will hace random values each time
void draw(); // overrided function
static void timer_cb(void *v); // timeout function
};
Canvas::Canvas( int x, int y, int w, int h, const char *l) : Fl_Box( x, y, w,
h, l)
{
box(FL_FLAT_BOX);
color((Fl_Color)255);
Fl::add_timeout(0.5,timer_cb,(void*) this); // first I call add_timeout
}
void Canvas::draw() // overriding drawing function to draw rectangle
{
Fl_Box::draw();
fl_push_clip( x(), y(), w(), h() ); // start clipping
fl_color(FL_BLACK);
fl_rect( x(), y(), w(), h() ); // draws an exterior contour
XC=rand_ab(0,200);
YC=rand_ab(0,200);
fl_rect( x() + XC, y() + YC,50,20 ); // draws the rectangle at random corner
fl_pop_clip(); // end clipping
}
void Canvas::timer_cb(void *v)
{
Canvas *c = (Canvas *) v;
c->redraw();
n_frames++;
if(n_frames < 10) // this makes the animation for only n_frames
Fl::repeat_timeout(timer_cb);
}
/******************************************************************************/
int main()
{
srand(time(0)); // added semicolon :)
Fl_Double_Window *my_win = new Fl_Double_Window(300,300, "Draw animation");
my_win->begin();
Canvas my_canvas(20,20,260,260,0); // a widget on which I draw a rectangle
my_win->resizable(my_canvas);
my_win->end();
my_win->show();
return Fl::run();
}
/******************************************************************************/
What is interesting is that the previous version that I first posted did work
this time, probably something I missed.
As you can see I don't even use Fl:remove_timeout() anymore, but I also tested
that version and it works too, so the below function is good too:
void Canvas::timer_cb(void *v)
{
Canvas *c = (Canvas *) v;
c->redraw();
Fl::repeat_timeout(0.5,timer_cb,v); // 0.5 s delay
n_frames++;
if(n_frames > 10)
Fl::remove_timeout(timer_cb);
}
and also this one:
void Canvas::timer_cb(void *v)
{
Canvas *c = (Canvas *) v;
c->redraw();
n_frames++;
if(n_frames > 10)
Fl::remove_timeout(timer_cb); // 0.5 s delay
else Fl::repeat_timeout(0.5,timer_cb,c);
}
Also, if I move the calculations of XC and YC in the timeout function, it works.
I have only one question now: since you don't really need remove_timeout() and
you can just stop calling repeat_timeout() when the time is right, what good is
remove_timeout() for?? Is it a last case resort, so you can force to stop the
timeout or what for???
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk