Skip to content

Instantly share code, notes, and snippets.

@madigan
Created December 21, 2015 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madigan/29256618fd03a1c449b7 to your computer and use it in GitHub Desktop.
Save madigan/29256618fd03a1c449b7 to your computer and use it in GitHub Desktop.
Infinite tiles in libGDX
// Draw a background
private void drawBackground() {
// To simplify the code a little, do these calculations ahead of time
// To speed things up, we might optimize this.
float cameraLeft = camera.position.x - camera.viewportWidth / 2f;
float cameraRight = camera.position.x + camera.viewportWidth / 2f;
float cameraTop = camera.position.y + camera.viewportHeight / 2f;
float cameraBottom = camera.position.y - camera.viewportHeight / 2f;
float width = background.getWidth();
float height = background.getHeight();
for(float column = (float) Math.floor(cameraLeft / width) * width; column < cameraRight; column += width) {
for(float row = (float) Math.floor(cameraBottom / height) * height; row < cameraTop; row += height) {
batch.draw(background, column, row);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment