C - How Is Double Buffering Implemented in SDL2 - Stack Overflow

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

How is double buffering implemented in SDL2?

Asked 2 years, 11 months ago Modified 2 years, 11 months ago Viewed 493 times

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.
0
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!

c sdl-2 double-buffering

Share Improve this question Follow asked May 1, 2021 at 12:36


Ruperrrt
509 2 14

If you care about how it's implemented, why not just read the sources? – HolyBlackCat May 1, 2021 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 1, 2021 at 13:23

@Ruperrrt platform-dependant; github.com/libsdl-org/SDL/blob/… , github.com/libsdl-org/SDL/blob/… , ... – keltar


May 1, 2021 at 13:38

Related questions

6 C++ and SDL: How does SDL_Rect work exactly?

6 SDL triple buffering

14 Is double buffering needed any more

0 What is the precise timing and effect of SDL_UpdateRect on a double-buffered screen?

1 SDL Double buffering on HW_SURFACE

1 Double buffering on lpc1788

4 SDL2 - How to render with one buffer instead of two?

0 How to do double buffering in C++ Builder 2010?

6 How can I do double buffering in SDL 2.0?

0 Double buffering in Direct2D?

Show fewer related questions

You might also like