5
\$\begingroup\$

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).

\$\endgroup\$
  • \$\begingroup\$ I'm surprised that this hasn't been asked before. \$\endgroup\$ – Lolums Aug 13 '15 at 10:12
  • \$\begingroup\$ You could also use a jar bundler to create a .app and .exe with your icon. \$\endgroup\$ – Addi Aug 13 '15 at 10:22
  • \$\begingroup\$ Now that LWJGL3 uses GLFW for window management, it is currently impossible to set the window icon directly with LWJGL. However, it is being worked on and will probably come in GLFW 3.2. \$\endgroup\$ – javac Sep 2 '15 at 17:57
1
\$\begingroup\$

They added it to build 79, the method is called: glfwSetWindowIcon(long window, Buffer images);

\$\endgroup\$
2
\$\begingroup\$

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;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.