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 <[EMAIL PROTECTED]> 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)
>