10

I am using SDL_SetWindowPosition to position my window. Can I use this function to position my window on another monitor?

UPDATE

Using SDL_GetDisplayBounds will not return the correct monitor positions when the text size is changed in Windows 10. Any ideas how to fix this?

enter image description here

Z0q
  • 1,689
  • 3
  • 28
  • 57
  • 2
    Thank you so much for raising this. I was wondering why scaling was completely wrong on my side while using SDL but it turned out it was because of Windows 125% scaling. Saved me hours of debugging. – julealgon Sep 01 '20 at 01:22

3 Answers3

17

SDL2 uses a global screen space coordinate system. Each display device has its own bounds inside this coordinate space. The following example places a window on a second display device:

// enumerate displays
int displays = SDL_GetNumVideoDisplays();
assert( displays > 1 );  // assume we have secondary monitor

// get display bounds for all displays
vector< SDL_Rect > displayBounds;
for( int i = 0; i < displays; i++ ) {
    displayBounds.push_back( SDL_Rect() );
    SDL_GetDisplayBounds( i, &displayBounds.back() );
}

// window of dimensions 500 * 500 offset 100 pixels on secondary monitor
int x = displayBounds[ 1 ].x + 100;
int y = displayBounds[ 1 ].y + 100;
int w = 500;
int h = 500;

// so now x and y are on secondary display
SDL_Window * window = SDL_CreateWindow( "title", x, y, w, h, FLAGS... );

Looking at the definition of SDL_WINDOWPOS_CENTERED in SDL_video.h we see it is defined as

#define SDL_WINDOWPOS_CENTERED         SDL_WINDOWPOS_CENTERED_DISPLAY(0)

so we could also use the macro SDL_WINDOWPOS_CENTERED_DISPLAY( n ) where n is the display index.

Update for Windows 10 - DPI scaling issue

It seems like there is indeed a bug with SDL2 and changing the DPI scale in Windows (i.e. text scale).

Here are two bug reports relevant to the problem. They are both still apparently unresolved.

https://bugzilla.libsdl.org/show_bug.cgi?id=3433

https://bugzilla.libsdl.org/show_bug.cgi?id=2713

Potential Solution

I am sure that the OP could use the WIN32 api to determine the dpi scale, for scale != 100%, and then correct the bounds by that.

Jacques Nel
  • 581
  • 2
  • 11
  • I'd like to comment on the original question to answer your question, but I do not yet have the privilege. My guess is changing the **text scale** in Windows 10 affects the screen coordinate with SDL in some way. I had a different but related problem, until I figured out how to use SDL2 with my Mac's Retina display. (Retina uses scaling). I suggest you try the following experiment: Output the display bounds, and check if they change, when you change the text scale setting, under Windows 10. – Jacques Nel Jan 19 '17 at 16:34
  • It could possibly be a bug with SDL2. Have you searched for others having the same experience, on Windows 10? – Jacques Nel Jan 19 '17 at 16:36
  • If my assumption is correct, all screen positions will be multiplied by 1.25 as a result of 125% text scale. – Jacques Nel Jan 19 '17 at 16:38
  • 2
    For the Win 10 scaling issue see this great article: https://nlguillemot.wordpress.com/2016/12/11/high-dpi-rendering/ If you make your app dpi aware (by using manifest or winapi's `SetProcessDpiAwareness`) `SDL_WINDOWPOS_CENTERED_DISPLAY` will just work fine! ;) – pergy Mar 20 '17 at 11:00
2

DPI scaling issue ("will not return the correct monitor positions when the text size is changed")

It's a known issue with SDL2 (I encountered it in those versions: 2.0.6, 2.0.7, 2.0.8, probably the older versions have this issue as well).

Solutions:

1) Use manifest file and set there:

<dpiAware>True/PM</dpiAware>

(you need to include the manifest file to your app distribution)

2) Try SetProcessDPIAware().

Chris
  • 63
  • 1
  • 9
0

Yes, you can use SetWindowPosition, if you know the boundaries of the second monitor. You can use the function SDL_GetDisplayBounds(int displayIndex,SDL_Rect* rect) to get them.

J. Rocha
  • 86
  • 2