The drastic reduction in performance is because each Label creates its
own groups which are children of the passed-in group, rather than
reusing the default Label groups.
Here's a demonstration of how you can work around this:
import pyglet
import random
window = pyglet.window.Window(512,512,vsync=0)
fps_display = pyglet.clock.ClockDisplay()
pyglet.clock.schedule(lambda dt: None)
batch = pyglet.graphics.Batch()
group = pyglet.graphics.OrderedGroup(0)
# group = None
LABEL_GROUPS = {}
LABEL_GROUP_REFCOUNTS = {}
class Label(pyglet.text.Label):
def _init_groups(self, group):
if not group:
return # use the default groups
if not LABEL_GROUPS.has_key(group):
top_group = pyglet.text.layout.TextLayoutGroup(group)
background_group = pyglet.graphics.OrderedGroup(0,
self.top_group)
foreground_group = \
pyglet.text.layout.TextLayoutForegroundGroup(1,
self.top_group)
foreground_decoration_group = \
pyglet.text.layout.TextLayoutForegroundDecorationGroup
(
2, self.top_group)
LABEL_GROUPS[group] = (top_group,
background_group,
foreground_group,
foreground_decoration_group)
LABEL_GROUP_REFCOUNTS[group] = 0
self.top_group, self.background_group, self.foreground_group,
\
self.foreground_decoration_group = LABEL_GROUPS[group]
LABEL_GROUP_REFCOUNTS[group] += 1
def delete(self):
pyglet.text.Label.delete(self)
group = self.top_group.parent
if group is not None:
LABEL_GROUP_REFCOUNTS[group] -= 1
if not LABEL_GROUP_REFCOUNTS[group]:
del LABEL_GROUP_REFCOUNTS[group]
del LABEL_GROUPS[group]
self.top_group = self.background_self =
self.foreground_group \
= self.foreground_decoration_group = None
labels = []
for i in range(100):
x = int(random.random()*window.width)
y = int(random.random()*(window.height - 80)) + 64
labels.append(Label('Hello world !',
x=x,y=y,batch=batch,group=group))
def kill_labels(dt):
for label in labels:
label.delete()
del labels[:]
assert not LABEL_GROUPS
assert not LABEL_GROUP_REFCOUNTS
pyglet.clock.schedule_once(kill_labels, 20)
@window.event
def on_draw():
window.clear()
batch.draw()
fps_display.draw()
pyglet.app.run()
I'd had something like this in mind to speed up Kytten, since I notice
performance was suffering when I had a lot of labels or documents on
the screen at one time, but your test program dramatically
demonstrates the difference this optimization makes.
On Jun 23, 4:50 am, Nicolas Rougier <[email protected]> wrote:
> Hi all,
>
> I tried to use labels with ordered groups and it seems to dramatically
> decrease performances. Can anyone confirm ? Here is a quick test, just
> replace "group" with None for fast, in my case, when group is an
> OrderedGroup, I get around 50 fps while with no group I get around
> 1600 fps.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en
-~----------~----~----~----~------~----~------~--~---