Pookie
Pookie

Reputation: 1279

FPS in libgdx java dropping to 20 in a simple game

I am trying to maintain an FPS of 60. However when I run my game, it drops down to about 16- 20 FPS. I seem to have found the problem but I just can't figure out why it is a problem and how to fix it. The problem lies in my render method for my grid (my game uses a 2d boolean array for collision). I use one double for loop in my render method to draw the grid itself out(for debugging purposes) and another double for loop to draw where the true elements in the grid are(where things that can be collided with lie). The strange part is, they do the exact same thing essentially but by commenting out the upper loop, it doesn't fix it, but commenting out the lower loop does. Here is code to better understand.

Here is the full render method

    public void render(ShapeRenderer render){

    render.setColor(Color.WHITE);
    for(int x = 0; x < mapGrid.length; x+= CELL_SIZE){
        for(int y = 0; y < mapGrid[x].length; y+= CELL_SIZE){
            render.line(x, y, x + CELL_SIZE, y);
            render.line(x + CELL_SIZE, y, x + CELL_SIZE, y + CELL_SIZE);
            render.line(x + CELL_SIZE, y + CELL_SIZE, x, y + CELL_SIZE);
        }
    }

    render.setColor(Color.RED);
    for(int x = 0; x < mapGrid.length; x++){
        for(int y = 0; y < mapGrid[x].length; y++){
            if(mapGrid[x][y]){
                render.line(x * CELL_SIZE, y * CELL_SIZE,x * CELL_SIZE + CELL_SIZE, y * CELL_SIZE);
                render.line(x * CELL_SIZE + CELL_SIZE, y * CELL_SIZE, x * CELL_SIZE + CELL_SIZE, y * CELL_SIZE + CELL_SIZE);
                render.line(x * CELL_SIZE + CELL_SIZE, y * CELL_SIZE + CELL_SIZE, x * CELL_SIZE, y * CELL_SIZE + CELL_SIZE);
            }
        }
    }


}

Now if I were to comment out the 3 render.line methods in the second double for loop, there is no issues, full 60 FPS working fine. However if I comment out the first loop instead, my FPS drops down to 16 FPS. Since this is only used for debugging I understand I can just delete it, however my game will be in debug mode for quite some time and getting this to run smoothly is practically essential as of right now.

The only difference in the two loops is the one if statement in the second double for loop and the loop is slightly longer, but in the game the second for loop is only rendering 3 things as there are only 3 platforms being rendered at this point int time. If the if statement is truely the problem, I need to know how to fix it. I just don't understand why my FPS is dropping so hard.

Any help is extremely appreciated in helping me figure this out. Thank you!

EDIT: I ran an if statement through the top statement every time and commented the second one out and still maintained 60 FPS so I don't believe it is the if statement

Upvotes: 0

Views: 278

Answers (1)

user146043
user146043

Reputation:

Your second loop has loads of multiplies which are missing from the first loop. Multiplication is nearly always a lot slower than addition. I'd find a way of optimising that; perhaps precalculating them or using a similar loop to the first loop.

Upvotes: 1

Related Questions