[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Alpha Channel Speed Question
On 22 Nov 2006 at 21:15, Kamilche wrote:
> I have a question - Do you know of a way to get method 3 as outlined
> below to be faster? The necessity of doing all those extra blits to
> preserve the alpha channel is really slowing things down.
>
>
>
>
> import pygame
>
[snip some code]
> def Run():
> bgcolor1 = (255, 255, 255)
> bgcolor2 = (192, 192, 192)
> bgcolor3 = (128, 128, 128)
> nightcolor = [0, 0, 0, 64]
>
> # Create a bitmap - assume this is an expensive operation
> # taking a few seconds.
> bitmap = pygame.Surface((100, 100), pygame.SRCALPHA,
> 32).convert_alpha()
> pygame.draw.circle(bitmap, (0, 255, 255), [50, 50], 50, 0)
> bitmappos = [0, 0]
>
> # Create the nighttime overlay
> night = pygame.Surface((WIDTH, HEIGHT/3), pygame.SRCALPHA,
> 32).convert_alpha()
> night.fill(nightcolor)
>
> # Loop forever
> quitgame = 0
> while not quitgame:
> pygame.event.pump()
[snip]
> # Look for quit
> for e in pygame.event.get():
> if e.type in [pygame.QUIT, pygame.MOUSEBUTTONDOWN]:
> quitgame = 1
> break
> elif e.type == pygame.KEYDOWN:
> if e.key == 27:
> quitgame = 1
> break
> elif e.type == pygame.MOUSEMOTION:
> bitmappos = e.pos
> nightcolor[3] = e.pos[0]/float(WIDTH) * 255
> night.fill(nightcolor)
>
Maybe, using colorkey and Surface alpha. But I have a question too.
Do you prepare all your bitmaps (circle) at game startup? More
specifically, would it be expensive to prepare a black and white
mask, to be used as the shader (night), for each bitmap? Even if it
is you can remove excess processing by declaring night as:
pygame.Surface((WIDTH, HEIGHT/3), 0, 32)
and adjust its surface alpha using set_alpha().
Lenard Lindstrom
<len-l@xxxxxxxxx>