Re: cannot use clock() for realtime graphical simulations



On Sat, 2004-08-14 at 12:03, edward hage wrote:
> Hello,
> 
> I have a simulation running in a drawing_area, I start this simulation with 
> gtk_timeout_add (50, Repaint, da).
> 
> gint Repaint (gpointer data)
> {
>    GtkWidget *da = (GtkWidget *) data;
>    GdkRectangle update_rect;
>    static double t = 0.0;
> 
> etc. etc. define graphics in pixmap and then repaint the window with the pixmap.
> 
> }
> 
> 
> To get the timing I have defined a static double t = 0.0;
> which I let count with t +=t + 0.050;
I am assuming this is a typo, since
t += t + 0.050 is different from
t += 0.050 which is what i think you would be expecting.

> Then I can look up the correct value in a table for the given time.
> 
> This works okay, well the block move but not exactly at the right time. It does not work 
> very accurate because I assume the function is called every 50 ms, but when I move my 
> mouse around then that can not be quaranteed. It slows down the simulation.
If you have heavy calculations and the repaint() function takes allot of
time to do it then you can feel the lag visually. Otherwise it
shouldn't. If you are designing a timed iteration for painting the
screen you should be aware that since it's being run in the main loop it
will halt subsequent paint calls, therefore i would advise you to do the
heavy calculations in a separate thread and then read only values from
the painting iteration, this will be a) more efficient, separate the
graphical part from the internals.

> 
> My idea was to overcome this problem by calling clock() (using <time.h>) in Repaint. This 
> does not work properly. The time seems to freeze, and only when I move my mouse around or 
> drag the window the time is updated.
> 
> Why does clock() not work properly in this case and how can I overcome this problem ? Does 
> anybody have experience with graphical simulations?
> 
> The source looks like this (inside Repaint)
> 
> static double t = 0;
> static double tstart;
> 
> ClockResult = clock();
> if (t==0)  tstart = (double) ClockResult / (double) CLOCKS_PER_SEC;
> t = (double) ClockResult  / (double) CLOCKS_PER_SEC - tstart + 0.00001;
> 
> 
> Thank you in advance for your help,
> Edward
> 
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]