LibGDX - how do I get my game to work at all screen resolutions?

I am currently using the Spelling Camera. Before I go into the game, I first want to fix the screen size. I currently have a screen size of 800 x 480. Will this work well on other devices and screen sizes? Right now I have a splash screen and a game screen. What lines of code should I add to achieve this.

+3


source to share


2 answers


Having static width on your camera is perfectly normal, as it gives your game a single, fixed dimension that you can work with reliably. This works because the camera is used to determine the coordinates of the world, which are not always the same as the coordinates of the screen.

Your concerns come from a fixed camera height. Having a fixed height will cause the screen to stretch higher or shorter depending on the aspect ratio of the device's screen. If you want to account for different aspect ratios, you need to increase your camera's height (currently 480) by the display factor. You can get the screen ratio by dividing the height by the width by the height. It will look something like this:

float width = Gdx.graphics.getWidth();
float height = Gdx.graphics.getHeight();

OrthographicCamera camera = new OrthographicCamera(800, 480 * (height / width));

      



As BennX pointed out in the comments, LibGDX introduced viewports that allow you to do the same thing above, just in a different way. To achieve the same effect as I outlined above using only the viewport, you should use ExtendViewport

. What it does is to keep the size of the world in one direction while stretching it in the other direction. Thus, the world will first scale to fill the screen, then the shorter dimension expands while maintaining proportions. To create this kind of viewport, it looks something like this:

OrthographicCamera camera = new OrthographicCamera(800, 480);
ExtendViewport viewport = new ExtendViewport(800, 480, camera);

      

The top view will have a minimum width of 800 and a minimum height of 480. One of these values ​​will be the same after applying the viewport, and the other will change depending on the aspect ratio of the screen. Narrower screens will have more vertical space, while wider screens will have more horizontal space. Read more about viewports on the LibGDX wiki page here .

+6


source


I use the approach below and it works for almost all screen sizes with a minor minor scaling issue.

I always use graphics for screen size 1920.0x1080.0

ScreenViewport screenViewport=new ScreenViewport(camera);
screenViewport.setUnitsPerPixel(Math.min(1920.0f/Gdx.graphics.getWidth(),1080.0f/Gdx.graphics.getHeight()));

      

Here you can set your approach from Math.min () or Math.max ().



This will cause your camera's port size to be close to 1920.0 * 1080.0

Device screen-size Math.max()      Math.max() 
800.0x480.0        1800.0x1080.0   1920.0x1152.0
1920.0x1080.0      1920.0x1080.0   1920.0x1080.0
2048.0x1440.0      1536.0x1080.0   1920.0x1350.0

      

Note. Always use camera.viewportWidth and camera.viewportHeight for given game UI screen positions.

0


source







All Articles