I'm a bit puzzled about pygame's Surface.convert()
:
It's common sense that I should convert a surface after loading an image to it (presumably a jpg/png/etc file), but what about surfaces that I only use pygame's "primitives" like pygame.draw.circle()
or Surface.fill()
?
Official documentation is very vague about it: it says "fastest format for blitting. It is a good idea to convert all Surfaces before they are blitted many times".
Consider this simple ball-bouncing code:
import pygame
position = [100, 200]
velocity = [7, 17]
radius = 50
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((800, 600))
background = pygame.Surface(screen.get_size())
screen.blit(background, (0,0))
ball = pygame.Surface(2*[radius*2])
pygame.draw.circle(ball, (0, 255, 0), 2*(radius,), radius)
bounds = (screen.get_size()[0] - 2 * radius,
screen.get_size()[1] - 2 * radius)
rect = ball.get_rect()
rect.x = position[0]
rect.y = position[1]
done = False
while not done:
for event in pygame.event.get():
if event.type in [pygame.QUIT, pygame.KEYDOWN]:
done = True
for i in [0, 1]:
position[i] += velocity[i]
if position[i] < 0 or position[i] > bounds[i]:
velocity[i] *= -1
screen.blit(background, rect)
rect.x = position[0]
rect.y = position[1]
screen.blit(ball, rect)
pygame.display.update()
clock.tick(60)
pygame.quit()
It has 3 surfaces: screen
, background
and ball
. They are blitted a lot, as source or dest. Would any of these surfaces benefit from a .convert()
?
If yes, then should I simply append a .convert()
when creating any surface? Like
surface = pygame.Surface(...).convert()
A lot of pygame tutorials do that, even for a background that is just .fill()
'ed. But this feels so... redundant. Why isn't the so called "fastest pixel format" the default when creating new surfaces?
And if such surfaces would benefit from a convert, when should I do so? After a .fill()
, before, or it doesn't matter? Is once enough or should I "reconvert" after each draw? What if I load an image?
References and sources about proper .convert()
usage are highly appreciated, thanks!