Hello,
So, I’ve implemented a rendering system where it only renders and updates what is on screen, which is common. The issue, however, is that I have to change the camera viewport size as I change my tile map size, which is not good.
Here is my rendering method: (The code that is commented out is what should be in place. int width, height is basically 15f and 9f).
public List<Tile> getDrawableBlocks(int width, int height) {
// int x = (int)player.getPosition().x - width;
// int y = (int)player.getPosition().y - height;
// if (x < 0) {
// x = 0;
// }
// if (y < 0) {
// y = 0;
// }
// int x2 = x + 2 * width;
// int y2 = y + 2 * height;
// if (x2 > level.getWidth()) {
// x2 = level.getWidth() - 1;
// }
// if (y2 > level.getHeight()) {
// y2 = level.getHeight() - 1;
// }
List<Tile> tiles = new ArrayList<Tile>();
Tile tile;
// for (int col = x; col <= x2; col++) {
// for (int row = y; row <= y2; row++) {
// tile = level.getTiles()[col][row];
// if (tile != null) {
// tiles.add(tile);
// }
// }
// }
for(int x = 0; x < level.getWidth(); x++){
for(int y = 0; y < level.getHeight(); y++){
tile = level.getTiles()[x][y];
if(tile != null){
tiles.add(tile);
}
}
}
return tiles;
}
The method is then called in my sprite batch:
public void drawBlocks(){
for(Tile tile : world.getDrawableBlocks(PlayState.CAM_WIDTH, PlayState.CAM_HEIGHT)){
Rectangle rect = tile.getBounds();
if(tile.getType() != Type.SOLID)
sb.draw(world.getTexture(tile), rect.x, rect.y, rect.width, rect.height);
}
}
The error I’m receiving is an out of bounds error.
Exception in thread "LWJGL Application" java.lang.ArrayIndexOutOfBoundsException: 18
at net.austinh.tilegame.models.World.getDrawableBlocks(World.java:42)
at net.austinh.tilegame.gears.WorldRenderer.drawBlocks(WorldRenderer.java:56)
at net.austinh.tilegame.gears.WorldRenderer.render(WorldRenderer.java:46)
at net.austinh.tilegame.screens.PlayState.render(PlayState.java:81)
at com.badlogic.gdx.Game.render(Game.java:46)
at net.austinh.tilegame.Main.render(Main.java:19)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:214)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
How can I appropriately make it so that I do not have to change my camera size as I change my tile map size? If you could share your code and ideas for better camera culling, that would be very much appreciated!
Thanks!
-A