Window Configuration

Multiple Windows with Same Title

What happens if I create multiple windows with the same title? Will this cause any problems?

Abstract art representing computer programming

Creating multiple windows with the same title is perfectly valid in SDL and won't cause any technical problems. Each window is a distinct object with its own memory and resources, regardless of its title.

Let's see this in action:

#include <SDL.h>

int main() {
  SDL_Init(SDL_INIT_VIDEO);

  // Create three windows with the same title
  SDL_Window* WindowA{SDL_CreateWindow(
    "Same Title",
    100, 100, 400, 300,
    SDL_WINDOW_SHOWN
  )};

  SDL_Window* WindowB{SDL_CreateWindow(
    "Same Title",
    200, 200, 400, 300,
    SDL_WINDOW_SHOWN
  )};

  SDL_Window* WindowC{SDL_CreateWindow(
    "Same Title",
    300, 300, 400, 300,
    SDL_WINDOW_SHOWN
  )};

  // We can work with each window individually
  SDL_SetWindowPosition(WindowB, 500, 200);

  // Always clean up
  SDL_DestroyWindow(WindowA);
  SDL_DestroyWindow(WindowB);
  SDL_DestroyWindow(WindowC);

  SDL_Quit();
  return 0;
}

However, there are some practical considerations to keep in mind:

User Experience

  • Users might have trouble distinguishing between windows
  • Most operating systems will add a number or identifier to duplicate titles
  • It might be confusing when Alt-Tabbing between windows

Window Management

  • When handling window events, you'll need to check the window ID to know which window triggered the event
  • It's generally better to give windows unique, descriptive titles that help users understand their purpose

Here's how to handle events for multiple windows:

#include <SDL.h>
#include <iostream>

int main() {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* WindowA{
    SDL_CreateWindow(
      "Same Title",
      100, 100, 400, 300,
      SDL_WINDOW_SHOWN
    )};

  SDL_Window* WindowB{
    SDL_CreateWindow(
      "Same Title",
      200, 200, 400, 300,
      SDL_WINDOW_SHOWN
    )};

  SDL_Event E;
  bool Running{true};

  while (Running) {
    while (SDL_PollEvent(&E)) {
      if (E.type == SDL_WINDOWEVENT) {
        // Check which window triggered the event
        if (E.window.windowID ==
          SDL_GetWindowID(WindowA)) {
          std::cout << "Event on Window A\n";
        } else if (E.window.windowID ==
          SDL_GetWindowID(WindowB)) {
          std::cout << "Event on Window B\n";
        }
      }
    }
  }

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

Window Configuration

Explore window creation, configuration, and event handling using SDL's windowing system

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

This Question is from the Lesson:

Window Configuration

Explore window creation, configuration, and event handling using SDL's windowing system

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:

  • 62 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