On 07/21/11 04:45, Paul R wrote:
> Hi all, i am a bit stuck with a program that is running far too fast
> it is an 3d implementation on the game of life and i am not sure where
> and or how to place fl timeouts, or even if that is the right option.
First: short answer, if your program is already written
like your example, just change the Fl::check() to Fl::wait(0.1)
and you're done. No timeout code needed.
However, if you want to use timeouts, and want to rewrite
your app to be more like most GUI apps that are 'callback'
oriented, you'd have to change your program around a bit
to be more like a regular GUI 'callback' app, eg:
----
class YourWindow : public Fl_Window {
float speed;
YourWindow(..) { // CTOR
..add sliders, init variables, whatever..
speed = 0.1;
Fl::add_timeout(speed, TimerCallback);
}
void TimerCallback(..) { // invoked when timer goes off
CalculateWorld();
redraw();
Fl::repeat_timeout(speed, CalculateWorld);
}
void CalculateWorld() {
..your calculations..
}
void draw() {
..your draw code..
}
};
main() {
YourWindow win(..);
return(Fl::run());
}
----
For an example of this, see:
http://seriss.com/people/erco/fltk/#AnimateDrawing
..which shows how to draw a clock's "second hand"
at "almost" exact 1.0 second intervals.
In that example, the "calculation" (sin/cos) is done in
the draw() routine.
But if your app works with an array of data,
you could do your calculation in the timer routine
so that the draw() code just draws the array.
This keeps your calculation code separate from the draw() code.
Either way, the timer callback should always finish
by calling redraw() and repeating the timeout
before returning to the app loop so it can call draw(),
and later invoke the next timer callback.
To make the speed adjustable, add a slider with a callback
that adjusts the speed variable, and use the speed variable
in the add_timeout/repeat_timeout calls.
That's the normal way to write GUI applications; with
callbacks, and surrendering to FLTK's 'application loop'
Fl::run() to handle all execution of your app. The app
spends most of its time waiting, the timer basically
keeps the app 'alive', giving you cpu every 1/10th of
a second (due to the 0.1 in the timer calls).
The way the machine actually runs your app would be:
FLTK run() loop (waits for stuff to happen)
|
\|/
0.1 timer goes off, invokes
your timer callback ---> YourTimerCallback()
|
you do calculations
|
you call redraw() which schedules next draw
|
you call repeat_timeout to schedule next timer event
|
|<------------------- you return
|
\|/
FLTK run() loop continues
|
\|/
FLTK sees redraw() scheduled,
calls your draw() ---> draw()
|
you draw stuff
|
|<---------------- you return
|
\|/
FLTK run() loop continues
|
(back to top)
But if you want to do it this way:
> void UpdateWorld()
> {
> while(running)
> {
> //code to examine each cell and determine its status
> //
> //
> Fl::check();
> if(running) //button callback controls bool member
> DrawWorld(); //step through arrays drawing live cells or not
> //includes call to cellGrp->redraw()before returns
> }
> }
..then you don't really need add_timeout/repeat_timeout,
just change the Fl::check() line to Fl::wait(0.1) instead.
To make the time adjustable, make the "0.1" a variable,
and add a slider that invokes a callback to adjust the variable.
Either way should work. If your app is already written
with the above while(running) loop, it's probably easiest
to change Fl::check() to Fl::wait(0.1).
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk