Hello Guest

Problems with a custom animated Mouse Cursor in LWJGL

  • 0 Replies
  • 3508 Views
Problems with a custom animated Mouse Cursor in LWJGL
« on: August 05, 2013, 07:31:10 »
Hi all,

I am trying to implement my own mouse cursor animation to my project using org.lwjgl.input.Mouse and org.lwjgl.input.Cursor.

When my own cursor is only made of a single image everything works fine.
As soon as I try to construct a cursor out of multiple images I get this error message:
java.lang.IllegalArgumentException: width*height*numImages > images.remaining()

Now this error doesn't make sense to me, because width*height*numImages is not bigger then images.remainging().
I let it print out and it has the same size (both 16384)

Here is my code:

Code: [Select]
/**
/* Returns a new Cursor.
/* @return new cursor
*/
private static Cursor getCursor() throws IOException, LWJGLException{

     // size of each cursor and amount of different cursor images
     int cursorWidth  = 32;
     int cursorHeight = 32;
     int numImages= 16;

     // load cursor image into BufferedImage
     BufferedImage img = ImageIO.read(new File("data/img/input/mouseCursor.png"));
     int[] rgbs = new int[cursorWidth*cursorHeight];

     // set up IntBuffers
     IntBuffer imageBuffer = IntBuffer.allocate(img.getWidth()*img.getHeight()/16*numImages);
     IntBuffer delays        = IntBuffer.allocate(cursorAmount);

     // put cursor images and delays into IntBuffers
     for( int y = 0; y < img.getHeight()/cursorHeight; y++){
        for (int x = 0; x < img.getWidth()/cursorWidth; x++){
           if( y*(img.getWidth()/cursorWidth)+x < numImages){
              imageBuffer.put(img.getRGB(x*cursorWidth, y*cursorHeight, cursorWidth, cursorHeight, rgbs, 0, cursorWidth));
              delays.put(100);
           }
        }
     }

     // rewind IntBuffers
     imageBuffer.rewind();
     delays.rewind();

     // controll values ("16384 16384")
     System.out.println(cursorWidth*cursorHeight*numImages+" "+imageBuffer.remaining());

     // return cursor
     return new Cursor(cursorWidth,cursorHeight,0,cursorWidth-1,numImages,imageBuffer,delays);
  }

And here is the Cursor Constructor from LWJGL which throws the exception

Code: [Select]
public Cursor(int width, int height, int xHotspot, int yHotspot, int numImages, IntBuffer images, IntBuffer delays) throws LWJGLException {
      synchronized (OpenGLPackageAccess.global_lock) {
         if ((getCapabilities() & CURSOR_ONE_BIT_TRANSPARENCY) == 0)
            throw new LWJGLException("Native cursors not supported");
         images = NondirectBufferWrapper.wrapBuffer(images, width*height*numImages);
         if (delays != null)
            delays = NondirectBufferWrapper.wrapBuffer(delays, numImages);
         if (!Mouse.isCreated())
            throw new IllegalStateException("Mouse must be created before creating cursor objects");
         if (width*height*numImages > images.remaining())
            throw new IllegalArgumentException("width*height*numImages > images.remaining()");
         if (xHotspot >= width || xHotspot < 0)
            throw new IllegalArgumentException("xHotspot > width || xHotspot < 0");
         if (yHotspot >= height || yHotspot < 0)
            throw new IllegalArgumentException("yHotspot > height || yHotspot < 0");

         Sys.initialize();

         // Hmm
         yHotspot = height - 1 - yHotspot;

         // create cursor (or cursors if multiple images supplied)
         cursors = createCursors(width, height, xHotspot, yHotspot, numImages, images, delays);
      }
   }

Thank you!