3

I am using libgdx to work on a game but just came across that how will I implement it for various screen sizes? I figured out how to position images for different sizes and resolutions but how do we make sprites support different screen sizes ? My background on 320x480 goes fine but takes a very small place on 480 by 800, how to accomplish this that it works on all the screens?

4 Answers4

5

You have various options depending on what you are happy to do,

a. You could use a set of HQ sprites scaled down to fit in each of the screens something like;

in resize()

width = arg0;
height = arg1;

then in your render()

batch.draw(textureRegion, -width/2, -height/2, width, height);

will draw a sprite across the whole screen (assuming orthographic camera centered at 0,0)

b. You could use different sets of sprites for different resolutions you would then load a set sprites based on the dimensions of the viewport.

Neilos
  • 2,696
  • 4
  • 25
  • 51
3

You could use scene2d. There you can inform the scene, that the window resized in application

@Override
public void resize(int width, int height) {
    stage.setViewport(width, height, true);
...
}
instantlink
  • 180
  • 1
  • 9
0

Divide your screen into virtual units, for example grid 10x10. Calculate your virtual units from actual screen size.

VIRTUAL_UNIT_WIDTH = Gdx.graphics.getWidth()/10;
VIRTUAL_UNIT_HEIGHT = Gdx.graphics.getHeight()/10;

And set your sprite size via those virtual units, and use virtual units when calling spriteBatch.draw();

Like this you will be able to keep the same aspect ratio of the game trough various screen resolutions.

Hope that this gives you an idea.

Veljko
  • 1,893
  • 6
  • 28
  • 58
0

I am using below approach and it's works for almost all screen sizes with ignoble minor scaling issue.

I always uses graphics images 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()));
stage = new Stage(screenViewport);

@Override
public void resize (int width, int height) {
    ScreenViewport screenViewport= (ScreenViewport) stage.getViewport();
    screenViewport.setUnitsPerPixel(Math.min(1920.0f/width,1080.0f/height));
    screenViewport.update(width,height,false);
}

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

It will result your camera view-port size near 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 set positions of Games UI screens.

Kalpesh
  • 1,767
  • 19
  • 29