Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame - More efficient to blit a sprite from spritesheet, or blit invidual sprites to their own surfaces?

Tags:

python

pygame

Using pygame, would it be more efficient to:

  1. Blit a single sprite from a portion of a spritesheet each frame
  2. At startup, blit each sprite from a spritesheet to their own Surfaces, then blit those Surfaces

Is there any performance differences between the two? Would it take more draw calls to use the first method, versus individual Surfaces at startup (i.e. does doing this at startup store copies of those pixels in ram/vram)?

Thanks

like image 454
Firedan1176 Avatar asked Dec 01 '25 05:12

Firedan1176


1 Answers

It would be faster to blit the images into a surface, then blit those surfaces.

However it would consume more memory, since you'll need to keep those surfaces somewhere in memory.

Why?

When you go to blit the image from your spritesheet, you'll end up subsurfing/clipping the spritesheet surface, which will mean you'll need to generate another surface on the spot. However, this process won't take long.

The performance benefit most likely isn't worth it, so I'd recommend just go with whichever method you're most comfortable with. If you're concerned about performance, check out the builtin CProfile python module.

VRAM never comes into this equation. pygame.Surface is derived from SDL_Surface from the SDL library for the C programming language. SDL_Surface is primarily targeted toward software rendering, which means the surface pixels are stored in standard RAM.

like image 54
Coolq B Avatar answered Dec 03 '25 20:12

Coolq B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!