Double Buffering

Correct Use of SDL_UpdateWindowSurface()

When should I call SDL_UpdateWindowSurface() in my application?

Abstract art representing computer programming

SDL_UpdateWindowSurface() should be called whenever you have finished rendering the current frame in your back buffer and are ready to display it. Typically, this is done at the end of your game loop after all drawing operations:

#include <SDL.h>

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* window = SDL_CreateWindow(
    "Update Example",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    640, 480, 0);

  SDL_Surface* surface = SDL_GetWindowSurface(
    window);

  while (true) {
    // Render graphics to the surface here
    SDL_FillRect(surface, NULL,
      SDL_MapRGB(surface->format,
        0x00, 0xFF, 0x00));  // Fill green

    // Update the front buffer with what has
    // been drawn on the back buffer
    SDL_UpdateWindowSurface(window);
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
This Question is from the Lesson:

Double Buffering

Learn the essentials of double buffering in C++ with practical examples and SDL2 specifics to improve your graphics projects

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Double Buffering

Learn the essentials of double buffering in C++ with practical examples and SDL2 specifics to improve your graphics projects

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved