I am learning the example of lwjgl 0.92, the spaceinvaders.I try to combine 3 alien sprite images into one png file.It’s very useful, isn’t it?
the code is listed below.
my problem is:the first frame is correct, and the second is wrong, it’s actually a mixture of the first and the second sprite. the third is wrong either. it’s a mixture of all sprite.
if I add a statement like g.clearRect(…)(see the code below), it works, the animate can play correctly, but transparent lost.
does anybody can tell me why, and how to solve this problem. thanks.
public Texture getFrameTexture(String resourceName, int width, int height,
int frame) throws IOException {
Texture tex = (Texture) table.get(resourceName+frame);
if (tex != null) {
return tex;
}
BufferedImage bufferedImage = loadImage(resourceName);
int totalFrame = bufferedImage.getWidth()/width;
if(frame >= totalFrame){
frame = 0;
tex = (Texture) table.get(resourceName+frame);
if (tex != null) {
return tex;
}
}
WritableRaster raster;
BufferedImage[] frames = new BufferedImage[totalFrame];
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,width, height,4,null);
for(int i= 0; i < totalFrame; i ++){
frames[i] = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable());
}
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,width, height,3,null);
for(int i= 0; i < totalFrame; i ++){
frames[i] = new BufferedImage(glColorModel,raster,false,new Hashtable());
}
}
//??????????????????????????????????????????????????????
// problem is here?????????????????????????????????????????
Graphics g=null;
for(int i = 0; i < totalFrame; i ++){
g = frames[i].getGraphics();
//g.clearRect(0,0,width, height); //it works, but transparent lost.
g.drawImage(bufferedImage, -i*width, 0, null);
tex = getTexture(frames[i],
GL11.GL_TEXTURE_2D, // target
GL11.GL_RGBA, // dst pixel format
GL11.GL_LINEAR, // min filter (unused)
GL11.GL_LINEAR);
table.put(resourceName+i,tex);
}
//end problem????????????????????????????????????????????
return (Texture) table.get(resourceName+frame);
}