Using Multiple Windows in SDL

Can I create and manage multiple windows in SDL, and if so, how?

Yes, SDL supports managing multiple windows. Each window in SDL is managed through a separate SDL_Window pointer. You can create multiple instances of your Window class or manage multiple SDL_Window pointers directly in your main application. Here's a simple example of managing two windows:

#include <SDL.h>

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* window1 = SDL_CreateWindow(
    "Window 1", 100, 100, 640, 480, 0);
  SDL_Window* window2 = SDL_CreateWindow(
    "Window 2", 750, 100, 640, 480, 0);

  bool running = true;
  SDL_Event event;

  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      }
    }

    SDL_UpdateWindowSurface(window1);
    SDL_UpdateWindowSurface(window2);
  }

  SDL_DestroyWindow(window1);
  SDL_DestroyWindow(window2);
  SDL_Quit();
  return 0;
}

This example initializes two separate windows and updates their surfaces in the main loop. When the SDL_QUIT event is detected, both windows close.

Creating a Window

Learn how to create and customize windows, covering initialization, window management, and rendering

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Purpose of SDL_Quit() in SDL2
Why do we need to call SDL_Quit()? What happens if we forget?
Understanding Screen Coordinates and Pixels in SDL2
What exactly is a "screen coordinate"? Is it always the same as a pixel?
How Window Position is Determined when using SDL_WINDOWPOS_UNDEFINED
How does does SDL decide where to actually place the window when using SDL_WINDOWPOS_UNDEFINED?
Understanding SDL_PumpEvents() and the Event Loop
What does SDL_PumpEvents() actually do? Why is it in an infinite loop?
Handling the Window Close Event in SDL2
How do I make the window close properly when I click the 'X' button?
The Rule of Three/Five and SDL Resource Management
What is the "Rule of Three" and why did deleting the copy operations satisfy it here? What about the Rule of Five?
Using Raw vs. Smart Pointers for SDL Resources
Is SDL_Window* a raw pointer? Should I be using smart pointers like std::unique_ptr?
Purpose of argc and argv in SDL Applications
What are argc/argv in main for? Do I need them here?
How to Handle Window Close Events in SDL
How can I make my SDL window respond to close events, such as clicking the close button?
Adjusting Window Size Dynamically in SDL
How can I adjust the size of an SDL window dynamically during runtime?
Improving SDL Window Performance
What are some tips to improve the performance of an SDL window?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant