What to do if you run Windows and "on_draw" isn't getting called in your basic application. For posterity.

168 views
Skip to first unread message

Cameron Seebach

unread,
Apr 2, 2012, 4:32:58 PM4/2/12
to pyglet...@googlegroups.com
Hi Everybody! I hope you are all doing well today.

Today I started exploring pyglet again after a considerable time away, and I ran into a curious problem. I could not get pyglet to call "on_draw" in even the most simple of applications!

Here's a demonstration:

================ 
import pyglet

window = pyglet.window.Window()
draw_count = 0

@window.event
def on_draw():
    global draw_count
    window.clear()
    draw_count += 1
    print "drawing {}".format(draw_count)

pyglet.app.run()
================ 

If you're using the latest hg repository pyglet (revision 2632 as of this post), and you're running Windows like me, you'll probably see just this:

C:\Users\cseebach>python pyglet_draw_demo.py
drawing 1

If you minimize and restore (hide and unhide) the window, you'll see a few more draw reports.

If you're anything like me, you panicked! This is basically what the "Hello, World!" tutorial in the docs says to do. What on Earth is going on?

You're about to see that this behavior is actually a feature, and not a bug.

I've changed the code slightly to add an update() method which does nothing, called approximately once every 1/60th of a second.

================
import pyglet

window = pyglet.window.Window()
draw_count = 0

@window.event
def on_draw():
    global draw_count
    window.clear()
    draw_count += 1
    print "drawing {}".format(draw_count)

def update(dt):
    pass

pyglet.clock.schedule_interval(update, 1/60.0)

pyglet.app.run()
================ 

Try running that sucker!

C:\Users\cseebach>python pyglet_draw_demo_2.py
drawing 1
drawing 2
drawing 3
...
drawing 3242562461     #(no, I didn't actually run it for 15,000 hours, but you get the point)
etc...

Here's my theory, which I hope will be supported and elucidated by somebody more familiar with the win32 event loop code in pyglet:

pyglet only calls "on_draw" when there are other events to be dispatched. Previously, there were no events in our loop to dispatch! Now that we've scheduled update() for 60 times a second, our loop is no longer empty, and pyglet will happily draw our stuff! This minimizes the cpu and gpu usage of your application, which I believe is more important than always calling on_draw.

Hope this saves somebody a little time and frustration when learning pyglet.

--Cam

Peter Enerccio

unread,
Apr 3, 2012, 3:16:55 AM4/3/12
to pyglet...@googlegroups.com
Actually, on_draw is called when window is considered dirty, ie it needs to be redrawn. Otherwise, window is idling.

2012/4/2 Cameron Seebach <csee...@sinfoniasolutions.com>

--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pyglet-users/-/Vxahw9FqL9wJ.
To post to this group, send email to pyglet...@googlegroups.com.
To unsubscribe from this group, send email to pyglet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyglet-users?hl=en.



--
Bc. Peter Vaňušanik
http://www.bishojo.tk

claudio canepa

unread,
Apr 3, 2012, 9:26:42 AM4/3/12
to pyglet...@googlegroups.com
On Tue, Apr 3, 2012 at 4:16 AM, Peter Enerccio <ener...@gmail.com> wrote:
Actually, on_draw is called when window is considered dirty, ie it needs to be redrawn. Otherwise, window is idling.



It should call on_draw when dirty, but that not always happen, at least in windows. Issue 562,

claudio

--

Reply all
Reply to author
Forward
0 new messages