Creating a Window

Using Multiple Windows in SDL

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

Abstract art representing computer programming

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.

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

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 27 Lessons
  • 100+ Code Samples
  • 91% 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