How to delete sprites and text labels

1,123 views
Skip to first unread message

Hello3171

unread,
Dec 21, 2009, 8:12:22 AM12/21/09
to pyglet-users
I was just wondering how sprites and text labels (in particular) were
being cleaned up. I don't want to have a design flaw in my game which
means later on when I do find that I wasn't doing things properly it
is hard to fix the problem.

Currently for every sprite I make the process is similar to this
(simplified):

#Initially I create a sprite batch (only once)
sprite_batch = pyglet.graphics.Batch()

#This occurs for every sprite that I make
texture = pyglet.resource.image(filename)
sprite = pyglet.sprite.Sprite(texture, batch=self.sprite_batch)
#Set sprite properties
sprite.color = ...

#Draw the sprites
sprite_batch.draw()


Now for cleaning up I simply call: "sprite.delete()" for every sprite
when I want to destroy my object. I just started doing this but I have
no idea what the correct procedure is (and the docs don't seem to have
them anywhere clearly explained!)

How are the lifetimes of sprites, text labels and sprite batches
managed? Do I need to do anything (such as call sprite.delete() ) for
anything?

Jonathan Hartley

unread,
Dec 21, 2009, 5:24:03 PM12/21/09
to pyglet-users

I was wondering exactly the same thing, especially since my Sprite
images are dynamically generated Textures (derived from Labels to
accelerate text rendering) Do I need to call delete on the Texture, if
no other Sprites reference it, or just on the Sprite?

Casey Duncan

unread,
Dec 21, 2009, 6:30:37 PM12/21/09
to pyglet...@googlegroups.com
On Mon, Dec 21, 2009 at 3:24 PM, Jonathan Hartley <tar...@tartley.com> wrote:
> On Dec 21, 1:12 pm, Hello3171 <hello3...@googlemail.com> wrote:
[..]

>> How are the lifetimes of sprites, text labels and sprite batches
>> managed? Do I need to do anything (such as call sprite.delete() ) for
>> anything?
>
> I was wondering exactly the same thing, especially since my Sprite
> images are dynamically generated Textures (derived from Labels to
> accelerate text rendering) Do I need to call delete on the Texture, if
> no other Sprites reference it, or just on the Sprite?

Calling delete on the sprite deletes the vertex list and removes the
reference to the texture from the sprite, but does not explicitly
delete the texture from video memory because other things may still be
using it.

Note that if nothing else is referencing the texture, it will
eventually get removed from video memory when the texture object is
garbage collected. This is probably fine unless you know you need to
reclaim the video memory immediately for other uses. In that case you
should call delete on the texture yourself.

-Casey

Hello3171

unread,
Dec 22, 2009, 12:36:48 PM12/22/09
to pyglet-users
So once all sprites which reference a texture are destroyed, the
texture will be removed by itself whenever (by the gc)?
To be clear, in these scenarios:

#my object does this
sprite = pyglet.sprite.Sprite(texture)

If I have one single reference to this object and I delete this
reference, the object should be free to be garbage collected. And when
this happens the sprite should be garbage collected. I do not need to
call sprite.delete()

2#My object does this:
sprite = pyglet.sprite.Sprite(texture, batch=batch)

In this case I do need to call sprite.delete() to remove the sprite
from the sprite batch, only then can it be garbage collected.

OR is it more appropriate to do this:

sprite.batch = None

This would remove it from the batch?

On Dec 21, 11:30 pm, Casey Duncan <casey.dun...@gmail.com> wrote:

Casey Duncan

unread,
Dec 22, 2009, 1:01:48 PM12/22/09
to pyglet...@googlegroups.com
On Tue, Dec 22, 2009 at 10:36 AM, Hello3171 <hell...@googlemail.com> wrote:
> So once all sprites which reference a texture are destroyed, the
> texture will be removed by itself whenever (by the gc)?

So long as nothing else references that texture, yes. However there is
no way to tell how long that will take. Though in many cases that is
not important.

> To be clear, in these scenarios:
>
> #my object does this
> sprite = pyglet.sprite.Sprite(texture)
>
> If I have one single reference to this object and I delete this
> reference, the object should be free to be garbage collected. And when
> this happens the sprite should be garbage collected. I do not need to
> call sprite.delete()
>
> 2#My object does this:
> sprite = pyglet.sprite.Sprite(texture, batch=batch)
>
> In this case I do need to call sprite.delete() to remove the sprite
> from the sprite batch, only then can it be garbage collected.
>
> OR is it more appropriate to do this:
>
> sprite.batch = None
>
> This would remove it from the batch?

The batch does not contain a reference to the sprite. The batch is a
place for the sprite to store its vertex list. When you are using a
batched sprite, you should call sprite.delete() when you are done with
it, otherwise it's vertex list may remain in the batch for a while
until the sprite object is collected. This could cause a "ghost" of
the sprite to continue to be drawn until collection actually occurs.

Note the docstring for Sprite.delete:

'''Force immediate removal of the sprite from video memory.

This is often necessary when using batches, as the Python garbage
collector will not necessarily call the finalizer as soon as the
sprite is garbage.
'''

-Casey

Hello3171

unread,
Dec 22, 2009, 1:17:49 PM12/22/09
to pyglet-users
Thank you for your help and clarifications!

On Dec 22, 6:01 pm, Casey Duncan <casey.dun...@gmail.com> wrote:

Jonathan Hartley

unread,
Dec 23, 2009, 1:58:50 PM12/23/09
to pyglet-users

On Dec 22, 12:01 pm, Casey Duncan <casey.dun...@gmail.com> wrote:

Good to know, thanks very much Casey

Reply all
Reply to author
Forward
0 new messages