B
B
bulatzaripov2012-02-18 20:40:56
libgdx
bulatzaripov, 2012-02-18 20:40:56

libgdx + android + Multiple Screens

Hello!

Perhaps there are people on Habré who use libgdx for android development.

Please share your experience, how exactly did you solve the problem of supporting various screen sizes, resolutions, dpi when using ligdx?

Which method do you prefer? Share a description, ideas, links, or more.

The android system itself is able to "select" the necessary resources from drawable-hdpi / drawable-ldpi / and so on.
But when using libgdx, this useful property is left aside, alas.
In addition, libgdx "looks" for files in assets/, which is how it's designed from the start.

Another question: is it possible to first get the desired file from drawable/ (so that the system itself chooses the preferred one) and only after that “feed” it to libgdx?

I will be glad to any answers.

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
ertaquo, 2012-02-18
@ertaquo

In libgdx, in a good way, you need to use AssetManager to load resources, assigning loaders for different types of resources to it:

AssetManager manager = new AssetManager();
manager.setLoader(Texture.class, new TextureLoader( new InternalFileHandleResolver() ));

That is, in this example, a TextureLoader loader is created for the Texture class, which will receive a FileHandle through the InternalFileHandleResolver. However, in addition to InternalFileHandleResolver, there are several more similar classes for obtaining FileHandle, one of which is ResolutionFileResolver:
Resolution[] resolutions = { new Resolution(320, 480, ".320480"),
   		new Resolution(480, 800, ".480800"),
   		new Resolution(480, 856, ".480854") };
ResolutionFileResolver resolver = new ResolutionFileResolver(new InternalFileHandleResolver(), resolutions);

That is, the screen dimensions and the suffix for the file name are specified. As for the suffix, to be honest, I won’t even say whether it should be before the expansion or after, but most likely before.
An example (where I pulled these pieces of code from) can be found here: http://libgdx.googlecode.com/svn/trunk/tests/gdx-tests/src/com/badlogic/gdx/tests/AssetManagerTest.java .

A
Andrey Apanasik, 2012-12-09
@Suvitruf

Work with relative coordinates, and when outputting already with absolute ones.
Determine the screen size in relative coordinates.

float CAMERA_WIDTH = 12f;
 float CAMERA_HEIGHT = 10f;
CAMERA_WIDTH =  CAMERA_HEIGHT* Gdx.graphics.getWidth()/Gdx.graphics.getHeight();

Calculate coefficients.
ppuX = (float)Gdx.graphics.getWidth() / CAMERA_WIDTH;
ppuY = (float)Gdx.graphics.getHeight() / CAMERA_HEIGHT;

Everything. Now, when rendering, the relative coordinates are multiplied by the coefficient. True, the camera area will be different on different screens, but objects will not be stretched.

E
ertaquo, 2012-02-18
@ertaquo

If you rigidly set the dimensions in pixels, then yes, the same display on different screens will not work. Therefore, as an option, you can rely on Gdx.graphics.getDensity(), changing the dimensions depending on this value. However, this approach seems to me not entirely correct, but I don’t know how to do it right.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question