Libgdx multiple viewports, cameras and properly sizing tiled map

21 Views Asked by At

I am having a hard times to connect the dots between camera, viewports and my tiled map on android app.

So I am having a Screen in which I want to have two separate parts of game. Top which would be 1/4th of the screen height and the bottom that takes all remaining space so 3/4.

I have created two stages with viewports and defined dimensions (total dimensions that I would like to work on are 400x800):

    private val topStage = Stage(FillViewport(400F, 200F)).apply {
        addActor(TopActor())
        isDebugAll = true
    }
    private val bottomStage = Stage(FitViewport(400F, 600F)).apply {
        addActor(BottomActor())
        isDebugAll = true
    }

First problem is that my actors who are currently drawing only squares for a debug purposes are not taking up whole space on all devices - I know that viewports are responsible for that, so that top FillViewport makes my top part fill missing top space. But unfortunately the bottom one is not working like that - if I use FillViewport it takes whole screen instead. Is there a way to know the real size of the bottom part so I can stick to 600 on bottom and take the rest of the space for my top part?

The second problem is with my tiled map that I want to display in top part. My test tile map has 30x13 tiles of 32x32 size.

So in my actor I am creating additional camera and loading the map like:

private val camera = OrthographicCamera()
private val tiledMap = TmxMapLoader().load("test.tmx")
private val renderer = OrthogonalTiledMapRenderer(tiledMap, 1/32F)

I also override the resize method:

topStage.viewport.update(width, height, true)
bottomStage.viewport.update(width, height, true)

Then I am trying to display part of the tile map on the width scale so doing

camera.setToOrtho(false, 30F, 26F)

But this makes the map totally squished and not properly visible - what I would like to archive is to have the map fill the height and then move to the right and left depending on my input.

How should I scale that tile map camera respecting all of the conditions? Maybe there is some better way to handle this case instead of combining two stages, with viewports with actors and internal ortho camera ?

0

There are 0 best solutions below