Libgdx Box2d屏幕分辨率 [英] Libgdx Box2d screen resolution

查看:140
本文介绍了Libgdx Box2d屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过一些关于其他人如何处理不同屏幕分辨率的信息,但我仍然很困惑。

I've read a bit about what other people do to deal with different screen resolutions but I'm still very confused.

如果我有高分辨率的图像并且我想使用的精灵,我应该在1080x1920之类的屏幕上工作,而不是创建一些可降低分辨率的屏幕的方法吗?还是应该尝试将其压缩到一些中间分辨率,并根据需要放大和缩小?

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?

任何人都可以给我一个很好的例子来说明如何进行缩放不同的分辨率?例如,我是否可以查看播放器精灵与原始屏幕之间的比例(假设宽度和高度为1/6),并且当分辨率改变时,我只是尝试保持此比例?还是有更好的方法来做到这一点?

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?

还有另外一件事,我还将Box2d用于物理和其他方面,以防万一,因为我已经不得不在每米之间使用像素转换了。 / p>

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.

推荐答案

您不必创建自己的机制,说实话,您根本不必关心分辨率视口和Scene2d 阶段

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.

视口是一种定义,当由于分辨率变化而需要调整大小时,如何处理屏幕-是否应该拉伸?还是保持固定?视口种类繁多(每种视口只是另一类,因此您正在选择一种视口来创建selected类的对象),每种视口都有自己的行为。

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.

您可以在此处阅读有关Viewport的更多信息:

You can read more about Viewport here:

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

舞台是一种演示抽象,让我们考虑一下剧院的舞台和演员。阶段只是一个对象-您正在render部分中绘制的一种处理程序。演员就是您的图像,标签,按钮等。

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:

所有人要做的就是创建一个阶段,将您的actor(按钮,图像,标签...)添加到该阶段并将其设置为自定义视口。

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.

这篇关于Libgdx Box2d屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆