Skip to content Skip to sidebar Skip to footer

Libgdx Box2d Screen Resolution

I've read a bit about what other people do to deal with different screen resolutions but I'm still very confused. If I have high resolution images and sprites that I want to use, s

Solution 1:

you don't have to create your own mechanism and to be honest don't have to care about resolution at all thanks to Viewports and Scene2d Stages.

Viewport is kind of definition how to treat your screen when it comes to resizing because of resolution change - if it should stretch? or maybe stay fixed? There are many kinds of Viewports (and every kind is just another class so you are choosing kind of viewport creating the object of choosen class) and every has its own kind of behaviour.

You can read more about Viewport here:

https://github.com/libgdx/libgdx/wiki/Viewports

Stage is kind of presentation abstraction - let's think about stage in a theatre and actor on it. The stage is just an object - kind of handler you are drawing in render section. Actors are you images, labels, buttons etc.

You can read more about Stage here:

All you have to do is create stage that you will add your actors (buttons, images, labels...) to and set it your custom viewport.

public GameScreen(Main game)
    {
        super(game);

        viewport = new ExtendViewport(this.screenWidth, this.screenHeight); // viewport definition is ExtendViewport viewport; screenWidth and Height are defined width and height of your screen

        stage = new MyStage(); //stage definition is Stage stage;
        stage.setViewport(this.viewport);
    }

    @Overrideprotected void show()
    {   
        ...

        stage.addActor( someLabel ); //someLabel definition is Label someLabel; and it is created somewhere before

        ...
    }

    @Overridepublic void render(float delta) 
    {               
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        this.viewport.update(this.screenWidth, this.screenHeight);
        this.stage.act();
        this.stage.draw();
    }

    @Overridepublic void resize(int width, int height) 
    {
        //you need this function to keep screen size updatedthis.screenWidth = width;
        this.screenHeight = height;
    }

Now about your sprites'es size - it's not very good to have very very big resources if it will be mobile application - most of mobile devices have small resolutions rather so it's not good to keep big files in your app - remember it takes disc space.

Post a Comment for "Libgdx Box2d Screen Resolution"