Pages

Wednesday, August 27, 2014

Libgdx supporting different screensizes

I finally fixed the density problem! I should have read the docs and libgdx wiki more careful, that would have saved me 3 days! The fix was pretty easy. There was no need in making several copies of each image in different resolutions, not even setting any coordinate to relative instead of absolute. Libgdx is a game engine, a framework, where most common problems have easy intuitive solutions.

I ended up adding just a few lines of code instead ~50+ of my noob-solution code. In the create method:

        viewport = new StretchViewport(480, 800, camera);

The StretchViewport stretches viewport to screen. You can use other ones (check libgdx wiki), so that you don't stretch, but show black borders on the sides of screen. But my game is locked in portrait mode, so there will probably be almost no difference in aspect ratios on different devices. 

and the resize method:

        @Override
public void resize(int width, int height) {
viewport.update(width, height);
}

The game now looks exactly the same on 480x800 and 1080x1920 screens. Just need to add Linear filter to images so they dont look too pixelated. In my case i added the filter to my assets.pack after TexturePacker.

No comments:

Post a Comment