Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] LWJGL 3 - glfwCreateCursor returning null  (Read 2961 times)
0 Members and 1 Guest are viewing this topic.
Offline ThePhotonicPizza

Senior Newbie


Medals: 4
Exp: 3 years



« Posted 2016-04-29 21:25:54 »

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:

1  
2  
3  
4  
5  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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:

1  
2  
3  
4  
5  
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.
Offline KaiHH

JGO Kernel


Medals: 616



« Reply #1 - Posted 2016-04-29 21:33:47 »

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.
Offline ThePhotonicPizza

Senior Newbie


Medals: 4
Exp: 3 years



« Reply #2 - Posted 2016-04-29 21:37:29 »

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!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 616



« Reply #3 - Posted 2016-04-29 21:40:56 »

Oh, wow, that seems a bit unnecessary.
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.
Offline ThePhotonicPizza

Senior Newbie


Medals: 4
Exp: 3 years



« Reply #4 - Posted 2016-04-29 21:53:42 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1572 views)
2018-06-10 19:43:48

EgonOlsen (1656 views)
2018-06-10 19:43:44

EgonOlsen (1145 views)
2018-06-10 19:43:20

DesertCoockie (1573 views)
2018-05-13 18:23:11

nelsongames (1174 views)
2018-04-24 18:15:36

nelsongames (1664 views)
2018-04-24 18:14:32

ivj94 (2421 views)
2018-03-24 14:47:39

ivj94 (1631 views)
2018-03-24 14:46:31

ivj94 (2719 views)
2018-03-24 14:43:53

Solater (884 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!