Hey,
So I’m revisiting the tower defense game I started making some time back, and decided to give it another whirl in Java2D, while waiting for LWJGL to hit 1.9.0 and be usable on OS X with Java 7.
The reason I stopped working on it, was that the rendering became horribly slow after placing ~110 towers. This time around I did a little digging and found out that it’s because I rotate my tower images that it starts to get slow. I rotate them, so that they’re always facing the nearest enemy they can reach.
I use the following code to render each tower:
public void render(Graphics2D g2d) {
// Turret base shadow
g2d.drawImage(this.actor.getImage(1), this.pos.x - 16 + 1, this.pos.y - 16 + 1, null);
// Turret base
g2d.drawImage(this.actor.getImage(0), this.pos.x - 16, this.pos.y - 16, null);
AffineTransform tx = new AffineTransform();
tx.rotate(this.rotation, 16, 16);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// Turret shadow
g2d.drawImage(op.filter(this.actor.getImage(5), null), this.pos.x - 16 + 2, this.pos.y - 16 + 2, null);
// Turret
g2d.drawImage(op.filter(this.actor.getImage(this.towerSprite), null), this.pos.x - 16, this.pos.y - 16, null);
}
When rendering 166 towers that way(Each tower renders itself, btw), the fps has gone from 60 to ~34, when I remove the rotating of the images, then it has no problem rendering the towers at 60fps.
Soo, golden question, how can I optimize this?