NoRelect NoRelect - 5 months ago 23
Java Question

GLFW_PLATFORM_ERROR when using glfwSetWindowIcon()

I currently coding a Project using LWJGL 3. Since GLFW 3.2 its possible to add a window icon. I used it like you can see below, but I keep getting the error, and I have no clue why. What am I doing wrong?

What I have tried already:


  • I have made sure that the file is present in "res/img/icon.png".

  • The PNGLoader (Found here) works, as I am using textures and it loads them without any problems.



Code:

PNGDecoder dec = null;
try {
dec = new PNGDecoder(new FileInputStream("res/img/icon.png"));
int width = dec.getWidth();
int height = dec.getHeight();
ByteBuffer buf = BufferUtils.createByteBuffer(width * height * 4);
dec.decode(buf, width * 4, PNGDecoder.Format.RGBA);
buf.flip();
Buffer images = new Buffer(buf);
GLFW.glfwSetWindowIcon(window, images);
} catch (IOException e) {
e.printStackTrace();
}


Here's the error that I get:

[LWJGL] GLFW_PLATFORM_ERROR error
Description : Win32: Failed to create RGBA bitmap
Stacktrace :
org.lwjgl.glfw.GLFW.nglfwSetWindowIcon(GLFW.java:1802)
org.lwjgl.glfw.GLFW.glfwSetWindowIcon(GLFW.java:1829)
ch.norelect.emulator.designer.Designer.initialize(Designer.java:81)
ch.norelect.emulator.designer.Designer.start(Designer.java:48)
ch.norelect.emulator.Main.main(Main.java:27)

Answer

You are passing the raw pixel data as a GLFWImage.Buffer, but GLFW needs the width and height as well. GLFWImage.Buffer is just a convenience class for putting several GLFWImages next to each other, which each hold properties about an image.

GLFWImage image = GLFWImage.malloc();
image.set(width, height, buf);
GLFWImage.Buffer images = GLFWImage.malloc(1);
images.put(0, image);

glfwSetWindowIcon(window, images);

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