Building SDL2 from Source (GCC and Make)

Creating Multiple Windows with SDL2

How can I create multiple windows in an SDL2 application?

Vector illustration representing computer programming

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.

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