transparent png and Graphics.drawImage method

Hi,

I have a transparent PNG (640x320) file holding all tiles used in my game (each tile is 32x32 and has an id based on the position in the big PNG file.

I load this image into a Image object and then use the following method to draw individual tiles:

public abstract boolean drawImage(Image img, int dx1, int dy1,int dx2,int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer).

But the speed is very low, the frame rate makes it impossible to play. When I change to use the following drawImage variation:

public abstract boolean drawImage(Image img, int dx1, int dy1,int dx2,int dy2, int sx1, int sy1, int sx2, int sy2, Color color, ImageObserver observer).

The speed keeps very good (60 FPS). The problem is that the above drawImage will fill transparent portions of the image with color passed as an argument and I do not want that behaviour because I'm using another image as a background (parallax scrolling).

Do you guys know why there is this diference in speed of these two methods? And do you have a solution to keep the transparency?

Thanks a lot,

L. F. Estivalet

[1149 byte] By [luisofta] at [2007-10-3 10:00:40]
# 1

I made a tile-based game with 3 layers of tiles(with transparency) and it runs extremely fast.

1. create a hashtable

2. store all the 32x32 tiles in the hashtable with the key being their index.

3. when drawing a square just call drawImage((hash.get(myIndex))

To get the 32x32 into the hashtable, load your tileset as a BufferedImage and use a double for-loop with bufferedimage.getSubimage(x,y,width,height) to extract all the tiles.

Good Luck

Nethera at 2007-7-15 5:19:17 > top of Java-index,Other Topics,Java Game Development...