TileMap taking huge time to load in phone

Forum for the book "Beginning Android Games", see http://www.apress.com/9781430230427

TileMap taking huge time to load in phone

Postby Qureshi » Tue Mar 14, 2017 2:23 pm

I am developing a game in which a level has 2400 * 450 tiles Map and each tile is of 4 pixels. I am also using box2d in my game.
My problem is that when I run the game on my phone it takes a lot of time to load the screen (more than 50 sec). How can I reduce this loading time?

Below is my code (SCALE is 2.0f and PPM is 4)

Show Method
Code: Select all
@Override
    public void show() {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        camera = new OrthographicCamera();
        camera.setToOrtho(false, w / SCALE, h / SCALE);
        camera.update();
        viewport = new FitViewport(w / SCALE, h / SCALE  ,camera);
        viewport.apply();

        world = new World(new Vector2(0f, -9.8f), false);
        b2dr = new Box2DDebugRenderer();

        Gdx.input.setInputProcessor(this);

        map = new TmxMapLoader().load("maps/lvl3.tmx");
        tmr = new OrthogonalTiledMapRenderer(map, 1/SCALE);
        MapProperties prop = map.getProperties();

        int mapWidth = prop.get("width", Integer.class);
        int mapHeight = prop.get("height", Integer.class);
        int tilePixelWidth = prop.get("tilewidth", Integer.class);
        int tilePixelHeight = prop.get("tileheight", Integer.class);

        mapPixelWidth = mapWidth * tilePixelWidth;
        mapPixelHeight = mapHeight * tilePixelHeight;

        batch = new SpriteBatch();
        tex = new Texture("images/frog.png");

        frog = createBox(100,100,w * 0.3f , h * 0.3f , false);
        createBox(0,mapHeight * 0.2f, mapPixelWidth , 32, true);

    }



Render Method
Code: Select all

    @Override
    public void render(float delta) {
        update(Gdx.graphics.getDeltaTime());

        //Render
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        tmr.render();

        batch.begin();
        batch.draw(tex, frog.getPosition().x * PPM - 5.05f/2, frog.getPosition().y * PPM - 6.55f/2, w * 0.3f , h * 0.3f );
        batch.end();

        b2dr.render(world, camera.combined.scl(PPM));
    }

    public void update(float delta) {

        world.step(1 / 60f, 6, 2);
        cameraUpdate(delta);
        tmr.setView(camera);
        batch.setProjectionMatrix(camera.combined);
    }

    public void cameraUpdate(float delta) {
        Vector3 position = camera.position;
        position.x = frog.getPosition().x * PPM;
        position.y = frog.getPosition().y * PPM;

        float halfWidth = frog.getPosition().x * PPM;
        float halfHeight = frog.getPosition().y * PPM;

        //for x axis
        if ((camera.viewportWidth/2 )> halfWidth)
            position.x=camera.viewportWidth/2;
        else if((mapPixelWidth - camera.viewportWidth/2)< halfWidth)
            position.x=mapPixelWidth -camera.viewportWidth/2;
        else
            position.x=halfWidth;

        //for y axis
        if(camera.viewportHeight/2 > halfHeight)
            position.y= camera.viewportHeight/2;
        else if((mapPixelHeight-camera.viewportHeight/2) < halfHeight)
            position.y = mapPixelHeight-camera.viewportHeight/2;
        else
            position.y = halfHeight;

        camera.position.set(position);
        camera.update();

    }



Resize Method
Code: Select all
    @Override
    public void resize(int width, int height) {
        camera.setToOrtho(false, width / SCALE, height / SCALE);

        viewport.update( width , height );
    }
Qureshi
 
Posts: 4
Joined: Tue Feb 07, 2017 8:22 am

Re: TileMap taking huge time to load in phone

Postby tenfour04 » Tue Mar 14, 2017 8:01 pm

Are your tiles in separate images? If so, that would explain it--use a texture atlas.
tenfour04
 
Posts: 1044
Joined: Sat Jun 18, 2011 3:24 pm

Re: TileMap taking huge time to load in phone

Postby Qureshi » Wed Mar 15, 2017 2:50 pm

tenfour04 wrote:Are your tiles in separate images? If so, that would explain it--use a texture atlas.

Yes, I created my tiled map using 3 separate tilesets. will try using a Texture atlas
Qureshi
 
Posts: 4
Joined: Tue Feb 07, 2017 8:22 am


Return to Beginning Android Games

Who is online

Users browsing this forum: No registered users and 2 guests