[SOLVED] LWJGL 3 - glfwCreateCursor returning null

I’m trying to create a custom cursor for my window. However, I constantly run into a problem, which is that glfwCreateCursor returns NULL. My code looks like this:


ByteBuffer buffer = FileUtils.imageToByteBuffer(ImageIO.read(new File("../res/cursor.png")));
GLFWImage cursorimg = new GLFWImage(buffer);
long cursorID = glfwCreateCursor(cursorimg, 0, 0);

Mouse.setCursor(cursorID)

Here’s the custom method for the image-to-buffer conversion, BTW:


public static ByteBuffer imageToByteBuffer(BufferedImage image)
{
	int[] pixels = new int[image.getWidth() * image.getHeight()];
	image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

	ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4);

	for (int y = 0; y < image.getHeight(); y++)
	{
		for (int x = 0; x < image.getWidth(); x++)
		{
			int pixel = pixels[y * image.getWidth() + x];
			buffer.put((byte) ((pixel >> 16)    & 0xFF));
			buffer.put((byte) ((pixel >> 8)     & 0xFF));
			buffer.put((byte) ((pixel)          & 0xFF));
			buffer.put((byte) ((pixel >> 24)    & 0xFF));
		}
	}

	buffer.flip();

	return buffer;
}

And here’s the one for setting the cursor itself:


public static void setCursor(long cursor)
{
	if (currCursor != 0) glfwDestroyCursor(currCursor);
	glfwSetCursor(Window.id(), currCursor = cursor);
}

I ran it through my debugger, and the cursor.png image loads correctly, the error happens either when I put the data in the buffer, when I create the GLFWImage, or when I create the cursor object itself.

Anyone know what could be causing this issue?

I’m using LWJGL 3 build #79 on Ubuntu 14.04, by the way.

In the constructor GLFWImage(ByteBuffer) the ByteBuffer argument is not meant to be the image. It is meant to be the native memory of the whole GLFWImage structure. Instead, use the no-args constructor and use GLFWImage.pixels() as well as .width() and .height() to specify the image data.

Oh, wow, that seems a bit unnecessary. But it works. Thanks!

Well, LWJGL 3 provides bindings for the native libraries, and in this case the respective native structures of GLFW in C code. Actually that is very nice, because you simply have to look at an example of the native library in C and can directly map that to LWJGL 3 then.

Yep, know that, it really feels like LWJGL wants to stay as close to native code as possible.

But this is a little issue with that, since the buffer is used as a pointer and, well, the struct itself, which could easily confuse others (like me). I dunno, maybe there’s a specific reason why it had to be done like this, it just seems a little bit all-around-the-place to me. I don’t know C, so I don’t know if it has to be done this way there, too.