How to render this the fastest way?

Hey all!

I have a little problem with rendering tiles for my map, it really slowes down the whole game very much, I noticed that using “rotate” really wasnt as cheap as i belived.

As it is now, i can run the game without rendering the tiles and get a nice fps at 50, but if i render the tiles it slowes down to about 20, with the rotation involved the game spins in on about 5-10 fps, well this just is’nt feasible so i really would need some rendering tips from some of you gurus on the forum =)

Here is the rendering function anyway, maybe you guys can spot something there and give me some pointers on how to make the whole thing faster…


	public void drawMap(Graphics2D g, Map m, int indexX, int indexY, short[] tileWidth, short[] tileHeight)
	{
		int tempIndexX = indexX;
		boolean trans = false;
		AffineTransform original = g.getTransform();
		
		for(int i = tileHeight[0]; i < tileHeight[1]; i++)
		{
			for(int j = tileWidth[0]; j < tileWidth[1]; j++)
			{
				if(i >= 0 && j >= 0 && i < m.sizeY && j < m.sizeX)
				{
					if(m.MapTile[j][i].id == 0)
					{
						
					}
					else
					{
						switch(m.MapTile[j][i].transform)
						{
						case 0:
							break;
						case 1:
							g.rotate(1.5707963267949d, indexX+24, indexY+24);
							trans = true;
							break;
						case 2:
							g.rotate(3.1415926535898d, indexX+24, indexY+24);
							trans = true;
							break;
						case 3:
							g.rotate(4.71238898038469d, indexX+24, indexY+24);
							trans = true;
							break;
						}
						
						switch(m.MapTile[j][i].id )
						{
						case 1:
							image = concrete1;
							break;
						case 2:
							image = concrete2;
							break;
						case 3:
							image = concrete2_c;
							break;
						case 4:
							image = concrete2_cc;
							break;
						case 101:
							image = wall1;
							break;
						case 102:
							image = wall1_c;
							break;
						case 103:
							image = wall1_cc;
						case 201:
							if(frame == 0)
								image = dorr1f1;
							else if(frame == 1)
								image = dorr1f2;
							else
								image = dorr1f3;
							break;
						default:
							image = concrete1;
						break;
						}
						
						g.drawImage(image, indexX, indexY, 48, 48, null);
						
						if(trans)
							g.setTransform(original);
					}
				}
				indexX += 48;
			}
			indexY += 48;
			indexX = tempIndexX;
		}
	}

tnx

Your code rotates in increments of 90 degrees. Therefore you could pre-rotate each tile in the initialisation & just use the appropriately rotated tile in the main loop. That should get you back to 20fps.

If you assigned all the images adjacent numbers starting from zero, you could just index into an array of images to acquire the correct image, saving on the switch statements. E.g. a 2D array of image[20][4] would provide 20 images in each of the four rotations. This probably won’t gain a huge amount.

Speeding things up is always a problem. Inspecting each line in the innermost loop & asking yourself does it really need to be there is a good start.

Alan

Yes, i think i will go with the “pre rendered” tiles, i really thought that i could work with transform now when i have accelerated graphics, but not hehe.
As a mather of fact, the game is much slower now than when i loaded my gfx with Toolkit/Image (now i use ImageIO/BufferedImage) so thats a little wierd, but i’ll continuing working on optimisation hehe, when will i be able to accually work on the game? =P

Very well, thx for the tip, i’ll start working on the init rotate stuff right away!

Wow!

Finally figured out how to enable the OpenGL pipeline, and i thought that this already were enabled, but it was’nt.

Now the game spins at 40-50 fps with the AffineTransform operations =))))
Soooooo sweet to finaly see it all work out like it should! damn thats sweet, now i can finally sitt down and start programing a game again hehe…

Anyway, tnx for the help. Is there a way to permenantly enable OpenGL pipeline or do i always have to run the game with the -Dsun stuff? And if so, is there a way to configure Eclipse to run the game with OpenGL enabled???

Thats odd that BufferedImage/ImageIO was slower… one thing you might consider is whether your images were larger than 256x256 (or any other dimensions with > 65536 pixels), it probably won’t get accelerated if its this big. The other thing is that it doesn’t matter if the image is accelerated if you rotate it (or scale it or draw it with translucency) because it loses its acceleration.

I’m guessing you are using jdk 1.5 (or higher), so this may not be an issue but if you plan on making your game compatible for 1.4 users then you should know that just loading the image with ImageIO won’t accelerate it. Instead you need to obtain a GraphicsConfiguration object and use its createCompatibleImage method to obtain a managed BufferedImage. From there you can draw your loaded image onto the managed image you created (Graphics2D g2 = myManagedImage.createGraphics();). I’m sure you can find a more complete example by using the search feature on these forums if you need to.

I’m not sure its safe to rely on the opengl pipeline as of 1.5, but maybe someone else will say differently :slight_smile: If you don’t absolutely require the rotation of large (or a bunch of) images in real-time then java2d should be sufficient, but otherwise you might want to look into using an opengl binding (lwjgl/jogl).

In eclipse you can go to the run options->arguments tab and enter vm arguments.