I’m trying to understand the behavior of having multiple viewports in libgdx as seems unpredictable

I have been fighting with placing two small viewports, the main one which uses the entire screen space, and a secondary with is just at top right (as the idea is to draw a FPS counter there)

The problem is that I can’t understand how exactly it works, I have been talking with the AI, and after some changes the viewports just work…. But I need to understand why my first implementation doesn’t work, as the AI answer is quite bad “Sometimes you just have to use manual viewport placing, because sometimes setScreenPosition may not work as expected”

The NOT working code:

public class TestViewportGame extends Game {
    private SpriteBatch batch;
    private OrthographicCamera mainCamera;
    private OrthographicCamera smallCamera;
    private Viewport mainViewport;
    private Viewport smallViewport;
    private ShapeDrawerHack shapeDrawerHack;

    @Override
    public void create() {
        batch = new SpriteBatch();

        // Main camera setup
        mainCamera = new OrthographicCamera();
        mainViewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), mainCamera);
        mainViewport.apply();
        mainCamera.position.set(mainViewport.getWorldWidth() / 2, mainViewport.getWorldHeight() / 2, 0);
        mainCamera.update();

        // Small camera setup
        smallCamera = new OrthographicCamera();
        smallViewport = new FitViewport(200, 150, smallCamera);
        smallViewport.setScreenPosition(Gdx.graphics.getWidth() - 200, Gdx.graphics.getHeight() - 150);
        smallViewport.apply();
        smallCamera.position.set(smallViewport.getWorldWidth() / 2, smallViewport.getWorldHeight() / 2, 0);
        smallCamera.update();

        // Initialize ShapeDrawer
        shapeDrawerHack = new ShapeDrawerHack(new ShapeDrawer(batch));
        shapeDrawerHack.load();
    }

    @Override
    public void render() {
        ScreenUtils.clear(Color.BLACK);

        // Render main scene
        mainViewport.apply();
        mainCamera.update();
        batch.setProjectionMatrix(mainCamera.combined);
        batch.begin();
        // Draw main scene elements here
        batch.end();

        // Render small viewport
        smallViewport.apply();
        smallCamera.update();
        batch.setProjectionMatrix(smallCamera.combined);
        batch.begin();
        shapeDrawerHack.getShapeDrawer().filledRectangle(0, 0, smallViewport.getWorldWidth(), smallViewport.getWorldHeight(), Color.RED);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
        mainViewport.update(width, height);
        smallViewport.update(width, height);
    }

    @Override
    public void dispose() {
        batch.dispose();
        shapeDrawerHack.dispose();
    }
}

The working code:

public class TestGame extends Game {
    private SpriteBatch batch;
    private OrthographicCamera mainCamera;
    private OrthographicCamera smallCamera;
    private Viewport mainViewport;
    private Viewport smallViewport;
    private ShapeDrawerHack shapeDrawerHack;

    @Override
    public void create() {
        batch = new SpriteBatch();

        // Main camera setup
        mainCamera = new OrthographicCamera();
        mainViewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), mainCamera);
        mainViewport.apply();
        mainCamera.position.set(mainViewport.getWorldWidth() / 2, mainViewport.getWorldHeight() / 2, 0);
        mainCamera.update();

        // Small camera setup
        smallCamera = new OrthographicCamera();
        smallViewport = new FitViewport(200, 150, smallCamera);
        smallCamera.position.set(smallViewport.getWorldWidth() / 2, smallViewport.getWorldHeight() / 2, 0);
        smallCamera.update();

        // Initialize ShapeDrawer
        shapeDrawerHack = new ShapeDrawerHack(new ShapeDrawer(batch));
        shapeDrawerHack.load();
    }

    @Override
    public void render() {
        ScreenUtils.clear(Color.BLACK);

        // Render main scene
        mainViewport.apply();
        mainCamera.update();
        batch.setProjectionMatrix(mainCamera.combined);
        batch.begin();
        // Draw main scene elements here
        batch.end();

        // Render small viewport
        Gdx.gl.glViewport(Gdx.graphics.getWidth() - 200, Gdx.graphics.getHeight() - 150, 200, 150);
        smallCamera.update();
        batch.setProjectionMatrix(smallCamera.combined);
        batch.begin();
        shapeDrawerHack.getShapeDrawer().filledRectangle(0, 0, smallViewport.getWorldWidth(), smallViewport.getWorldHeight(), Color.RED);
        batch.end();

        // Reset viewport to main camera
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    }

    @Override
    public void resize(int width, int height) {
        mainViewport.update(width, height);
        smallViewport.update(width, height);
    }

    @Override
    public void dispose() {
        batch.dispose();
        shapeDrawerHack.dispose();
    }
}

Notice:

  • The only difference is the use of manual viewport positioning, instead of using the viewPort apply method
  • The only custom class is ShapeDrawerHack, which is just a wrapper around ShapeDrawer texture loading and disposal

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật