0

I've decided to create a simple game in Libgdx library, but after while I've got the problem.

The problem is with rendering the gameplay in fixed position in the screen. I'd like to split the screen for following parts:

  • gameplay which would takes around 70% spaces of screen,
  • character equipment,
  • chat.

But I can't still set position of viewport. Let's say how I understand the viewport and camera concepts. If I am wrong, please change my mind.

Camera decides what part of the world should be seen currently. For instance, I have a image with size 500x500px, if world's size is fixed to 200x200px then I can see only 200x200px part of the image.

Viewport is a thing responsible for translate camera view for fixed "window". For example, If currently camera shows 200x200px part then viewport can scale to fixed size (e.g. full screen).

If I understand these concepts properly, I am trying to set size of the gameplay viewport to 70% of the screen's width and height and it's working fine. Thanks to that, I see my world in above given size. Problem begins when I want to move gameplay viewport to left top corner by using ViewPort.setScreenPosition method. Calling the fucntion does not affect on gameplay view position.

Below is code of my prototype.

public class MyGdxGame extends ApplicationAdapter {
    private static final int WORLD_WIDTH = 250;
    private static final int WORLD_HEIGHT = 250;
    private SpriteBatch spriteBatch;
    private Texture texture;
    private OrthographicCamera gameplayCamera;
    private Viewport gameplayViewPort;

    @Override
    public void create() {
        spriteBatch = new SpriteBatch();
        texture = new Texture("life.png");

        gameplayCamera = new OrthographicCamera();
        gameplayCamera.translate(gameplayCamera.viewportWidth / 2.0f, gameplayCamera.viewportHeight / 2.0f);

        gameplayViewPort = new ExtendViewport(WORLD_WIDTH, WORLD_HEIGHT, gameplayCamera);
        gameplayViewPort.setScreenPosition(0, Gdx.graphics.getHeight() / 4);
        gameplayViewPort.apply();

        gameplayCamera.position.set(WORLD_WIDTH / 2.0f, WORLD_HEIGHT / 2.0f, 0);
    }


    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        gameplayCamera.update();
        spriteBatch.setProjectionMatrix(gameplayCamera.combined);

        spriteBatch.begin();
        spriteBatch.draw(texture, 0, 0);
        spriteBatch.end();
    }

    @Override
    public void resize(int width, int height) {
        gameplayViewPort.update(width * 3 / 4, height * 3 / 4);
        gameplayCamera.position.set(width / 2.0f, height / 2.0f, 0);
    }

    @Override
    public void dispose() {
        texture.dispose();
    }
}

I hope you know any properly solution for my issue and maybe it is not related to manipulating viewports at all. ;)

Expected effect

klimucha
  • 1
  • 1
  • 3

1 Answers1

0

I've been gone some through libgdx's docs and found section about multiply viewports. https://github.com/libgdx/libgdx/wiki/Viewports

After add line

viewport.apply().

in render method instead of create method, everything starts working properly.

klimucha
  • 1
  • 1
  • 3