Mouse.setNativeCursor

Started by Uli, November 17, 2003, 19:44:12

Previous topic - Next topic

Uli

I tried to create a cursor for Mouse.setNativeCursor().  The 6th attribute in the constructor of the cursor class is an IntBuffer containing the image(s) for the cursor.  I'm wondering in which format these images should be. All my attempts failed, I only got NullPointerExceptions. Does anyone have a hint or some simple example code?

princec

Needs to be a native IntBuffer, with data in ARGB format I think. I've not used it yet. Maybe Elias can shed some more light on it.

Post up a code fragment too so.

Cas :)

cfmdobbie

Take a look at [this discussion].  Basically, it's an IntBuffer, with each pixel's data packed into a single int in ARGB format.

Here's a fragment of code for you, but bear in mind it was written for 0.7-pre2 and may need a few tweaks to get running.  Also, I use a custom Image object which isn't included here - but it's basically just a wrapper for a pixel format (GL_RGBA) and a ByteBuffer of pixel data.

public NativeCursor(Image image, int xhotspot, int yhotspot)
		throws Exception
	{
		if(image.getPixelFormat() != GL.GL_RGBA)
			throw new ImageException("Unsupported pixel format") ;

		int width = image.getWidth() ;
		int height = image.getHeight() ;

		/* Note that the IntBuffer contains packed pixel data,
		 * so one int contains all four pixel components.
		 */
		IntBuffer intbuf =
			ByteBuffer.allocateDirect(width * height * 4 /* sizeof(int) */)
			.order(ByteOrder.nativeOrder())
			.asIntBuffer() ;

		ByteBuffer pixels = image.getPixels() ;
		pixels.position(0).limit(width * height * 4 /* RGBA: four pixel components*/) ;
		
		byte[] inPixel = new byte[4] ;
		for(int i = 0 ; i < (width * height) ; i++)
		{
			int outPixel = 0x00000000 ; // AARRGGBB

			inPixel[0] = pixels.get() ; // RR
			inPixel[1] = pixels.get() ; // GG
			inPixel[2] = pixels.get() ; // BB
			inPixel[3] = pixels.get() ; // AA

			outPixel |= inPixel[3] << 24 ; // AA
			outPixel |= inPixel[0] << 16 ; // RR
			outPixel |= inPixel[1] << 8 ; // GG
			outPixel |= inPixel[2] ; // BB
			
			intbuf.put(outPixel) ;
		}
		intbuf.flip() ;

		cursor = new Cursor(
			width, height,
			xhotspot, yhotspot,
			1,
			intbuf,
			null) ;
	}


I personally dropped the idea of using native cursors as my hardware just doesn't like them one bit.  In fact this code has never actually been seen to work, but it should be fine! :D
ellomynameis Charlie Dobbie.

Uli

Thanks, it works! My hardware seems to have no problem with native cursors.
I had the idea of building a screensaver with lwjgl, using JET to compile to an exe. I need the native cursor for the configure dialog. Do you think this is a stupid idea?

princec

It's kinda off-the-wall :)

Cas :)

cfmdobbie

An interesting idea, and I think LWJGL should be very well suited for it!  I don't suppose it's as easy as renaming the Jet EXE (you need the right compiler flags to write screensaver-compatable apps, don't you?) but I don't know anything about Jet, so this may not be an issue.

Even so, I expect with a bit of Win32 knowledge you could come up with a wrapper application to chain your Jet-compiled LWJGL application when the screensaver kicks in, if necessary.

Cas, you should think about this for a moment - with a little work you could produce demo screensavers that run in the same engine the game uses.  That's a pretty good marketting hook, if you ask me...
ellomynameis Charlie Dobbie.

princec

I think you can get Jet to compile to a DLL, which you could then call from a stub .scr program.

There ain't really an engine in Alien Flux... it just sort of "does".

Cas :)

cfmdobbie

Well okay, but if you get that stub available to LWJGL users, you open up a whole new avenue of interest with the library - those who aren't really interested in writing complete applications, but enjoy implementing interesting graphical effects.  Might be worthwhile, if it's really that easy?

Just out of interest - how much work would it be to create an AF-alike screensaver with your current codebase?  A single screen (with background), 8 fluffies, 8 bubbles, make Jellified Fluffies race off-screen and disappear; reset everything when all the fluffies are dead.  Something like that.
ellomynameis Charlie Dobbie.

princec

Far more work than I can be bothered with :)
If I'm going to do anything to Alien Flux now, it'll be either:
1. Adding the credit card purchase thing
or 2. Adding the special bonus levels and a few new rare gidrahs

Cas :)

Uli

Writing a windows screensaver is very easy. You can take any .exe file, rename it to .scr and put it into the windows directory. Okay, there are some things to take care of. Of cause the program should terminate when a key is hit or the mouse is moved. At the beginning of the program the command line parameters must be checked, /c means show the configure dialog, /s is "normal" screen-saving mode. There's also a parameter for preview mode, but that would be hard to implement I think, because there's also a parameter which contains a window handle for the preview window. But that's it, a .scr is just a renamed .exe. I didn't test it with JET yet, I will try when I find the time. For now I have to do my main work for this f***ing insurance company.