Fullscreen resolution on Android for libgdx games

11,250

You've set your glViewport to 480x800, that means you're asking the phone hardware for a 480x800 window to draw on. Because most (all?) phone hardware doesn't do screen scaling (like your desktop monitor does), they just give you a 480x800 area on the screen.

You need to get OpenGL to "zoom" your screen, and you do that by setting the glViewport() to the physical resolution of your device (modulo aspect ratio caveats!). You should leave the camera at the 'virtual' resolution you prefer (so 480x800 in this case). Now OpenGL will scale all your primitives up to the screen's resolution.

As a quick test, try this:

gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

The next problem is that if the hardware aspect ratio doesn't match your 'virtual' aspect ratio. In that case you need to decide between stretching, leaving black bars on one side, or changing your virtual aspect ratio. See this blog post for more details on the camera/viewport setup, and some solutions to the aspect ratio issue: http://blog.acamara.es/2012/02/05/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/

Share:
11,250
MasterBirdy
Author by

MasterBirdy

Updated on June 27, 2022

Comments

  • MasterBirdy
    MasterBirdy almost 2 years

    I was wondering if there was a way to make Android games in libgdx so they all share the same resolution of 480x800. Using Gdx.graphics.setDisplayMode(480, 800, true) doesn't seem to change anything. Creating an OrthographicCamera of 480 by 800 makes it so the game is 480 by 800, but doesn't zoom into fullscreen and take up the entire screen like I expected it would. When I tested it out on my phone, the phone just used blank space to fill up the rest of the screen while the game in 480x800 resolution. Here is the code that I'm using.

    public class GameScreen implements Screen {
    
    private Game game;
    private OrthographicCamera guiCam;
    private SpriteBatch batch;
    private Texture texture;
    private Rectangle glViewport;
    
    public GameScreen (Game game)
    {
            this.game = game;
            guiCam = new OrthographicCamera(GAME_WIDTH, GAME_HEIGHT);
            guiCam.position.set(GAME_WIDTH / 2, GAME_HEIGHT / 2, 0);
            batch = new SpriteBatch();
            texture = new Texture(Gdx.files.internal("data/c2.png"));
            glViewport = new Rectangle(0, 0, GAME_WIDTH, GAME_HEIGHT);
    }
    
    @Override
    public void render(float delta) {
        if (Gdx.input.justTouched()) {
              texture = new Texture(Gdx.files.internal("data/c1.png"));
        }
        GL10 gl = Gdx.graphics.getGL10();
        gl.glClearColor(1, 0, 0, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        gl.glViewport((int) glViewport.x, (int) glViewport.y,
                (int) glViewport.width, (int) glViewport.height);
    
        guiCam.update();
        guiCam.apply(gl);
        batch.setProjectionMatrix(guiCam.combined);
        batch.begin();
        batch.draw(texture, 0, 0, 0, 0, 142, 192);
        batch.end();
    }
    
    private static final int GAME_WIDTH = 480;
    private static final int GAME_HEIGHT = 800;
    }
    

    Thanks in advance.

  • MasterBirdy
    MasterBirdy about 12 years
    Thank you, this was really helpful.