3
\$\begingroup\$

I have been trying to create a custom mouse cursor in my LWJGL2 application running under Linux and I am almost there. I have implemented the following method that I call right after creating the game window:

public void loadCursor(BufferedImage img) throws LWJGLException
{
    final int w = img.getWidth();
    final int h = img.getHeight();

    int rgbData[] = new int[w * h];

    for (int i = 0; i < rgbData.length; i++)
    {
        int x = i % w;
        int y = h - 1 - i / w; // this will also flip the image vertically

        rgbData[i] = img.getRGB(x, y);
    }

    IntBuffer buffer = BufferUtils.createIntBuffer(w * h);
    buffer.put(rgbData);
    buffer.rewind();

    Cursor cursor = new Cursor(w, h, 2, h - 2, 1, buffer, null);

    Mouse.setNativeCursor(cursor);
}

The resulting cursor is almost perfect, except for a horizontal blank line in the middle.

Note: The image is being flipped vertically in my code because otherwise the cursor is displayed upside down in OpenGL. I have seen other implementations that flip the IntBuffer instead of doing this in the for-loop but they lead to the same result for me.

I don't think it has to do with screen tearing either since I enabled VSync and the blank line is always at 50% of the cursor's height (there is no flickering).

Can anybody tell what I am doing wrong or if my code example is missing something essential in order to find the problem? Thank you!

\$\endgroup\$
  • \$\begingroup\$ I have removed links to no longer valid image urls. You may want to consider re-uploading them to StackExchange's imgur account to avoid this in the future. \$\endgroup\$ – Vaillancourt Aug 22 '18 at 1:17
1
\$\begingroup\$

At first glance, this looks far too complicated. Lwjgl should have something to the effect of 'getmouseposition()'. Call this function every iteration of your game loop, cast or set it as a Vec2d, then use that position to blit or draw the mouse you want. All of your blits should be called every game iteration in some kind of draw loop.

Mind you, I was trying to use Lwjgl in java, and ended up needing opengl and a shader program to discard the background color of the images I was blitting.

As I recall, with opengl there were issues with blitting images of certain sizes - I think the dimensions had to increase by set numbers for it to blit correctly. Since, if I remeber correctly, Lwjgl uses opengl, this could be what your problem is.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

It's not that your code might be wrong but the size of your buffered image. LWJGL imaging uses increments of 2 (2, 4, 8, 16, 32, 64, 128...), so then your image should be just that. I have used your code and it works just fine.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

A guess as to why that line is there is that your flipping algorithm doesn't handle odd/middle lines well. I recommend the classical algorithm for flipping, for(int i = 0;i<length/2;i++) array[i] = array[array.length-i - 1] (psuedocode).

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thank you for the proposal but the error doesn't seem to be my implementation of filling the array. The following code leads to the same problem with that blank line in the middle, only that the cursor is upside down: rgbData[i] = cropped.getRGB(i % w, i / w); \$\endgroup\$ – xoric May 31 '16 at 20:56
  • \$\begingroup\$ Have you tried gl quads? \$\endgroup\$ – bigcodeszzer Jul 10 '16 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.