Mercurial > py-sdl2
changeset 144:bb3193f65f33 sub-sprite
Use subsurface() to implement a subsprite() method for SoftwareSprites
| author | Jan Varho <jan@varho.org> |
|---|---|
| date | Fri, 17 Jan 2014 15:42:49 +0200 |
| parents | 303befb0b617 |
| children | a1f0310959b1 |
| files | sdl2/ext/sprite.py |
| diffstat | 1 files changed, 14 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/sdl2/ext/sprite.py Fri Jan 17 15:42:39 2014 +0200 +++ b/sdl2/ext/sprite.py Fri Jan 17 15:42:49 2014 +0200 @@ -5,6 +5,7 @@ from .compat import * from .color import convert_to_color from .ebs import System +from .surface import subsurface from .window import Window from .image import load_image from .. import blendmode, surface, rect, video, pixels, render, rwops @@ -313,6 +314,19 @@ """The size of the SoftwareSprite as tuple.""" return self.surface.w, self.surface.h + def subsprite(self, area): + """Creates another SoftwareSprite from a part of the SoftwareSprite. + + The two sprites share pixel data, so if the parent sprite's surface is + not managed by the sprite (free is False), you will need to keep it + alive while the subsprite exists.""" + ssurface = subsurface(self.surface, area) + ssprite = SoftwareSprite(ssurface, True) + # Keeps the parent surface alive until subsprite is freed + if self.free: + ssprite._parent = self + return ssprite + def __repr__(self): return "SoftwareSprite(size=%s, bpp=%d)" % \ (self.size, self.surface.format.contents.BitsPerPixel)
