Pages

Showing posts with label density. Show all posts
Showing posts with label density. Show all posts

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.

Thursday, August 21, 2014

First game prototype almost done! One problem to solve... different densities

 

So there it is. You can see a device with 480x800 resolution on the left and a device with 1080x1920 on the right. This is wrong for sure. Problem is that when making an native android application there are folders for different screen densities and the app chooses assets depending on screen densities/sizes. There is another way in libgdx to deal with it, but im not familiar with it yet. Probably you need to load several sizes of one image and switch between them depending on screen size. That is really easy to implement using the Gdx.graphics.getHeight() and choosing different images to load depending on the value returned from that method, but i better learn about the ready solution first. There are some probable pitfalls concerning image coordinates if they are of a different size. Probably this can be solved by not using actual pixel coordinates, but using instead proportions of the screen, like Gdx.graphics.getHeight()/10 etc.