What's the best, cross platform, way of setting the window icon using LWJGL 3?
I have a PNG image and would like to set it to the icon present in my dock (OS X) or the application bar (Linux).
What's the best, cross platform, way of setting the window icon using LWJGL 3?
I have a PNG image and would like to set it to the icon present in my dock (OS X) or the application bar (Linux).
They added it to build 79, the method is called: glfwSetWindowIcon(long window, Buffer images);
I have found an answer!!! After searching far and wide through the depths of the internet, I have found the following method. It works perfectly!
public void setIcon(String path) throws Exception{
IntBuffer w = memAllocInt(1);
IntBuffer h = memAllocInt(1);
IntBuffer comp = memAllocInt(1);
// Icons
{
ByteBuffer icon16;
ByteBuffer icon32;
try {
icon16 = ioResourceToByteBuffer(path, 2048);
icon32 = ioResourceToByteBuffer(path, 4096);
} catch (Exception e) {
throw new RuntimeException(e);
}
try ( GLFWImage.Buffer icons = GLFWImage.malloc(2) ) {
ByteBuffer pixels16 = STBImage.stbi_load_from_memory(icon16, w, h, comp, 4);
icons
.position(0)
.width(w.get(0))
.height(h.get(0))
.pixels(pixels16);
ByteBuffer pixels32 = STBImage.stbi_load_from_memory(icon32, w, h, comp, 4);
icons
.position(1)
.width(w.get(0))
.height(h.get(0))
.pixels(pixels32);
icons.position(0);
glfwSetWindowIcon(window, icons);
STBImage.stbi_image_free(pixels32);
STBImage.stbi_image_free(pixels16);
}
}
memFree(comp);
memFree(h);
memFree(w);
}
And the "ioResourceToByteBuffer" method is in a class that contains this:
private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) {
ByteBuffer newBuffer = BufferUtils.createByteBuffer(newCapacity);
buffer.flip();
newBuffer.put(buffer);
return newBuffer;
}
/**
* Reads the specified resource and returns the raw data as a ByteBuffer.
*
* @param resource the resource to read
* @param bufferSize the initial buffer size
*
* @return the resource data
*
* @throws IOException if an IO error occurs
*/
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
ByteBuffer buffer;
Path path = Paths.get(resource);
if ( Files.isReadable(path) ) {
try (SeekableByteChannel fc = Files.newByteChannel(path)) {
buffer = BufferUtils.createByteBuffer((int)fc.size() + 1);
while ( fc.read(buffer) != -1 ) ;
}
} else {
try (
InputStream source = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
ReadableByteChannel rbc = Channels.newChannel(source)
) {
buffer = BufferUtils.createByteBuffer(bufferSize);
while ( true ) {
int bytes = rbc.read(buffer);
if ( bytes == -1 )
break;
if ( buffer.remaining() == 0 )
buffer = resizeBuffer(buffer, buffer.capacity() * 2);
}
}
}
buffer.flip();
return buffer;
}