Java 2D Game Tile rendering problem with difference between height and width

116 views Asked by At

So i am programming basic Java 2D Game with a TileManager. I am creating the maps in Tiled and export a .xml file.

Here an example:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.7.2" orientation="orthogonal" renderorder="right-down" width="25" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="2" nextobjectid="1">
 <tileset firstgid="1" name="outdoor" tilewidth="16" tileheight="16" tilecount="1975" columns="25">
  <image source="outdoor.png" trans="ffffff" width="400" height="1264"/>
 </tileset>
 <layer id="1" name="Back" width="25" height="20">
  <data encoding="csv">
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,
176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176,176
</data>
 </layer>
</map>

The following code takes the information and renders the specific tile:

public class TileMapNorm extends TileMap{
    
    private ArrayList<Block> blocks;
    
    public TileMapNorm(String data, Sprite sprite, int width, int height, int tileWidth, int tileHeight, int tileCols) {
        blocks = new ArrayList<Block>();
        String[] block = data.split(",");
        for(int x = 0; x < (width * height); x++) {
            int tmp = Integer.parseInt(block[x].replaceAll("\\s+", ""));
            if(tmp != 0) {
                blocks.add(new NormBlock(sprite.getSprite((int) ((tmp - 1) % tileCols), (int) ((tmp - 1) / tileCols)), new Vector2((int) (x % width) * (tileWidth), (int) (x / height) * (tileHeight)), tileWidth, tileHeight));
            }
            
        }
    }
    
    public void render(Graphics2D g) {
        for(int x = 0; x < blocks.size(); x++) {
            blocks.get(x).render(g);
        }
    }
}

When width and height of the map are equal or the height is bigger then the width, then the map renders fine. But when the width is bigger then the height, then there is a rendering problem as following:

image of rendering error

What is the problem and how can it solved?

0

There are 0 answers