Usage without pyglet.app.run

780 views
Skip to first unread message

Salvakiya

unread,
Oct 3, 2015, 12:15:53 PM10/3/15
to pyglet-users
Is there a way I can avoid using the Pyglet.app.run()? I hate the way pyglet schedules events and I tend to run into performance issues quite quickly. I was hoping to avoid using them and create my own system for the gameloop.

magu...@gmail.com

unread,
Oct 3, 2015, 2:03:38 PM10/3/15
to pyglet-users
Sure, something like this perhaps?

import pyglet
from pyglet import clock
from pyglet.window import key
from pyglet.gl import *
pyglet
.options['debug_gl'] = False

class Test(pyglet.window.Window):
   
def __init__(self):
       
super(Test, self).__init__(640, 480, resizable=False, fullscreen=False, caption="Test")
       
self.clear()

   
#setup the tick counter and Frames Per Second counter
       
self.dt_count = clock.tick()
       
self.fps_display = pyglet.clock.ClockDisplay()

   
#start main loop
       
self.update()


   
def update(self):
       
while not self.has_exit:
       
#Make sure events are timed properly
           
self.dt_count += clock.tick()

       
#timer rate for updating code
           
if self.dt_count >= 0.025:
               
self.dt_count = 0

       
#render to screen and flip frame buffers
           
self.draw()

       
#manually dispatch window events
           
self.dispatch_events()


   
def draw(self):
   
#clear screen
       
self.clear()

   
#draw frames per second
       
self.fps_display.draw()

   
#flip frame buffer
       
self.flip()
       

   
def on_key_press(self,symbol,modifiers):
       
if symbol == key.ESCAPE:
           
self.close()


if __name__ == '__main__':
    window
= Test()


Salvakiya

unread,
Oct 3, 2015, 2:21:09 PM10/3/15
to pyglet-users
I think so. Does this avoid the event scheduler?

magu...@gmail.com

unread,
Oct 3, 2015, 2:41:30 PM10/3/15
to pyglet-users
I believe so. What it does is run the main loop, then the dispatch_events method checks the window's operating system event queue for user input and dispatches any events found. The method doesn't wait for input -- if there are no events pending, control is returned to the program immediately.

Salvakiya

unread,
Oct 3, 2015, 2:50:18 PM10/3/15
to pyglet-users
is the dt_count the delta_time that I should pass to my instances?

magu...@gmail.com

unread,
Oct 3, 2015, 3:55:14 PM10/3/15
to pyglet-users
No, dt_count adds up each delta_time tick, if you want to pass delta_time to your instances you'd do something like this:

def update(self):
   
while not self.has_exit:

   
#store delta_time for instances
       
self.tick = clock.tick()

Leif Theden

unread,
Oct 21, 2015, 2:30:16 PM10/21/15
to pyglet-users
Bypassing the event loop may give you other issues, like extreme CPU usage.  If you're interested, I rewrote the pyglet scheduler to use a heap queue instead of sorted list and got a nice performance boost with several hundred sprites/scheduled events.  I'll clean up my code and post it if you want to try it out. 

claudio canepa

unread,
Oct 21, 2015, 2:34:45 PM10/21/15
to pyglet...@googlegroups.com
On Wed, Oct 21, 2015 at 3:30 PM, Leif Theden <leif....@gmail.com> wrote:
Bypassing the event loop may give you other issues, like extreme CPU usage.  If you're interested, I rewrote the pyglet scheduler to use a heap queue instead of sorted list and got a nice performance boost with several hundred sprites/scheduled events.  I'll clean up my code and post it if you want to try it out. 

Please do, I could use a better scheduler 

Leif Theden

unread,
Oct 22, 2015, 12:53:03 AM10/22/15
to pyglet-users
When I was maintaining my personal pyglet fork with win32 and python3 compatibility, I rewrote the scheduler to be a bit quicker in most situations.  The standard clock is slow because it is constantly resorting the scheduled items list.  This implementation uses a heap queue to keep the list 'sorted'.  According to my own benchmarks, it is considerably quicker when dealing with a large number of scheduled events (over 200 or so...).

I'm open to suggestions and opinions.  Thanks!

Here is the clock:
https://github.com/bitcraft/pyglet/blob/master/pyglet/clock.py

Here is the benchmark I used:
https://github.com/bitcraft/pyglet/blob/master/tests/benchmarks/benchmark_heapq_clock.py

Here is the 'legacy' clock to use with the benchmark...it is just the pyglet clock from 1.2 alpha-something:
https://github.com/bitcraft/pyglet/blob/master/tests/benchmarks/clocklegacy.py

I'm not sure if this will be a drop-in replacement, but it is a start!  Hope it helps.




On Saturday, October 3, 2015 at 11:15:53 AM UTC-5, Salvakiya wrote:

Benjamin Moran

unread,
Oct 22, 2015, 8:19:06 AM10/22/15
to pyglet-users
This doesn't work with the latest release of pyglet, but I love the heap/priority queue implementation.

Did you try to get this merged into the main pyglet branch in the past? If so, any reaons why it wouldn't be accepted? The heapq module is in the standard library (and pure python), so it would be nice to see the main clock module get a rewrite utilizing it.

Leif Theden

unread,
Oct 22, 2015, 9:37:02 AM10/22/15
to pyglet-users
I didn't expect it to work without a few changes. I simplified the entire implementation and likely broke a few things. For one, I didn't like that the scheduler was tightly coupled to the system clock and event loop. My implementation is more generic. At this point, I think the easiest way to adapt it would be to use this version, then add the missing bits. It's also written for Python 3.4, so there may be some parts that won't work with 2.7.

I never pushed for inclusion in pyglet mostly because I didn't want to back port it and wasn't sure if it was needed. Guess I was wrong. I'll let this issue sit for a few days then consider merging it if I get more response. There needs to be more testing than just my own use though. It has the potential for making odd bugs due and that's scary. Anyway, I'll revisit this next week after some thought.

Benjamin Moran

unread,
Oct 22, 2015, 10:52:14 AM10/22/15
to pyglet-users
I'm on board for testing on Linux. I only use Python 3 these days, but I can test on 2.7 as well.

Leif Theden

unread,
Oct 24, 2015, 5:39:41 PM10/24/15
to pyglet-users
Sorry for hijacking the thread; I've opened a new topic on the clock.

https://groups.google.com/forum/#!topic/pyglet-users/_0eLxtKfUy4


On Saturday, October 3, 2015 at 11:15:53 AM UTC-5, Salvakiya wrote:
Reply all
Reply to author
Forward
0 new messages