舞台,视口,相机,缩放和Nexus 7

时间:2013-10-24 00:01:16

标签: camera libgdx viewport scaling stage

我是Android的新手,我也在学习如何使用libgdx。我发现了一些使用“DisplayScreen extends AbstractScreen”编码技术的教程,他们使用Stage和Actor来显示输出。我对这种编码技术的解释是DisplayScreen将使用AbstractScreen中的任何内容,除非它是@Override(如果我错了请纠正我)。

因此,如果我将代码放在AbstractScreen中,调整大小()以将显示缩放到更大的屏幕,同时保持宽高比; DisplayScreen中的舞台应调整大小以适应更大的屏幕。主要目标是我只想将我的游戏开发集中在800x480环境中并完全忽略所有不同的大小/分辨率。 AbstractScreen中的resize()将完成所有艰苦的工作以扩展并使我的游戏适合任何分辨率。

请允许我使用我的测试示例以获得更好的解释。我在手机上显示800x480黑色背景没问题。但是,显示的是相同的背景但未在Nexus 7上保持其宽高比。

这个turorial解决了我上面提到的问题(我采用屏幕两侧有两个黑条)。但是,我在将此解决方案集成到“DisplayScreen extends AbstractScreen”技术时遇到了一个小问题。

请参阅此屏幕截图here。我的问题是:

  1. 为什么我的黑匣子没有调整大小以适应屏幕,但在屏幕两侧留下两个红色条?
  2. 我不明白为什么黑色图像只在我的Nexus 7上显示766x480?
  3. 如果有人能指出我正确的方向,那将是太棒了。

    awesomegame的代码

    package com.example.somegame;
    
    import com.badlogic.gdx.Game;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Screen;
    import com.badlogic.gdx.assets.AssetManager;
    import com.example.somegame.screens.DisplayScreen;
    
    public class awesomegame extends Game {
    
    public static AssetManager AssetManager = new AssetManager();
    
    @Override
    public void create() {
    }
    
    @Override
    public void render() {
        super.render();
    }
    
    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
        setScreen( new DisplayScreen(this) );
    
    }
    
    @Override
    public void setScreen(Screen screen) {
        super.setScreen( screen );
    }
    

    AbstractScreen代码

    package com.example.somegame.screens;
    
    import com.example.somegame.awesomegame;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Screen;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.OrthographicCamera;
    import com.badlogic.gdx.graphics.g2d.BitmapFont;
    import com.badlogic.gdx.graphics.g2d.SpriteBatch;
    import com.badlogic.gdx.math.Vector2;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.utils.Scaling;
    
    public class AbstractScreen implements Screen{
    
    private awesomegame awesomegame;
    private Stage stage;
    
    private BitmapFont font;
        private SpriteBatch batch;
    private OrthographicCamera camera;
    
    public AbstractScreen(awesomegame awesomegame) {
        this.awesomegame = awesomegame;
    
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);
        camera.update();
    
        stage = new Stage(800, 480, false);
    }
    
    @Override
    public void render(float delta) {
        stage.act( delta );
        Gdx.gl.glClearColor( .5f, .5f, 0f, 1f );
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
    
        stage.draw();
    }
    
    @Override
    public void resize(int width, int height) {
        Vector2 size = Scaling.fit.apply(800, 480, width, height);
        int viewportX = (int)(width - size.x) / 2;
        int viewportY = (int)(height - size.y) / 2;
        int viewportWidth = (int)size.x;
        int viewportHeight = (int)size.y;
        Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
        stage.setViewport(800, 480, true);
    }
    

    DisplayScreen代码

    package com.example.somegame.screens;
    
    import com.example.somegame.awesomegame;
    
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.g2d.TextureAtlas;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.scenes.scene2d.ui.Image;
    
    public class DisplayScreen extends AbstractScreen {
    
    private Image loadingBg;
    
    private Stage stage;
    
    float loadingPercent;
    
    public DisplayScreen(awesomegame awesomegame) {
        super (awesomegame);
    }
    
    @Override
    public void show()
    {
        super.show();
    
        awesomegame.AssetManager.load("img/loading.atlas", TextureAtlas.class);
        awesomegame.AssetManager.finishLoading();
    
        stage = new Stage();
    
        TextureAtlas atlas = awesomegame.AssetManager.get("img/loading.atlas", TextureAtlas.class);
    
    
        loadingBg = new Image(atlas.findRegion("loadingBg"));
    
        loadingBg.setSize(800, 480);
        loadingBg.setX(0);
        loadingBg.setY(0);
    
        stage.addActor(loadingBg);
    
        // add all asset need to be loaded here. for example
        // awesomegame.AssetManager.load("img/whatever.pack", TextureAtlas.class);
    
    }
    
    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor( 1f, 0f, 0f, 1f );
        stage.draw();
    
    }
    

2 个答案:

答案 0 :(得分:1)

Nexus 7的屏幕分辨率为1280 x 800,但部分高度用于屏幕菜单面板(使用背面/主页/菜单按钮)。

您的代码中的主要元凶就在这里,您尝试强制执行不适合此调整维度的特定宽高比,从而导致这些条形图一边:

public void resize(int width, int height) {
    Vector2 size = Scaling.fit.apply(800, 480, width, height);
    ...
}

看起来你从另一个stackoverflow帖子中删除了resize函数。我也做了同样的事情,但当我遇到同样的问题时,我改用了更简单的东西:

public void resize(int width, int height) {
    stage.setViewport(true, width,height);
    stage.getCamera().setToOrtho(false,width,height);
    ...
}

答案 1 :(得分:0)

此代码适用于我的最新更新:

OrthographicCamera是凸轮,这不会裁剪,只需更改视口,使宽度仍然比实际窗口/设备“大很多倍”

public void resize(int width, int height) {
    int newW = width, newH = height;
    if (cam.viewportWidth > width) {
        float scale = (float) cam.viewportWidth / (float) width;
        newW *= scale;
        newH *= scale;
    }

    // true here to flip the Y-axis
    cam.setToOrtho(true, newW, newH);
}