David Lasry
David Lasry

Reputation: 1407

support screen sizes libgdx - android

I'm trying to develop a snake game to Android devices using Libgdx. As we all know, the snake game is based on a grid which holds different cells.

Now, I have developed the first version of the game. In the desktop, it looks fine, however, in big Android devices(Galaxy S4, HTC ONE), all the objects look very small. Furthermore, the functionality of the game has been "destoyed".

In the desktop, the game runs on a resolution of 480px Width X 800px Height(OrthographicCamera). I also defined in the game screen an instance of a Viewport class and set it to FillViewPort(when the resize method in invoked, I update the viewport).

Obviously all this things didn't work. The game size is still distorted.

How can I maintain this fixed resolution or I do I scale the game and keep this ratio between the pixels.

P.S I have tried to implement all the types of Viewports as they are documented in the official site of libgdx - https://github.com/libgdx/libgdx/wiki/Viewports

Didn't help my snake game.

Here is my screen's code:

package com.david.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.david.gameworld.GameRenderer;
import com.david.gameworld.GameWorld;
import com.david.helpers.InputHandler;

public class GameScreen implements Screen{

    private GameRenderer renderer;
    private GameWorld world;
    private OrthographicCamera camera;
    private Viewport viewport;

    public GameScreen() {
        world = new GameWorld();
        this.camera = new OrthographicCamera();
        this.camera.setToOrtho(false, 480, 800);
        viewport = new ExtendViewport(480, 800);
        renderer = new GameRenderer(world, camera);
        Gdx.input.setInputProcessor(new InputHandler(world.getSnake()));
    }

    @Override
    public void render(float delta) {
        // TODO Auto-generated method stub
        renderer.render(1/delta);
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub
        viewport.update(width, height);
    }

    @Override
    public void show() {
        // TODO Auto-generated method stub      
    }

    @Override
    public void hide() {
        // TODO Auto-generated method stub      
    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub      
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub      
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub      
    }
}

Upvotes: 0

Views: 427

Answers (1)

Boldijar Paul
Boldijar Paul

Reputation: 5495

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

And before drawing stuff, don't forget to set your projection matrix.

Upvotes: 1

Related Questions