I am writing some kind of image viewer in which the user can use a camera to pan/zoom on the images.
The user needs to be able to zoom a lot to see details on high resolution images.
When the images are small relative to the size of the application window, I get about 300 fps, and the smaller the images, the higher the fps. However, when I zoom in so that an image almost fills the application window, the fps drop to less than 10fps ...
My images are quite high resolution PNGs (around 2000x1000 for the most part), but I tried with smaller images and I get the same performance issues
I have already searched quite a lot for solutions to this problem, but I could not find anything useful.
I am already using the "convert()
" function (more precisely the "convert_alpha()
" function)
I use pygame.transform.scale
to rescale the images to simulate a camera zooming. Here is my set_scale()
function:
def set_scale(self, factor):
self.original_rect_size *= factor
self.rect.size = self.original_rect_size
self.image = pygame.transform.scale(self.original_im, Vector2(self.original_rect_size.x*factor, self.original_rect_size.y*factor))
self.rect.size = Vector2(self.original_rect_size.x*factor, self.original_rect_size.y*factor)
I use pygame.sprite.LayeredUpdates()
to group and draw all the images
Is there any way to improve performance ? I may be doing something wrong as I am new to pygame.
Thank you very much !
CodePudding user response:
pygame.transform.scale
is a very expensive operation. Scale the image only when the scale factor changes:
class YouClass:
def __init__(self):
# [...]
self.factor = 1
def set_scale(self, factor):
if factor != self.factor:
self.factor = factor
self.original_rect_size *= factor
self.rect.size = self.original_rect_size
self.image = pygame.transform.scale(self.original_im, Vector2(self.original_rect_size.x*factor, self.original_rect_size.y*factor))
self.rect.size = Vector2(self.original_rect_size.x*factor, self.original_rect_size.y*factor)