0

While trying to learn SDL2, I read about double buffering. I know that SDL uses double buffering on its own. However, I am confused about how double buffering could be implemented in software rendering.

First, you have to create a window:

SDL_Window *window = SDL_CreateWindow("SDL2 Window",
                                      SDL_WINDOWPOS_CENTERED,
                                      SDL_WINDOWPOS_CENTERED,
                                      680, 480,
                                      0);

Then you get a pointer to the window's surface ONCE:

SDL_Surface *window_surface = SDL_GetWindowSurface(window);

You can now use this pointer to blit something to the surface and display it by updating the window's surface:

 SDL_BlitSurface(image, NULL, window_surface, NULL);
 SDL_UpdateWindowSurface(window);

I know that UpdateWindowSurface() is equivalent to SDL1.2's Flip(). This implies that the buffer swap is performed here. Theoretically, the buffer that was the window's back buffer (the one pointed to by window_surface) should now be the front buffer, and you should need another pointer to the old front buffer, which now is the new back buffer - at least in my mind. But you don't. You can blit and update like above again to display a possibly changed frame. How is that possible?

Here (Where can I find the definition of 'SDL_Window') you can see that the struct SDL_Window also has only ONE member SDL_Surface* surface. There is no "back surface" and "front surface". A surface neither has two buffers that could act as front and back buffer. So how does double buffering work here? Is UpdateWindowSurface() not a flip anymore? Is the WindowSurface just copied to a buffer in graphics output by the graphics card that implements double buffering? What am I missing? I'd appreciate any help a lot!

Ruperrrt
  • 489
  • 2
  • 13
  • If you care about how it's implemented, why not just read the sources? – HolyBlackCat May 01 '21 at 12:46
  • @HolyBlackCat Honestly, I didn't consider this up until now. I just caught up on it, and it was a bit overwhelming as I'm fairly new to programming. UpdateWindowSurface() is calling UpdateWindowSurfaceRects(), which calls UpdateWindowFrameBuffer(), which is a function pointer in the source file I'm reading right now. I cannot find the implementation of UpdateWindowFrameBuffer(). It would be sufficient for me to know how double buffering gets basically realised here rather than know every detail of the implementation that would overwhelm me most likely anyway. – Ruperrrt May 01 '21 at 13:23
  • @Ruperrrt platform-dependant; https://github.com/libsdl-org/SDL/blob/c59d4dcd38c382a1e9b69b053756f1139a861574/src/video/x11/SDL_x11framebuffer.c#L142 , https://github.com/libsdl-org/SDL/blob/c59d4dcd38c382a1e9b69b053756f1139a861574/src/video/windows/SDL_windowsframebuffer.c#L99 , ... – keltar May 01 '21 at 13:38

0 Answers0