Search code examples
javaperformancegraphicsframe-rate

Java platformer game performance


I recently started working on a 2d platformer in java, and am asking a performance related question.

In my game, I have a world (relatively small for now).

But over half+ of the world isn't visible to the players camera, however I'm still painting the entire world (even the unseen parts).

Here's what I'm doing right now.

@Override
public void paintComponent(Graphics g){
    g.translate(player.camX(), player.camY());

    for (GameObject gameObject : solidObjects){
        if (gameObject.isTouching(player.getCameraRect()))
            gameObject.paint(g);
    }

    player.paint(g);

    for (GameObject gameObject : unSolidObjects){
        if (gameObject.isTouching(player.getCameraRect()))
            gameObject.paint(g);
    }

}

So the question is, would it be faster having an if-statement checking if its on the screen before painting?


Solution

  • Yes.

    Doing a check to see if it's on a screen should take no more than 1 or 2 steps.

    Painting everything requires you to draw every single pixel on to the screen regardless of whether it's used or not.