shailesh / pyglet

Automatically exported from code.google.com/p/pyglet
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Documentation does not prominently suggest setting window.invalid = False in on_draw #399

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
If I just copy and paste the code from 
http://pyglet.org/doc/programming_guide/new_features_replacing_standard_pra
ctice.html to create a basic application loop it will look like this:

import pyglet

window = pyglet.window.Window()

@window.event
def on_draw():
    window.clear()

def update(dt):
    pass
pyglet.clock.schedule_interval(update, 1/60.0)

pyglet.app.run()

And it will (at least on Windows) use 100% CPU. You have to put 
window.invalid = False in on_draw to avoid this problem, but I had a hard 
time figuring it out. I did not come across this in the documentation and 
finally found out by dissecting a PyWeek game.

I think the documentation could be made clearer on this or possibly even 
the API could be changed to clear the invalid flag after drawing (similarly 
to how it flips the image automatically).

Original issue reported on code.google.com by darabos....@gmail.com on 10 Feb 2009 at 2:33

GoogleCodeExporter commented 9 years ago
This flag is a bit mysterious altogether - in particular, it seems very tricky 
to synchronise window redraw to the 
slower of 2 scheduled intervals.

Original comment by swiftcoder on 27 Feb 2009 at 1:13

GoogleCodeExporter commented 9 years ago
I don't pretend to understand what's going on here, but just in case it's a
significant data point, without setting window.invalid = False, I see 100%
kernal-mode cpu use. When I do set it, I still see 100% cpu, but now in 
user-mode.

Pyglet 1.1.3 on Windows XP, Python 2.6

Original comment by tart...@gmail.com on 15 Jul 2009 at 12:10

GoogleCodeExporter commented 9 years ago

Original comment by benjamin...@gmail.com on 20 Aug 2009 at 2:03

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I confirm this one. It looks like both window update functions (on_draw) AND 
the scheduled procedure calls (via clock.schedule_interval) are handled by the 
same event loop so it is NOT POSSIBLE to have window.on_draw called at 30 FPS 
and some other functions at 90 times per seconds - it will all go down to the 
lowest scheduled timeframe. 

I hit this bug on Windows XP. 

Original comment by michal....@gmail.com on 19 Mar 2011 at 11:48

GoogleCodeExporter commented 9 years ago
... so what I do is: 

1) I create my custom event loop with idle() method overridden 

2) I am NOT doing anything in the idle loop, especially I am NOT redrawing ANY 
WINDOWS 

3) I register re-drawing of the window with clock.schedule_interval.

This way, on Windows, I can have half the CPU usage, redraws with the FPS I 
like and so on.

Original comment by michal....@gmail.com on 19 Mar 2011 at 12:08

GoogleCodeExporter commented 9 years ago
Can you please download latest version from the repository?

http://pyglet.googlecode.com/archive/tip.zip

Just to see if this makes any difference. Thanks!

Original comment by useboxnet on 6 Nov 2013 at 6:51

GoogleCodeExporter commented 9 years ago
Hi,

I have downloaded trunk SVN checkout AND I ran the example program included in 
the first message of this issue. 

CPU usage was pretty decent, which was around 0%. Pretty good result for a 
program, that does /almost/ nothing.

Original comment by michal....@gmail.com on 10 Nov 2013 at 6:26

GoogleCodeExporter commented 9 years ago
OK, thanks Michal!

Daniel, looks like there's a problem with the generated documentation because I 
can see a comment about invalid in the Window Class. I'll investigate what's 
going on.

Original comment by useboxnet on 16 Nov 2013 at 11:04

GoogleCodeExporter commented 9 years ago
I've regenerated the online docs for 1.2 using a patch for sphinx [1] and it 
shows the documentation properly, see:

http://pyglet-current.usebox.net/api/window/pyglet.window.Window.html#pyglet.win
dow.Window.invalid

[1]: https://groups.google.com/d/msg/pyglet-users/QaD2AUYu2WI/0gMwLf97c1AJ

Original comment by useboxnet on 17 Nov 2013 at 4:41

GoogleCodeExporter commented 9 years ago
Ignore my last comment, that document is not correct. We're working on it 
though.

Original comment by useboxnet on 17 Nov 2013 at 4:42