0
\$\begingroup\$

As I was trying to adapt my game to different device resolutions I ran into a problem when adjusting the game screen between the "black bars." In my case I am using the FitViewport class from the LibGdx library to add the black bars for me. Everything works fine and the black bars (one bar in my case) are being effectively created when necessary. My problem, however, is that my screen is not centered and I end up with one ugly black bar either to the top, bottom, right, or left of my game. For example:

enter image description here

How can I fix this so it looks like this: enter image description here

Here is my code:

public class NordicMadness extends Game implements ApplicationListener{ 
    private Camera camera; 
    public static FitViewport viewport;

    @Override
    public void create () {
        camera = new PerspectiveCamera();
        viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
        AssetLoader.loadAssets();
        setScreen(new SplashScreen(this));
        System.out.println(Gdx.graphics.getWidth() + ", " + Gdx.graphics.getHeight());      
    }

    @Override
    public void dispose(){
        super.dispose();
        AssetLoader.dispose();
    }

    @Override
    public void resize(int width, int height) {
        // All resizing shall be done in main Game class                
        viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getWidth(), true);        
    }
}

The libgdx API states that adding true to the third argument of viewport.update(width, height, boolean center) should do the job but it's not working in my case.

After this I tried doing the following but with no success:

public void resize(int width, int height) {
        // All resizing shall be done in main Game class 
        ScalingViewport scalingViewport = (ScalingViewport)viewport;
        float leftGutterWidth = scalingViewport.getTopGutterHeight();       
        viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getWidth()); // Resize screen
        camera.position.set(0, Gdx.graphics.getHeight() - (leftGutterWidth / 2), 0);
    }
\$\endgroup\$
2
\$\begingroup\$

I have never used a viewport without a stage, but replacing width and height with the calls to Gdx.Graphics.getHeight/getWidth that you are using causes the same behavior you are experiencing. Removing those calls and using the passed values should fix your problem.

public void resize(int width, int height){
     stage.getViewport().update(width, height,    true);
}

You might find this helpful. https://github.com/libgdx/libgdx/wiki/Viewports

\$\endgroup\$
1
  • \$\begingroup\$ Wow I completely forgot to use width and height. I left Gdx.Graphics.getHeight/getWidth because I previously had a problem were width/height were giving me some issues so I completely forgot about those parameters. Thanks a ton! \$\endgroup\$
    – naitsirc
    Sep 3 '14 at 1:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .