9

I saw online the use of HWSURFACE|DOUBLEBUF|RESIZABLE to resize the window.

It works but I'm not sure what the HWSURFACE and DOUBLEBUF actually do.

I know it stands for hardware surface and double buffer, but what they actually help with I have no idea.

skrx
  • 19,980
  • 5
  • 34
  • 48
MicChe
  • 101
  • 2
  • 10

2 Answers2

17

I'm disappointed that the pygame docs (e.g. http://www.pygame.org/docs/ref/display.html) don't seem to explain this as I would have thought they should have.

Double-buffering, as the description for the tag mentions, is using a separate block of memory to apply all the draw routines and then copying that block (buffer) to video memory as a single operation. Failure to do this can lead to graphical artifacts. A simple example could be flickering of the scene caused by part of background being drawn right before the video refreshes and then other parts afterwards (so they aren't shown until the next refresh).

Hardware surface refers to using memory on the video card ("hardware") for storing draws as opposed to main memory ("software"). The main reason for this is that the bandwidth between main memory and video memory tends to be slow and so being able to draw directly can speed this up. SDL (which PyGame is built on top of) was originally developed to support older video cards which didn't support hardware surfaces and so you have to request extra features to see if your hardware can take advantage of them. At this point in time, my understanding is that pretty much any video card (even on board ones on low end laptops made within the last couple of years (or even things like the Raspberry Pi but I'd need to check it) should support DOUBLEBUF and HWSURFACE. You may want to check the SDL documentation to see if that provides any additional details you need

Hope that helps

Foon
  • 6,148
  • 11
  • 40
  • 42
0

From pygame docs pygame.display.flip():

"if your display mode is using the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this will wait for a vertical retrace and swap the surfaces."

This means that surfaces and sprites are drawn, ie. copied or blited, to another area of video memory that is not currently being displayed. At the vertical retrace, ie. when the previous frame was fully drawn, and the next one did not started to be drawn yet, the pointer to the address of the area of video memory to display next is changed to the area where sprites were drawn, the double buffer. This has a swap screen effect and is much faster than copying the whole image. This also avoids screen tearing, ie. part of the previous frame to be drawn with part of the next frame, since the swapping to the double buffer is made when the hardware is not updating the display.

Some times, when using a fixed surface as background, pygame.display.update() can be faster, since only the areas of the sprites that moved between frames are updated.

Helder Daniel
  • 331
  • 3
  • 8