Question

Now that I'm loading my map from a 2D Array of char, the game is really slow when I load more than 100 blocks (10x10 area). I have it rendering the blocks only within a certain distance of the character already. I also believe I know why it runs so slow, but I dont know how to fix it. Heres the code:

public void renderBlocks(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics 2D)g;
    for(int x = 0; x < 50; x++) {
        for(int y = 0; y < 50; y++) {
            //blocks[x][y] tells it which type of block to load
            Block next = new Block(blocks[x][y], x, y); 

            if((next.getX() - player.getX()) >= (-13*32) && 
            (next.getX() - player.getX()) <= (13*32)) {

                if((next.getY() - player.getY()) >= (-6*32) && 
                (next.getY() - player.getY()) <= (6*32)) {

                     g2d.drawImage(next.getImage(), (next.getX() - player.getX()),    
                      (next.getY() - player.getY()), this);
                }
            }
         }
     }
 }

The line:

Block next = new Block(blocks[x][y], x, y);

is what is making it run really slow. Even though it's not drawing those to the screen, it's still setting Block next equal to new Block(blocks[x][y], x, y) for all of the elements of blocks[x][y]. The problem is I can't just remove this line of code because it decides how far away from the character to draw blocks. I need to make it so that it only sets next equal to as many elements of blocks[x][y] that are being drawn to the screen.

I have tried putting:

if((x - player.getX()) >= (-13*32) && (x - player.getX()) <= (13*32)){
    Block next = new Block(blocks[x][y], x, y);
    ...Rest of Code here
}

around it and the same for y - playerY, but it doesn't work, though in theory it should.

Please answer this question if you know a way to do this and make the game run as fast as it did when I was loading the Blocks from an ArrayList, which allowed me to load over 5000 blocks and it run at a good FPS rate. If you need the source code to the game to help me fix this issue, comment that you need it and I will post it.

Was it helpful?

Solution

You could make the loop only over the tiles surrounding the player:

super.paint(g);
Graphics2D g2d = (Graphics 2D)g;

// find the block with the player (might need correction)
int playerX = (int) (player.getX() / 32);
int playerY = (int) (player.getY() / 32);

int viewDist = 6;

int lowerX = Math.max(playerX - viewDist, 0);
int upperX = Math.min(playerX + viewDist + 1, 50);
int lowerY = Math.max(playerY - viewDist, 0);
int upperY = Math.min(playerY + viewDist +  1, 50);

for (int x = lowerX; x < upperX; x++)
{
    for (int y = lowerY; y < upperY; y++)
    {
        Block next = new Block(blocks[x][y], x, y);
        g2d.drawImage(next.getImage(), (next.getX() - player.getX()),    
                      (next.getY() - player.getY()), this);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top