LibGDX - 查明当前设备是否支持给定的屏幕分辨率

时间:2014-05-11 02:08:59

标签: java libgdx lwjgl

所以我正在为我的程序编写一些工具来处理基本配置,从数据文件中读取设置,然后将这些设置作为活动配置。我还在构建一个错误检查机制,以确保设置格式正确并具有有效值。

我想查看当前设备支持的分辨率是什么,我想将其与数据文件中指定的分辨率进行比较。在libGDX中有一种简单的方法可以解决这个问题吗?我知道LWJGL有一个功能可以为你提供一系列支持的分辨率,但我不想重新发明轮子。

我正在寻找类似的东西: boolean isValidResolutionForDevice(int width,int height)

我自己要写这个吗?或者这已经存在了吗?在我看来,它是一个非常有用的功能,它必须已经写入某个库中。

2 个答案:

答案 0 :(得分:2)

实际上有一种方法可以做到这一点。主com.badlogic.gdx.Application接口具有getGraphics()方法,并且从其返回的com.badlogic.gdx.Graphics类具有getDisplayModes()方法,该方法返回DisplayMode[]DisplayMode基本上看起来像这样(删除了注释和构造函数):

public class DisplayMode {
    /** the width in physical pixels **/
    public final int width;
    /** the height in physical pixles **/
    public final int height;
    /** the refresh rate in Hertz **/
    public final int refreshRate;
    /** the number of bits per pixel, may exclude alpha **/
    public final int bitsPerPixel;
}

然后你可以浏览这些以查看是否支持所讨论的分辨率。

唯一令人讨厌的新闻是因为getGraphics()位于Application对象上,看起来你可以查询可用的图形模式,直到之后 Application对象(例如LwjglApplication)已创建。所以也许你选择一个任意分辨率(例如1024x768),开始游戏,如果原来没有实际支持,可以立即切换到支持的分辨率(并且可选择让用户在设置菜单中选择)。

我遇到了我认为你指的方法,org.lwjgl.opengl.Display.getAvailableDisplayModes(),而且......老实说,我只是将这个用于你的用例。你只需要用一个简单的条件内部迭代它就可以了。不完全重塑任何东西。

答案 1 :(得分:1)

不幸的是,在libgdx中没有现成的解决方案,我解决了这个问题:

    private Map<Integer, Integer> supportedReolutions;
    private String graphicFolder;

    supportedReolutions = new HashMap<Integer, Integer>();
    supportedReolutions.put(1024, 768);
    supportedReolutions.put(1080, 1920);
    supportedReolutions.put(1200, 1920);
    supportedReolutions.put(2048, 1536);
    supportedReolutions.put(480, 800);
    supportedReolutions.put(640, 1136);
    graphicFolder = "1080x1920";

/**
 * Chose the folder with best graphic for current device
 */
private void setCurrentResolutionFolder() {
    int width = Gdx.graphics.getWidth();
    int height = Gdx.graphics.getHeight();
    if (supportedReolutions.containsKey(width)) {
        if (supportedReolutions.get(width) == height) {
            graphicFolder = String.valueOf(width) + "x"
                    + String.valueOf(height);
        }
    }
}
相关问题