如何使用SDL2获得屏幕分辨率? [英] How can I get the screen resolution using SDL2?

查看:958
本文介绍了如何使用SDL2获得屏幕分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用像素完美的源图像和SDL2来制作一个程序。当设置为全屏时,我希望它使用本机分辨率(SDL_WINDOW_FULLSCREEN_DESKTOP标志),但只在某个最小分辨率和合理的最大分辨率之间,之后它将从最大的法律分辨率。问题是我只能在SDL2文档中找到程序自己的窗口或程序大小的引用。

I'm using pixel-perfect source images and SDL2 to make a program. When set to fullscreen, I'd like it to use the native resolution (the SDL_WINDOW_FULLSCREEN_DESKTOP flag) but only between a certain minimum resolution and a reasonable maximum one, after which it will stretch from the largest legal resolution. The problem is that I can only find references to the program's own window or program size on the SDL2 documentation.

或者,我应该使用SDL_WINDOW_SULLCREEN_DESKTOP,然后SDL_GetWindowSize()和调整大小的屏幕宽度和高度窗口了吗?这甚至可以按预期工作吗?我想要一个更优雅的解决方案。

Or I should do something like using SDL_WINDOW_SULLCREEN_DESKTOP and then SDL_GetWindowSize() and resizing the window again? Would this even work as expected? I'd like to fins a more elegant solution than that. It feels dirty.

推荐答案

在SDL 2 wiki中,有一个名为显示和窗口管理

In the SDL 2 wiki, you have a category named Display and Window Management. It lists everything you need to know about SDL 2's management of displays (screens) and windows.

你有多个选择,最通用的将是使用 SDL_GetCurrentDisplayMode SDL_GetDesktopDisplayMode 。差异在wiki中解释:

You have multiple choices, the most generic would be using SDL_GetCurrentDisplayMode or SDL_GetDesktopDisplayMode. The difference is explained in the wiki :


当SDL运行全屏并更改分辨率时,SDL_GetDesktopDisplayMode()和SDL_GetCurrentDisplayMode 。在这种情况下,SDL_GetDesktopDisplayMode()将返回上一个本机显示模式,而不是当前的显示模式。

There's a difference between SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() when SDL runs fullscreen and has changed the resolution. In that case SDL_GetDesktopDisplayMode() will return the previous native display mode, and not the current display mode.

=https://wiki.libsdl.org/SDL_DisplayMode> SDL_DisplayMode ,您可以检索其中的属性 w h

After setting a SDL_DisplayMode with one of these, you can retrieve its attributes w and h.

但是,还有一个更合适的功能,直接: SDL_GetDisplayBounds 。如果我没有误,它给你显示相对于整个显示器可以在计算机上活动的坐标,以及显示器的大小。

However, there is another function that might be more appropriate and straightforward : SDL_GetDisplayBounds. If I am not mistaken, it gives you the coordinates of the display relative to the whole set of displays that can be active on the computer, and also the size of the display.

这两种方法都需要你知道你想知道的显示的索引。我没有玩这个SDL 2部分,但我想你可以使用 SDL_GetNumVideoDisplays 获取显示数量(并检查是否至少有一个? - 我认为如果没有显示, SDL_Window 部分可能不工作)并选择一个。或者你可以选择第一个索引为0的索引。

Both methods need you to know the index of the display you want to know about. I have not played that much with this part of SDL 2, but I guess you can use SDL_GetNumVideoDisplays to get the number of displays (and check if there is at least one ? - I think the SDL_Window part might not work if there is no display available anyway) and choose one. Or you could pick the first one, which has index 0.

哦,你可以看一下 SDL_GetCurrentDisplayMode 的页面,它们有效地检索显示器的大小。

Oh, and you can look at the exemple on the page of SDL_GetCurrentDisplayMode, they effectively retreive the size of a display.

这篇关于如何使用SDL2获得屏幕分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆