Specifying Display Index with SDL_CreateWindow()
Can I specify the display index when using SDL_CreateWindow()
?
SDL allows you to specify the display index indirectly by setting the initial window position to match the desired monitor. Use the SDL_WINDOWPOS_CENTERED_DISPLAY(n)
or SDL_WINDOWPOS_UNDEFINED_DISPLAY(n)
macros, where n
is the zero-based index of the monitor.
Example Code
Here's an example where we create a window on the display with index 1
:
#include <SDL2/SDL.h>
#include <iostream>
int main() {
SDL_Init(SDL_INIT_VIDEO);
// Creating a window on the second
// monitor (index 1)
SDL_Window* window = SDL_CreateWindow(
"Multi-Monitor Test",
SDL_WINDOWPOS_CENTERED_DISPLAY(1),
SDL_WINDOWPOS_CENTERED_DISPLAY(1),
800, 600, SDL_WINDOW_SHOWN);
if (!window) {
std::cerr << "Window creation failed: "
<< SDL_GetError() << "\n";
} else {
std::cout << "Window created on "
"specified monitor\n";
}
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Window created on specified monitor
Notes
- The
SDL_WINDOWPOS_CENTERED_DISPLAY(n)
macro positions the window in the center of monitorn
. - If
n
exceeds the available number of displays, SDL defaults to the primary display.
Video Displays
Learn how to handle multiple monitors in SDL, including creating windows on specific displays.