Libgdx Box2d screen resolution

心不动则不痛 提交于 2019-12-25 13:16:34

问题


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, should i work with a screen that like 1080x1920 and than create some methods that scale down for lower resolution screen? Or should I try to compress it down to some middle ground resolution and scale up and down as necessary?

Also can anyone give me a good example of how to to the scaling for different resolutions? Would I for example see what the ratio between my player sprite and my original screen (lets say 1/6 of the width and height) and when the resolution changes i just try to keep this ratio? Or is there a better way to do this?

One more thing I'm also using Box2d for physics and stuff in case that matter cause I already have to use a pixel per meter conversion to go between the two.


回答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);
    }

    @Override
    protected void show()
    {   
        ...

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

        ...
    }

    @Override
    public 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();
    }

    @Override
    public void resize(int width, int height) 
    {
        //you need this function to keep screen size updated
        this.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.



来源:https://stackoverflow.com/questions/32255098/libgdx-box2d-screen-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!