Creating Multiple Windows with SDL2

How can I create multiple windows in an SDL2 application?

SDL2 allows you to create multiple windows within a single application. Here's an example of how to create two windows:

#include <SDL.h>

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

  SDL_Window* window1{SDL_CreateWindow(
    "Window 1",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    800, 600, SDL_WINDOW_SHOWN
  )};

  SDL_Window* window2{SDL_CreateWindow(
    "Window 2",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    400, 300, SDL_WINDOW_SHOWN
  )};

  SDL_Event event;
  bool quit = false;
  while (!quit) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        quit = true;
      }
    }
  }

  SDL_DestroyWindow(window1);
  SDL_DestroyWindow(window2);
  SDL_Quit();

  return 0;
}

In this example, we create two windows using SDL_CreateWindow, each with different titles and sizes. The windows are displayed simultaneously, and the event loop handles events for both windows.

Remember to destroy each window using SDL_DestroyWindow before quitting the application.

You can also create and manage windows dynamically during the application's runtime based on your specific requirements.

Building SDL2 from Source (GCC and Make)

This guide walks you through the process of compiling SDL2, SDL_image, and SDL_ttf libraries from source

Questions & Answers

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

SDL2: Static vs Dynamic Linking
What is the difference between static and dynamic linking when building SDL2 from source?
SDL2: Renderer vs Surface
What is the difference between an SDL_Renderer and an SDL_Surface in SDL2?
SDL2 Event Handling
How can I handle multiple types of events in SDL2, such as keyboard and mouse events?
Creating a Fullscreen Window in SDL2
How can I create a fullscreen window in SDL2?
Audio Playback with SDL2
How can I play audio files using SDL2?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant