gtkD: How to paint to screen for animation

Michelle Long HappyDance321 at gmail.com
Thu Mar 21 18:53:03 UTC 2019


On Tuesday, 19 March 2019 at 19:03:37 UTC, Mike Wey wrote:
> On 19-03-2019 01:54, Michelle Long wrote:
>> I've added a function to addOnDraw for a DrawingArea and it 
>> paints using the code I have when I resize.
>> 
>> I added a queueDraw in threadsAddIdle and it seems to draws 
>> the screen immediately but it does not seem to be called again.
>> 
>> If I put queueDraw inside the addOnDraw routine then the 
>> animation works but it is quite slow, about 1 fps and cpu 
>> usage is 100% without it, it is 0%.
>
> You will probably want to use glib.Timeout to make the time 
> between redraws consistent.
> The callBack for treadsAddIdle or glib.Idle is only called when 
> the mainloop has nothing else to do.
>
> The cairo clock demo is a good example: 
> https://github.com/gtkd-developers/GtkD/blob/master/demos/cairo/cairo_clock/clock.d
>
> If performance is an issue one option would be to save your 
> context in a cairo surface and only redraw the parts that have 
> changed.


This seems to be not any different than calling queueDraw inside 
AddOnDraw. I did the timing code and it does seem to say that I'm 
getting max 60fps so maybe it is quite fast.

I was bitting an imagine and moving a rect across the screen at 
1px per frame. I though it should be moving much faster but 
apparently my mental calculations are not all that great. 1680/60 
~= 28 so it should take nearly half a minute to move the rect 
from one side to the other.

The behavior was essentially identical. I'm curious if the 
drawing routine itself uses Timeout to get 60fps and adding 
another timeout is only useful to regulate a slower fps?




More information about the Digitalmars-d-learn mailing list