Writing

Drawing Sprite Speed Test

In an attempt to make my code a bit more elegant, robust, and fast, I've started review my newly created Sprite class in detail. I included percentage resizing and alpha blending. I chose an easy way to alpha blend but had two choices on how to resize my images. I started to become curious:

What is the best way to draw my sprites in Java 2D?

I turned off my sleep timer in the game loop and then drew a 59x448 pixel index image on to my canvas. Note I tried the same image as RGB and had scary slow downs. Making sure your art work is all indexed will be your greatest speed boost by far.

The results where interesting.

Resizing causes a much greater hit than alpha blending.
drawImage(img, destination, source) was slightly faster than drawImage(img, destination, size)

Here are the details in milliseconds over 10 seconds.

// As fast a you can get. Used whenever the others are not required.
g2d.drawImage(buffImg, 40, 40, null);

Result: 1381.4

1375
1390
1378
1378
1386


// Slowest draw. Half size, setting width from 448 to 224, and height from 59 to 30.
g2d.drawImage(buffImg, 40, 40, 224, 30, null);

Result: 1313.6

1292
1374
1298
1290
1314

Result second test: 1305.4
1310
1298
1307
1302
1310

// Oddly enough faster than the above method.  This is what I use.
       g2d.drawImage(buffImg,
                40, 40, 40+224, 40+30,   // destination, scalable
                0, 0, 448, 59,  //source on sprite
                null);

Results: 1309.8
1303
1307
1301
1311
1327

Results: 1313
1320
1286
1319
1339
1301

// Sets alpha blending to 50 percent. Not as bad as expeced but changing the composite 100 times would be terrible.
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2d.drawImage(buffImg, 40, 40, null);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));

Results: 1366.2
1360
1376
1364
1368
1363

0 comments:

Post a Comment

Thanks for the comment.