SDL_GetWindowDisplayIndex()
for Multiple Displays
How does SDL_GetWindowDisplayIndex()
work if the window spans multiple displays?
When a window spans multiple displays, SDL_GetWindowDisplayIndex()
returns the index of the display that contains the largest portion of the window. SDL uses internal calculations based on window bounds to determine this.
Example Code
Here's an example where we create a large window that might span multiple displays:
#include <SDL2/SDL.h>
#include <iostream>
int main() {
SDL_Init(SDL_INIT_VIDEO);
// A window that may span multiple displays
SDL_Window* window = SDL_CreateWindow(
"Multi-Display Test",
SDL_WINDOWPOS_CENTERED_DISPLAY(0),
SDL_WINDOWPOS_CENTERED_DISPLAY(0),
1920, 1080, SDL_WINDOW_SHOWN);
int displayIndex =
SDL_GetWindowDisplayIndex(window);
std::cout << "Window is primarily on display: "
<< displayIndex << "\n";
SDL_Delay(3000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Window is primarily on display: 0
Important Notes
- The returned display index may change if the window is resized or moved.
- Use
SDL_GetDisplayBounds()
for additional precision when handling multi-display setups.
Video Displays
Learn how to handle multiple monitors in SDL, including creating windows on specific displays.