Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

There actually is a way to do this. The main com.badlogic.gdx.Application interface has a getGraphics() method, and the com.badlogic.gdx.Graphics class returned from that has getDisplayModes() method, which returns DisplayMode[]. DisplayMode basically looks like this (comments and constructor removed):

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

And then you can scan through these to see if the resolution in question is supported or not.

The only bit of annoying news is because getGraphics() is on the Application object, it doesn't seem like you can query for the available graphics modes until after the Application object (e.g. LwjglApplication) has been created. So perhaps you pick an arbitrary resolution (e.g. 1024x768), start the game, then immediately switch to a supported resolution if the original wasn't actually supported (and optionally let the user pick in a settings menu).


I came across the method I believe you're referring to, org.lwjgl.opengl.Display.getAvailableDisplayModes(), and...honestly I'd just use this one for your use case. You'd pretty much just be iterating over it with a simple conditional inside. Not exactly reinventing anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top