Search code examples
pythonpygamescalingframe-rate

Pygame (Python) Scale Transform Slow


I'm writing a simple program in python which takes in data over the serial port and updates the screen.

Because I want this program to look the same on whatever computer it runs on, and it needs to be fullscreen, I had the idea that I wanted to draw everything in a small 640, 480 window, and then scale it to a fullscreen window every time I update the frame.

This allows me to keep all the offsets the same for text, etc. It also turns out this is really slow.

Here's about what the important part of the code looks like:

window = pygame.display.set_mode((1920, 1080),pygame.FULLSCREEN)    
screenPrescaled=pygame.Surface((640,480))

clock=pygame.time.Clock()


while iterations<400:
#Blit all the stuff to the prescaled surface here
    screenPostscaled=pygame.transform.scale(screenPrescaled,(1920, 1080))
    window.blit(screenPostscaled,(0,0))
    pygame.display.flip() 
    iterations+=1
    clock.tick(40)

This runs a WHOLE lot slower than 40fps.

Everything on the screen is either text or lines, there are no images loaded. I suspect I'm doing something stupid.

I know I can update "dirty rectangles" only, but I wonder if I'm missing something more fundamental.

Thanks in advance!


Solution

  • You can save one blit by using window as destination surface:

    pygame.transform.scale(screenPrescaled, (1920, 1080), window)
    

    If it continues being too slow, you should use update rectangles, you can scale them using the same factor as you scale the image 1920/640 and 1080/480.