Hello everyone, I was trying to make some simple program that involves
animation, and I came up whit the idea of making a program that draws a
rectangle at a different position each half a second.That seems pretty simple.I
managed to do that, but until I exit the aplication, the animation never
stops.So I wanted to make a version of it that stops after a number of
frames.But I couldn't do it.
Here's the code for the animation that runs forever (which I meant to do):
#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>
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();
Fl::repeat_timeout(0.5,timer_cb,c); // 0.5 s delay
}
/****************************************************************************************************/
int main()
{
srand(time(0))
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();
}
/****************************************************************************************************/
That program works.Also, the rand_ab() function is a function that returns a
random number between 2 numbers.Here it is:
#include <cstdlib>
using namespace std;
int rand_ab(int a, int b)
{
int x;
x = rand() % (b-a+1) + a;
return x;
}
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();
Fl::repeat_timeout(0.5,timer_cb,c); // 0.5 s delay
n_frames++;
if(n_frames > 10) // this should stop the animation but it doesn't
Fl::remove_timeout(timer_cb);
}
/****************************************************************************************************/
int main()
{
srand(time(0))
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();
}
/****************************************************************************************************/
As you can see, I've addes a variable the increases by 1 every time the
timer_cb() function is called.But it does not work.How can I stop this
animation after a certain number of frames.I know it has something to do whit
remove_timeout(), but how do I do it.Please help me if you can.I know that I'm
close. :)
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk