Search code examples
androidlibgdx

How to set map size on Android using LibGdx


I'm working on a simple game and I have problem with setting the map size. I am using OrthographicCamera. I want the map to be visible. How should I set the proper size of viewport new OrthographicCamera(viewport_width, viewport_height)? Currently I'm passing some value and when I'm drawing elements on the map I don't see them because they are out of bound, if I make the width and height enormous I can see them. I want to have the whole map visible and draw everything on the sight of the user. I'm not sure where I'm making a mistake


Solution

  • You map is in square size and most of devices are in rectangular size so you need to keep your map in center of screen, either you're using portrait or landscape mode.

    public class TileTest extends ApplicationAdapter {
    
        ExtendViewport extendViewport;
        OrthogonalTiledMapRenderer mapRenderer;
        OrthographicCamera camera;
        float worldWidth,worldHeight;
    
        @Override
        public void create() {
    
            float tileWidth=64,tileHeight=64;
            float mapWidth=20,mapHeight=20;
    
            worldWidth=tileWidth*mapWidth;
            worldHeight=tileHeight*mapHeight;
    
            camera=new OrthographicCamera();
            extendViewport =new ExtendViewport(worldWidth,worldHeight,camera);
    
            TmxMapLoader mapLoader=new TmxMapLoader();
            TiledMap map=mapLoader.load("square.tmx");
    
            mapRenderer=new OrthogonalTiledMapRenderer(map);
        }
    
        @Override
        public void render() {
    
            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
            mapRenderer.setView(camera);
            mapRenderer.render();
        }
    
        @Override
        public void dispose() {
            mapRenderer.dispose();
        }
    
        @Override
        public void resize(int width, int height) {
            extendViewport.update(width,height,false);
            extendViewport.getCamera().position.set(worldWidth/2,worldHeight/2,0);
            extendViewport.getCamera().update();
       }
    }
    

    Output is :

    enter image description here