Displaying Windows on Different Monitors
How can I ensure windows are displayed on different monitors?
To display multiple windows on different monitors, use the SDL_WINDOWPOS_CENTERED_DISPLAY(n)
macro for each window, specifying a unique display index for n
.
Example Code
Here's an example where we create two windows on two different displays:
#include <SDL2/SDL.h>
#include <iostream>
int main() {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window1 = SDL_CreateWindow(
"Monitor 1",
SDL_WINDOWPOS_CENTERED_DISPLAY(0),
SDL_WINDOWPOS_CENTERED_DISPLAY(0),
800, 600, SDL_WINDOW_SHOWN);
SDL_Window* window2 = SDL_CreateWindow(
"Monitor 2",
SDL_WINDOWPOS_CENTERED_DISPLAY(1),
SDL_WINDOWPOS_CENTERED_DISPLAY(1),
800, 600, SDL_WINDOW_SHOWN);
if (!window1 || !window2) {
std::cerr << "Failed to create windows: "
<< SDL_GetError() << "\n";
} else {
std::cout << "Windows displayed on "
"different monitors\n";
}
SDL_Delay(3000);
SDL_DestroyWindow(window1);
SDL_DestroyWindow(window2);
SDL_Quit();
return 0;
}
Windows displayed on different monitors
Tips
- Ensure your system has multiple connected monitors.
- Handle errors when monitors are unavailable or indices are invalid.
Video Displays
Learn how to handle multiple monitors in SDL, including creating windows on specific displays.