2D tile render

Hi im working on a 2D tile rendering program but the tiles just keep jittering and flashing really wierdly

Heres my code:

for(int line : map) {
			    for(int x = 0; x < map.length; x++) {
		        	if(tilex>4){
		        		tilex-=4;
		        		tiley++;
		        	}else{
		        		tilex++;
		        	}
			        switch(map[x]) {
			            case 1:
			            	woodTexture.bind();
			                tiles(tilex*tileSize,tiley*tileSize);
			                break;
			            case 2:
			            	grassTexture.bind();
			                tiles(tilex*tileSize,tiley*tileSize);
			                break;
			        }
			    }

			}

Not to be picky, but that’s a rather convoluted way to perform a loop around something that is essentially a 2D array.

I mean, you’re not even using ‘line’ in your algorithm, so that might be something? Not sure without seeing the rest of the code.

If I had to guess, it comes from the multiple calls to the .bind() method. But don’t quote me there. Providing us with information about what library you’re using to draw this stuff, as well as information about what tiles(int x, int y) does might help us trouble shoot this better.

How would you recomend i redo it then

Thanks for your reply

I’m guessing you’re using Slick-Util?

Improve performance by placing all your tiles into the same texture (aka a sprite sheet), and then draw a textured quad using proper texcoords. Code example:
http://pastebin.java-gaming.org/54a6b7d54

Then the in-game code would look like this:


	public void init() {
		sheet = new Image2D(sheetTexture);
		tileGrass = sheet.getSubImage(...);
		tileDirt = sheet.getSubImage(...);
		tileWater = sheet.getSubImage(...);
	}

	public void render() {
		sheet.begin();
		for (int x=0; x<mapWidth; x++) {
			for (int y=0; y<mapHeight; y++) {
				Image2D img = getImageForTile( map[x][y] );
				img.draw( x * TILESIZE, y * TILESIZE );
			}
		}
		sheet.end();
	}

If all your tiles have the same size then you don’t need sub images. All you need is to calculate tile index in image and draw that spec. place using drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)

Ok i tried what you said and it didnt work heres my source code i would be really grateful if you could explain in full what im doing wrong here thanks!

http://pastebin.com/zCFMgGm3