[cairo] Why is my simple python gtk+cairo program running so slowly/stutteringly?

Kalle Vahlman kalle.vahlman at gmail.com
Mon Feb 1 06:39:00 PST 2010


And instead of busylooping in rendering with

  gobject.idle_add(timeout)

you could limit the framerate to eg. maximum of 60fps with

  gobject.timeout_add(1000/60, timeout)

Much smoother that way ;)

 Kalle

2010/1/31 Rye Terrell <ryeterrell at ryeterrell.net>

> Thank you; that is tremendously faster.
>
> -Rye
>
> On Sun, Jan 31, 2010 at 1:31 PM, Chris Wilson <chris at chris-wilson.co.uk>wrote:
>
>> On Sun, 31 Jan 2010 13:00:58 -0600, Rye Terrell <
>> ryeterrell at ryeterrell.net> wrote:
>> > My program draws circles moving on the window. I think I must be missing
>> > some basic gtk/cairo concept because it seems to be running too
>> > slowly/stutteringly for what I am doing. Any ideas? Thanks for any help!
>>
>> You happen to be drawing the same sprite 128 times, asking for full
>> subpixel tessellation of the nigh on the saem geometry 384 times per
>> expose. This is where a canvas and scenegraph are so valuable. But for
>> this simple case:
>>
>> #!/usr/bin/env python
>>
>> import gtk
>> import gtk.gdk as gdk
>> import math
>> import random
>> import gobject
>>
>> # The number of circles and the window size.
>> num = 128
>> size = 512
>>
>> # Initialize circle coordinates and velocities.
>> x = []
>> y = []
>> xv = []
>> yv = []
>> for i in range(num):
>>    x.append(random.randint(0, size))
>>    y.append(random.randint(0, size))
>>    xv.append(random.randint(-4, 4))
>>    yv.append(random.randint(-4, 4))
>>
>>
>> # Draw the circles and update their positions.
>> def expose(*args):
>>     r = 8
>>    lw = 4
>>    c = int (r + (lw+1)/2 + 1)
>>
>>    cr = darea.window.cairo_create()
>>     cr.rectangle(0, 0, 2*c, 2*c)
>>    cr.clip()
>>    cr.push_group()
>>
>>    # draw the circle sprite
>>    cr.set_line_width(lw)
>>     cr.set_source_rgb(1, 0, 0)
>>     cr.arc(c, c, r, 0, 2 * math.pi)
>>     cr.stroke_preserve()
>>    cr.set_source_rgb(1, 1, 1)
>>    cr.fill()
>>
>>     p = cr.pop_group()
>>    cr.reset_clip()
>>
>>    for i in range(num):
>>         cr.save()
>>        cr.translate(x[i] - c, y[i] - c)
>>        cr.set_source(p)
>>        cr.paint()
>>        cr.restore()
>>
>>        x[i] += xv[i]
>>        y[i] += yv[i]
>>        if x[i] > size or x[i] < 0:
>>            xv[i] = -xv[i]
>>        if y[i] > size or y[i] < 0:
>>            yv[i] = -yv[i]
>>
>> # Self-evident?
>> def timeout():
>>    darea.queue_draw()
>>    return True
>>
>> # Initialize the window.
>> window = gtk.Window()
>> window.resize(size, size)
>> window.connect("destroy", gtk.main_quit)
>> darea = gtk.DrawingArea()
>> darea.connect("expose-event", expose)
>> window.add(darea)
>> window.show_all()
>>
>> # Self-evident?
>> gobject.idle_add(timeout)
>> gtk.main()
>>
>> --
>> Chris Wilson, Intel Open Source Technology Centre
>>
>
>
> --
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo
>



-- 
Kalle Vahlman, zuh at iki.fi
Powered by http://movial.com
Interesting stuff at http://sandbox.movial.com
See also http://syslog.movial.fi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cairographics.org/archives/cairo/attachments/20100201/462e7e3f/attachment.htm 


More information about the cairo mailing list