Background created from rectangles, low FPS.

Hi, Just started using P5 since I'm making 2D online shooter (got it all working without P5, switching to P5). So, what I want to do is create a background (very big) that's made from rectangles.

Here is a function that draw such background.

function drawMap() {
    var x = width/2 - player.x;
    var y = height/2 - player.y;

    background(127);

    /**   THIS IS MIDDLE OF THE MAP */
    for (var i = 0; i < rows; i++) {
        for (var k = 0; k < cols; k++) {
            if (i <= 40 && k <= 40 && i >= rows-3 && k >= cols-3) continue;
            if(player.x-width/2 > (k+1)*scl || player.x+width/2 < k*scl) continue;
            if(player.y-height/2 > (i+1)*scl || player.y+height/2 < i*scl) continue;
            noFill();
            stroke(32);
            rect(x + (k*scl), y + (i*scl), scl, scl);
        }
    }

    /**   THIS IS BORDER OF THE MAP */
    for (var i = 0; i < rows; i++) {
        for (var k = 0; k < cols; k++) {
            if(player.x-width/2 > (k+1)*scl || player.x+width/2 < k*scl) continue;
            if(player.y-height/2 > (i+1)*scl || player.y+height/2 < i*scl) continue;
            if (i > 40 && k > 40 && i < rows-3 && k < cols-3) continue;
            stroke(32);
            fill(0);
            rect(x + (k*scl), y + (i*scl), scl, scl);
        }
    }
}

As you can see, I did some optimizations like not drawing rectangles offscreen.

Player is always centered and everything else is acting like there is camera above player so it's moving around etc.

Here is some image to that:

Screenshot 2017-01-03 18-32-03

Still, it's not doing well, ~10 FPS, that's bad, very bad.

If we look at http://diep.io you can see that it's using the same way to draw background and it's way more optimized.

Any ideas what to do with this?

Tagged:

Answers

Sign In or Register to comment.