2016-10-01 129 views
2

使用SDL2,我设法使用SDL_GetCurrentDisplayMode()SDL_GetDisplayBounds()获得我的显示器的分辨率和位置,但是当我在外部更改分辨率(在这种情况下,使用Windows 7控制面板)或显示器的相应位置并再次调用这两个函数,我会得到相同的旧值,而不是新的分辨率和位置。直到我重新启动我的程序当然。获取SDL2中更新的屏幕分辨率

我想SDL不更新这些。如何在不重新启动程序的情况下获取更新值需要做什么?

回答

0

AFAIK SDL无法获得更新的分辨率(任何人如果我错了,请纠正我)。

你可以通过这种方式来做到这一点,就是使用你的操作系统的API。在你的情况下,你说你正在使用Windows。所以你可以继续使用Windows API来检索更新的分辨率信息。这显然不能移植到其他操作系统 - 所以你必须为你想要支持的每一个操作系统做到这一点。

我在答案的底部添加了一个小例子,它展示了如何在C++中检索主显示器的分辨率。如果你想更详细地处理多个显示器及其相对位置等,你应该看看this question

#include "wtypes.h" 
#include <SDL.h> 
#include <iostream> 
using namespace std; 

void GetDesktopResolution(int& w, int& h) 
{ 
    RECT r; 
    GetWindowRect(GetDesktopWindow(), &r); 
    w = r.right; 
    h = r.bottom; 
} 


int main(int argc, char *argv[]) 
{ 
    SDL_Init(SDL_INIT_EVERYTHING); 
    SDL_Window* window = SDL_CreateWindow("SDL", 0, 0, 640, 480, SDL_WINDOW_RESIZABLE); 

    bool running = true; 
    while(running) { 
     SDL_Event game_event; 
     if(SDL_PollEvent(&game_event)) { 
     switch(game_event.type) { 
      case SDL_QUIT: 
       running = false; 
       break; 
     } 
     } 

     SDL_DisplayMode current; 
     SDL_GetCurrentDisplayMode(0, &current); 
     cout << "SDL" << current.w << "," << current.h << '\n'; 

     int w, h; 
     GetDesktopResolution(w, h); 
     cout << "winapi" << w << "," << h << '\n'; 
    } 

    SDL_DestroyWindow(window); 
    SDL_Quit(); 

    return 0; 
}