Search code examples
javalibgdxlwjgl

LibGDX - find out if a given screen resolution is supported on the current device


So I'm writing some tools for my program to deal with basic configurations, reading settings from a data file, and then making those settings the active configuration. I'm also building in an error checking mechanism to make sure that settings are formatted correctly and have valid values.

I want to check to see what the current devices supported resolutions are, and I want to compare that to the resolution specified in the data file. Is there an easy way to do this in libGDX that I'm missing? I know that LWJGL has a function that will give you an array of the supported resolutions, but I don't want to reinvent the wheel.

I'm looking for something like: boolean isValidResolutionForDevice(int width, int height)

Am I going to have to write this myself? Or does this exist already? It seems to me to be such a useful function that it must have been written into the libraries somewhere.


Solution

  • Unfortunately, there is no ready solution in libgdx, I solved it like this:

        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);
            }
        }
    }