1
\$\begingroup\$

Let's say that my code looks like this:

OrtographicCamera camera=new OrthographicCamera();
camera.setToOrtho(true, 100, 150);
SpriteBatch spriteBatch=new SpriteBatch();
spriteBatch.setProjectionMatrix(camera.combined);
TextureRegion textureRegion = textureAtlas.findRegion("redCircle");
spriteBatch.begin();
spriteBatch.draw(textureRegion, 200, 200);
spriteBatch.end();
//This will try to render a TextureRegion outside of the OrthographicCamera's field of view

My question is: should I write my code so that it checks everytime the coordinates of what I am trying to draw, detects if it's offscreen, and in that case skips it (to spped up the rendering), or does Libgdx already do that for me?

\$\endgroup\$
  • \$\begingroup\$ I had asked the same question and my conclusion was yes, you should check it yourself, because it appears that it matters. The math to check if something is in screen is very simple, so there is no point not doing it. Its a simple if check. \$\endgroup\$ – dimitris93 Sep 28 '15 at 18:27
  • \$\begingroup\$ @Shiro , I have run some tests, and it seems that there's actually no need to check, as libgdx already does that. See my answer for details. \$\endgroup\$ – Riccardo Vailati Sep 28 '15 at 19:43
3
\$\begingroup\$

Ok, so I tried executing these 2 pieces of code: (camera is camera=new OrthographicCamera(); camera.setToOrtho(true, 100, 100*screenW/screenH);, and the 2 texture regions are of size 64*64, from the same textureAtlas)
Code 1: rendering on-screen

public void renderGameScreen(){
    spriteBatch.setProjectionMatrix(camera.combined);
    spriteBatch.begin();
    TextureRegion tr;
    for (int i = 0; i<1000; i++){
        if (Math.random()>0.5){
            tr = R.border;
        } else {
            tr=R.gamebackground;
        }
        spriteBatch.draw(tr, (float)(Math.random()*100), (float)(Math.random()*100));
    }
    spriteBatch.end();
}

Versus code 2: rendering off-screen:

public void renderGameScreen(){
    spriteBatch.setProjectionMatrix(camera.combined);
    spriteBatch.begin();
    TextureRegion tr;
    for (int i = 0; i<1000; i++){
        if (Math.random()>0.5){
            tr = R.border;
        } else {
            tr=R.gamebackground;
        }
        spriteBatch.draw(tr, (float)(Math.random()*100)+200, (float)(Math.random()*100)+200);
    }
    spriteBatch.end();
}

Notice how the two are identical except for the spriteBatch.draw() method, which in code 2 is offset by 200 pixels, and is therefore off screen)
Result:
On PC, code 1 was at 10 to 30 fps. code 2 was at 50 to 60 fps.
On android code 1 was at 3-4 fps, code 2 was at 60.
So, I'd conclude that yes, libgdx does check if a texture is offscreen before rendering it.
If there are any flaws in my test that may have caused it to give the wrong result, I'd be glad if someone could point them out.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.