user1630217
user1630217

Reputation: 39

Poor render performance in LibGDX with hexagonal map

So I am making a simple game using LibGDX which involes a 150*150 hexagonal map, and various types of cell, rocky, clear etc.

Problem is when i load the map, my computer almost completely freezes up and any movement thats supposed to be fluid (character moving, button highlights) take 5+ seconds longer than they should.

Here's the relevant code:

public void render(float deltY){
    Gdx.gl.glClearColor(255, 255, 255, 100);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(); 

    polygonSpriteBatch.begin();
    for (int j = 0; j < 150; j++) {
        for (int i = 0; i < 150; i++) {
           offset = i%2 == 0 ? multipleX/2 : 0;
           if (mc.getMap().getRow(i).getTile(j).getTileType().equals(TileType.Rocky)) {
                drawCell(Color.BLACK, j, i);}
           if (mc.getMap().getRow(i).getTile(j).getTileType().equals(TileType.Clear)) {
                drawCell(Color.LIGHT_GRAY, j, i);}
        }
    }
    polygonSpriteBatch.end();
    stage.draw();
}

private void drawCell(Color color, int x, int y) {
    polySprite = new PolygonSprite(makePoints(color));
    polySprite.setX(mc.getMap().getRow(y).getTile(x).getTilePosition().get_x() * multipleX + offset);
    polySprite.setY(mc.getMap().getRow(y).getTile(x).getTilePosition().get_y() * multipleY);
    polySprite.draw(polygonSpriteBatch);
}


public PolygonRegion makePoints(Color color){
    side =  5;
    h = CalculateH(side);
    r = CalculateR(side);
    multipleX = (float)Math.sqrt(3)*side;
    multipleY = side+(side/2);
    float[] points = {      // vertices
            x, y,
            x+r, y+h,
            x+r, y+side+h,
            x,y+side+h+h,
            x-r, y+side+h,
            x-r, y+h};
    return new PolygonRegion(new TextureRegion(getTexture(color)),points
            , new short[] { //4 triangles using vertices to make hexagon
            0, 1, 5,
            1, 4, 2,
            5, 1, 4,
            2, 3, 4});
}
public Texture getTexture(Color color){


    Pixmap pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888);

    pix.setColor(color);
    pix.fill();
    textureSolid = new Texture(pix);

   return textureSolid;
}

I'm new to coding and LibGDX so there's probably something stupid i'm doing. Is there any way to render the map once and only redraw the polygons if they change?

Thanks

Upvotes: 0

Views: 630

Answers (1)

Khopa
Khopa

Reputation: 2220

Looking at your code, you are computing a square root for each cell, for each rendering pass.

So your code currently involves more than 22500 square root operations for each frame you render and is creating as many objects, that's quite a lot !

You should compute the points for your hexagons only once.

Upvotes: 1

Related Questions