3

I am currently using the Orthographic Camera. Before I get too much into the game, I want to fix the screen size first. Currently, I have the screen size set for 800 x 480. Will this work well on other devices and screen sizes? Right now all I have is a splash screen and game screen. What lines of code would I have to add to achieve this.

Dakota
  • 115
  • 2
  • 11
  • Use an emulator like the native avd or genymotion, to test across a wide array. Haven't done much libgdx work but that's how I usually test resolution. – zgc7009 Aug 31 '14 at 01:26
  • Take a look at the wiki of libgdx about viewports. I think you'll find a way better solution there! https://github.com/libgdx/libgdx/wiki/Viewports – bemeyer Sep 01 '14 at 12:32

2 Answers2

6

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

Your issues come from the fixed camera height. Having a fixed height will cause your screen to stretch taller or shorter depending on the aspect ratio of the device screen. If you want to account for the different aspect ratios, you'll need to multiply your camera height (currently 480) by the display ratio. You can get the screen ratio by dividing the height by the width by the height. This would look something like:

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

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

As pointed out by BennX in the comments, LibGDX introduced viewports which allow you do very much the same thing as above, just in a different way. To achieve the same effect as I outlined above, only using a viewport, you'd use an ExtendViewport. What this does is maintain the world size in one direction while stretching it in the other direction. So the world will first scale up to fill the screen, then the shorter dimension is expanded while maintaining aspect ratio. To create this type of viewport, it'd look something like this:

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

The viewport above will have a minimum width of 800 and a minimum height of 480. One of these values will be the same after the viewport is applied and the other will change based on the aspect ratio of the screen. More narrow screens will have more vertical space while wider screens will have more horizontal space. For more on viewports, visit the LibGDX wiki page here.

Michael Celey
  • 12,645
  • 6
  • 57
  • 62
  • I dont think its a good solution. Libgdx implemented Viewports especialy for that problem like 4 month ago which does all the stuff. https://github.com/libgdx/libgdx/wiki/Viewports – bemeyer Sep 01 '14 at 12:35
  • 1
    @BennX Thanks for the heads up. I'm still using a slightly older version of the engine that didn't have Viewports in it so I wasn't aware of this solution. I've updated the answer to reflect both solutions. – Michael Celey Sep 01 '14 at 15:48
  • @Mceley Would I have to apply this line of code to every screen in my application? – Dakota Sep 02 '14 at 01:22
  • You should only have to do this in the create() method your Game class or whatever your main ApplicationListener class is. Then, if you're using stages, you need to set the viewport to the current stage using [setViewport()](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/Stage.html#setViewport-com.badlogic.gdx.utils.viewport.Viewport-) – Michael Celey Sep 02 '14 at 01:28
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()));

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