Created
December 21, 2015 22:36
-
-
Save madigan/29256618fd03a1c449b7 to your computer and use it in GitHub Desktop.
Infinite tiles in libGDX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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