LWJGL 3 glfwSetWindowIcon does not work because I didn't use directBuff [solved]

Hello,

I somehow got informed that GLFW 3.2 added support for icons - and that this should work in LWJGL 3.0 too (right?)… I tried to implement it and failed miserably.

Facts: Windows 10 64bit, LWJGL 3.0 (using 64bit Java 1.8, therefore 64bit natives: latest release)

Following code does not work:


    public static void setIcon(long window, BufferedImage img) {
        GLFWImage image = GLFWImage.malloc();
        image.set(img.getWidth(), img.getHeight(), loadImageToByteBuffer(img));

        GLFWImage.Buffer images = GLFWImage.malloc(1);
        images.put(0, image);

        GLFW.glfwSetWindowIcon(window, images);

        images.free();
        image.free();
    }

    private static ByteBuffer loadImageToByteBuffer(final BufferedImage image) {
        final byte[] buffer = new byte[image.getWidth() * image.getHeight() * 4];
        int counter = 0;
        for (int i = 0; i < image.getHeight(); i++) {
            for (int j = 0; j < image.getWidth(); j++) {
                final int c = image.getRGB(j, i);
                buffer[counter + 0] = (byte) (c << 8 >> 24);
                buffer[counter + 1] = (byte) (c << 16 >> 24);
                buffer[counter + 2] = (byte) (c << 24 >> 24);
                buffer[counter + 3] = (byte) (c >> 24);
                counter += 4;
            }
        }
        return ByteBuffer.wrap(buffer);
    }

Note: the loadImageToByteBuffer used to work with LWJGL 2.9.x in some ways, so I had icons on my application prior to the switch to 3.0 :wink:

I already searched around and the setIcon is actually from a stackoverflow (apparently working) answer.

The feedback I get when it crashes is that the LWJGLs checkPointer method from class Checks returns null after the GLFWImage validate method returns 0 as the pointer (although it is called with a pointer thats at least not 0) - this happens in the glfwSetWindowIcon method.

The image I try to set is a 48x48 PNG with alpha and is loaded via ImageIO.

Need help

Thank you very much
Matt_P