[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Re: Drawing/Surface performance
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Re: Drawing/Surface performance
- From: "Stuart Axon" <stu.axon+pygame@xxxxxxxxx>
- Date: Fri, 1 Feb 2008 01:22:05 +0000
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Thu, 31 Jan 2008 20:22:14 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; bh=fCSmIsxXWTUWA/W+JThYn9wQj3YyDCbu+u4EOtOHOeM=; b=Ios6C68sL6argtHHfDMsLcXvqehwd0uM2bqq5RBHZ5JOtGL9NBCxpcq6bsV6f+odupYDbNyeiVu0LHhcVC7EciQ85+2Oqr+RdYua8N5mmDT9Dj9zAZr/l3YGxOWAEIHJCNQQk7K+Urp5jclscIbAv0/lInVodZ8md52ak5ubObc=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth; b=M6jxOhex84/29VboYD3q7JVtHaLokGS58w6vAgDLrp+5hC/+CsISU8Ap4/Euvuw9d6jm2zPuMnZUxc3VpueyLNZHQJNFH4j8RwC2zHTwMzb5lmmjrpXQIl71Ytu3vBte3d2pq8LiGmM9Xp0rUNTpM1h++JxZbCx/NkDCMCSVgLE=
- In-reply-to: <838f4dc10801311540x4e0aa908oabd4fe7566e13cd4@xxxxxxxxxxxxxx>
- References: <838f4dc10801311540x4e0aa908oabd4fe7566e13cd4@xxxxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
I simplified my create_surface... by keeping copies of the indexed
(greyscale) image and it's palette; but still slow...
I'm wondering -
Would locking surfaces help
Is the pygame 1.8 buffer object relevant here?
Would surarray be quicker?
The palette manipulation seems like it could def be quicker; but my
python knowledge isn't good enough here.
For performance reference I'm using a 1.5ghz centrino core 2 thingy.
Simplified create_surface:
def create_surface(self, colour):
# Check cache
#p_colour = utils.pack_colour(colour)
#if p_colour in Block.surface_cache:
# return Block.surface_cache[p_colour]
pal_image = Block.indexed_image.copy()
# Manipulate the palette
pal_image.set_palette(self.colourise_palette(Block.indexed_palette,
colour))
image = pygame.Surface((Block.width, Block.height), pygame.SRCALPHA, 32)
image.blit(pal_image, (0, 0), image.get_rect())
# Apply the alpha to our newly colourised image
pygame.surfarray.pixels_alpha(image)[...] = Block.alpha_channel
# Add to cache; eviction policy might be nice ;)
#Block.surface_cache[p_colour] = image
return image
On 31/01/2008, Stuart Axon <stu.axon+pygame@xxxxxxxxx> wrote:
> Hello,
> I'm combining a paletted surface and an alpha surface so I can
> manipulate the colours. As I'm quite new to python + pygame so
> slowness is probably down to me :)
>
> At the moment I cache the surfaces, but if it was realtime it would be
> simpler and not use shedloads of memory.
>
> Both methods seem too slow - especially the palette manipulation
> (commenting that out speeds things up quite a bit). The rest of the
> code still seems to be slower than I'd expect without the palette code
> enabled; I thought pygame 1.8 might hardware accelerate this, but it
> doesn't seem to make a difference (so I'm using 1.7.1 as 1.8 isn't
> finished).
>
> The game (in progress): (python ballbreaker.py)
> stuartaxon.com/ballbreaker/pygame.zip - shows the performance
> stuartaxon.com/ballbreaker - build with caching
>
>
> Cheers :)
>
> ---------------------------------------------------------------
> These are the relevant two methods from block.py
>
> ##
> # Return a surface to represent the block
> #
> def create_surface(self, colour):
> # Check cache
> #p_colour = utils.pack_colour(colour)
> #if p_colour in Block.surface_cache:
> # return Block.surface_cache[p_colour]
>
> # Create 8 bit surface and copy our image there
> pal_image = pygame.Surface((Block.width, Block.height), 0, 8)
> pal_image.set_palette(Block.indexed_image.get_palette())
> pal_image.blit(Block.indexed_image, (0, 0))
>
> palette = Block.indexed_image.get_palette()
>
> # Manipulate the palette
> pal_image.set_palette(self.colourise_palette(palette, colour))
>
> # Create our image palette
> image = pygame.Surface((Block.width, Block.height), pygame.SRCALPHA, 32)
> image.blit(pal_image, (0, 0), image.get_rect())
>
> # Get the alpha channel from our alpha image
> alpha_channel = pygame.surfarray.array_alpha(Block.alpha_image)
>
> # Apply the alpha to our newly colourised image
> pygame.surfarray.pixels_alpha(image)[...] = alpha_channel
>
> # Add to cache; eviction policy might be nice ;)
> # Block.surface_cache[p_colour] = image
>
> return image
>
> def colourise_palette(self, palette, colour):
> hls_colour = colorsys.rgb_to_hls(*utils.normalize_colour(colour))
>
> ##
> # Function for actually altering the colour
> def alter_colour(colour):
> h, l, s = colorsys.rgb_to_hls(*utils.normalize_colour(colour))
> h = hls_colour[0]
> l = (hls_colour[1] + l) / 2.0
> s = 0.95
> return utils.reformat_colour( colorsys.hls_to_rgb(h, l, s) )
>
> return map(alter_colour, palette)
>