SDL2 How to position a window on a second monitor?

后端 未结 3 1563
独厮守ぢ
独厮守ぢ 2021-02-15 16:39

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

UPDATE

Us

3条回答
  •  长情又很酷
    2021-02-15 17:10

    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.

提交回复
热议问题