适用于libgdx游戏的Android全屏分辨率

时间:2012-03-27 23:46:16

标签: android libgdx

我想知道是否有办法在libgdx中制作Android游戏,所以他们都拥有480x800的相同分辨率。使用Gdx.graphics.setDisplayMode(480,800,true)似乎没有任何改变。创建480乘800的OrthographicCamera使得游戏的尺寸为480乘800,但不会缩放到全屏并占据整个屏幕,就像我预期的那样。当我在手机上测试时,手机只用空格填充屏幕的其余部分,而游戏的分辨率为480x800。这是我正在使用的代码。

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;
}

提前致谢。

1 个答案:

答案 0 :(得分:11)

您已将glViewport设置为480x800,这意味着您要求手机硬件使用480x800窗口进行绘制。因为大多数(所有?)手机硬件都不进行屏幕缩放(就像桌面显示器那样),它们只会在屏幕上显示480x800区域。

你需要让OpenGL“缩放”你的屏幕,你可以通过将glViewport()设置为设备的物理分辨率(模数宽高比警告!)来实现。您应该将相机保持为您喜欢的“虚拟”分辨率(在这种情况下为480x800)。现在,OpenGL将所有基元缩放到屏幕分辨率。

快速测试,试试这个:

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

下一个问题是,如果硬件宽高比与您的“虚拟”宽高比不匹配。在这种情况下,您需要决定拉伸,在一侧留下黑条,或更改虚拟纵横比。有关相机/视口设置的更多详细信息以及宽高比问题的一些解决方案,请参阅此博客文章:http://blog.acamara.es/2012/02/05/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/