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.