[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Drawing/Surface performance
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Drawing/Surface performance
- From: "Stuart Axon" <stu.axon+pygame@xxxxxxxxx>
- Date: Thu, 31 Jan 2008 23:40:57 +0000
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Thu, 31 Jan 2008 18:41:07 -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:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; bh=LAZmJnCzbozMIYjs2K0xGt9aawuVPTpEpHTa7YjjULA=; b=QDAeOmlyzs4xWDbS1NJLnFC8L6+sQ9OjZRkDBYTUqWAzx47L31LioZplEpqyLlFbzaq6A70kyVx6vozCuhyKd3QP14dFJwe0Fi0yfIaM1Mgh1QzmDTeP1WSUFR/firkqj4hXxMSm166OEAAuZaEmur8CLYpjFKC7zIVV8jSx25s=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=WoFSjz5SDyByb3BP1S4S37LyVZsiIvxpCHladrYRHrslYIGS26RDZUbTRf7V17oQ+HO9uJ6X/ldLzYnLZs8HVSdUnc6DTN/RREzumf7lqtpc2tyx3ZFFE35QlT8GkfpUeI6WfImPTyVVB6lmwFxuvhm2g/0n0A0l6Ag/g2Qkm6c=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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)