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:
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;
}
Answers to questions are automatically generated and may not have been reviewed.
Explore window creation, configuration, and event handling using SDL's windowing system
Comprehensive course covering advanced concepts, and how to use them on large-scale projects.
View Course