On Mar 18, 2007, at 9:57 PM, Maximilian Matthé wrote:
> So how should one do a simple animation in fltk?

All GUI libraries are event based, which basically means that  
anything (or rather "any event") can happen at any time. You can't  
really think in a serial order.

The user clicking on the start button is just one of many events,  
which changes the /state/ of your program. Before the click, the  
program was idling around. After the click, you are in "solving"  
state. In solving state, the program is supposed to show an animation  
step by step until some other event happens (the puzzle is solved,  
for example).

But the human eye can only grasp 25 frames (or steps) per second, so  
there is no need to run the poor CPU at full speed. Just solve one  
step every 1/25th of a second.

So what I would do is
- write a callback function for the start button
- in the callback function, call Fl::add_timeout(1/25.0, timerCB, 0)
- now write another function, called timerCB
- in timerCB, solve one step of the problem, don't draw anything
- at the end of timerCB, if the puzzle is not solved yet, call  
Fl::repeat_timer(1.25.0, timerCB, 0)
- as a last step, call MyWidget->redraw();

The button callback will start your timer. The timerCB will solve  
your puzzle (or animation) one step at a time and make sure that it  
will be called again in another 25th of a second. It will also tell  
your rendering widget to redisplay the changes you just calculated.

Now implement MyWidget by deriving it from soem other widget and  
overriding the draw() function to erase the background and then draw  
your whole puzzle again using the dataset that was calculated in  
timerCB.

Even though this may seem kind of backwards, this is by far the best  
way for a "simple" animation in an event-based system. It doesn't  
matter if your rendering is in OpenGL of FLTK code (or WIN32 GDI, if  
really needed, but you lose cross platform compatibility). Your other  
applications will thank you for not using more CPU time than needed,  
plus your program will always run at the same speed on all platforms.

Matthias

PS: exceptions: if you want to o as much calculation as possible  
during the display phases, or if you have overlapping calculations  
that are much longer than 1/25th of a second, you will be entering  
the wonderful field of multithreading... .

----
http://robowerk.com/


_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to