From 54c854bec69e89e1bb08e3e8d5789f271e2aa751 Mon Sep 17 00:00:00 2001 From: Fennel Date: Sun, 4 Dec 2016 02:43:27 +0100 Subject: [PATCH] Fixes mouse cursor format for LWJGL3 --- .../com/jme3/input/lwjgl/GlfwMouseInput.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java index f9081f731..7e9f70f2d 100644 --- a/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java +++ b/jme3-lwjgl3/src/main/java/com/jme3/input/lwjgl/GlfwMouseInput.java @@ -238,17 +238,35 @@ public class GlfwMouseInput implements MouseInput { return (long) (glfwGetTime() * 1000000000); } - private long createGlfwCursor(JmeCursor jmeCursor) { - GLFWImage glfwImage = new GLFWImage(BufferUtils.createByteBuffer(GLFWImage.SIZEOF)); + private ByteBuffer transformCursorImage(IntBuffer imageData, int w, int h) { + ByteBuffer buf = BufferUtils.createByteBuffer(imageData.capacity() * 4); + + // Transform image: ARGB -> RGBA, vertical flip + for (int y = h-1; y >= 0; --y) { + for (int x = 0; x < w; ++x) { + int pixel = imageData.get(y*w + x); + buf.put((byte) ((pixel >> 16) & 0xFF)); // red + buf.put((byte) ((pixel >> 8) & 0xFF)); // green + buf.put((byte) ( pixel & 0xFF)); // blue + buf.put((byte) ((pixel >> 24) & 0xFF)); // alpha + } + } + buf.flip(); + return buf; + } + + private long createGlfwCursor(JmeCursor jmeCursor) { // TODO: currently animated cursors are not supported IntBuffer imageData = jmeCursor.getImagesData(); - ByteBuffer buf = BufferUtils.createByteBuffer(imageData.capacity() * 4); - buf.asIntBuffer().put(imageData); + ByteBuffer buf = transformCursorImage(imageData, jmeCursor.getWidth(), jmeCursor.getHeight()); + GLFWImage glfwImage = new GLFWImage(BufferUtils.createByteBuffer(GLFWImage.SIZEOF)); glfwImage.set(jmeCursor.getWidth(), jmeCursor.getHeight(), buf); - return glfwCreateCursor(glfwImage, jmeCursor.getXHotSpot(), jmeCursor.getYHotSpot()); + int hotspotX = jmeCursor.getXHotSpot(); + int hotspotY = jmeCursor.getHeight() - jmeCursor.getYHotSpot(); + return glfwCreateCursor(glfwImage, hotspotX, hotspotY); } public void setNativeCursor(JmeCursor jmeCursor) {