LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: elias4444 on May 23, 2006, 16:06:18

Title: Display.setIcon on Macs
Post by: elias4444 on May 23, 2006, 16:06:18
Well, I got my Macbook Pro finally!  :D

I noticed however, that when I use the display.setIcon() command, it doesn't seem to work, although it works on windows and linux for me. Here's the code I'm using:

try {
if (System.getProperty("os.name").startsWith("Mac OS")) {
ByteBuffer[] icons = new ByteBuffer[1];
BufferedImage bufferedImage1 = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("mainmenu/images/icon128.png")));
icons[0] = convertImageData(bufferedImage1);
Display.setIcon(icons);
} else {
ByteBuffer[] icons = new ByteBuffer[3];
BufferedImage bufferedImage1 = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("mainmenu/images/icon16.png")));
BufferedImage bufferedImage2 = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("mainmenu/images/icon32.png")));
BufferedImage bufferedImage3 = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("mainmenu/images/icon128.png")));
icons[2] = convertImageData(bufferedImage1);
icons[1] = convertImageData(bufferedImage2);
icons[0] = convertImageData(bufferedImage3);
Display.setIcon(icons);
}
} catch (IOException e1) {
e1.printStackTrace();
}


Title: Display.setIcon on Macs
Post by: elias4444 on May 23, 2006, 16:10:39
and here's the convertImageData method I'm using (got it somewhere off this forum):

private ByteBuffer convertImageData(BufferedImage bufferedImage) {
ColorModel glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,8},
true,
false,
ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);

ColorModel glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,0},
false,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);

ByteBuffer imageBuffer = null;
WritableRaster raster;
BufferedImage texImage;

int texWidth = 2;
int texHeight = 2;

// find the closest power of 2 for the width and height
// of the produced texture
while (texWidth < bufferedImage.getWidth()) {
texWidth *= 2;
}
while (texHeight < bufferedImage.getHeight()) {
texHeight *= 2;
}

// create a raster that can be used by OpenGL as a source
// for a texture
if (bufferedImage.getColorModel().hasAlpha()) {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,4,null);
texImage = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable());
} else {
raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null);
texImage = new BufferedImage(glColorModel,raster,false,new Hashtable());
}

// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f,0f,0f,0f));
g.fillRect(0,0,texWidth,texHeight);
g.drawImage(bufferedImage,0,0,null);

// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();

imageBuffer = ByteBuffer.allocateDirect(data.length);
imageBuffer.order(ByteOrder.nativeOrder());
imageBuffer.put(data, 0, data.length);
imageBuffer.flip();

return imageBuffer;
}
Title: Display.setIcon on Macs
Post by: elias4444 on May 23, 2006, 16:18:35
I should probably also mention, I'm assuming that the display.seticon method will change the dock's icon (since there is no "window icon" technically). If that's incorrect, please let me know.