| 
      
      
      From: <eli...@us...> - 2008-04-01 19:46:29
      
     | 
| Revision: 2966
          http://java-game-lib.svn.sourceforge.net/java-game-lib/?rev=2966&view=rev
Author:   elias_naur
Date:     2008-04-01 12:46:20 -0700 (Tue, 01 Apr 2008)
Log Message:
-----------
Linux: Added alpha mask support to Display.setIcon
Modified Paths:
--------------
    trunk/LWJGL/src/java/org/lwjgl/opengl/LinuxDisplay.java
    trunk/LWJGL/src/native/linux/org_lwjgl_opengl_Display.c
Modified: trunk/LWJGL/src/java/org/lwjgl/opengl/LinuxDisplay.java
===================================================================
--- trunk/LWJGL/src/java/org/lwjgl/opengl/LinuxDisplay.java	2008-03-23 15:51:10 UTC (rev 2965)
+++ trunk/LWJGL/src/java/org/lwjgl/opengl/LinuxDisplay.java	2008-04-01 19:46:20 UTC (rev 2966)
@@ -962,8 +962,7 @@
 		throw new UnsupportedOperationException();
 	}
 	
-	private static ByteBuffer convertIcon(ByteBuffer icon, int width, int height) {
-		ByteBuffer icon_copy = BufferUtils.createByteBuffer(icon.capacity());
+	private static void convertIcon(ByteBuffer icon, int width, int height, ByteBuffer icon_rgb, ByteBuffer icon_mask) {
 		int x = 0;
 		int y = 5;
 		byte r,g,b,a;
@@ -977,13 +976,14 @@
 				b = icon.get((x*4)+(y*width*4)+2);
 				a = icon.get((x*4)+(y*width*4)+3);
 
-				icon_copy.put((x*depth)+(y*width*depth), b); // blue
-				icon_copy.put((x*depth)+(y*width*depth)+1, g); // green
-				icon_copy.put((x*depth)+(y*width*depth)+2, r);
-				icon_copy.put((x*depth)+(y*width*depth)+3, a);
+				icon_rgb.put((x*depth)+(y*width*depth), b); // blue
+				icon_rgb.put((x*depth)+(y*width*depth)+1, g); // green
+				icon_rgb.put((x*depth)+(y*width*depth)+2, r);
+				icon_mask.put((x*depth)+(y*width*depth), a);
+				icon_mask.put((x*depth)+(y*width*depth)+1, a);
+				icon_mask.put((x*depth)+(y*width*depth)+2, a);
 			}
 		}
-		return icon_copy;
 	}
 
 	/**
@@ -1006,8 +1006,11 @@
 				for (int i=0;i<icons.length;i++) {
 					int size = icons[i].limit() / 4;
 					int dimension = (int)Math.sqrt(size);
-					ByteBuffer icon = convertIcon(icons[i], dimension, dimension);
-					nSetWindowIcon(getDisplay(), getWindow(), icon, icon.capacity(), dimension, dimension);
+					int cap = icons[i].capacity();
+					ByteBuffer icon_rgb = BufferUtils.createByteBuffer(cap);
+					ByteBuffer icon_mask = BufferUtils.createByteBuffer(cap);
+					convertIcon(icons[i], dimension, dimension, icon_rgb, icon_mask);
+					nSetWindowIcon(getDisplay(), getWindow(), icon_rgb, icon_mask, cap, dimension, dimension);
 					return 1;
 				}
 				return 0;
@@ -1022,7 +1025,7 @@
 		}
 	}
 	
-	private static native void nSetWindowIcon(long display, long window, ByteBuffer icon, int icons_size, int width, int height);
+	private static native void nSetWindowIcon(long display, long window, ByteBuffer icon_rgb, ByteBuffer icon_mask, int icon_size, int width, int height);
 
 	public int getWidth() {
 		return Display.getDisplayMode().getWidth();
Modified: trunk/LWJGL/src/native/linux/org_lwjgl_opengl_Display.c
===================================================================
--- trunk/LWJGL/src/native/linux/org_lwjgl_opengl_Display.c	2008-03-23 15:51:10 UTC (rev 2965)
+++ trunk/LWJGL/src/native/linux/org_lwjgl_opengl_Display.c	2008-04-01 19:46:20 UTC (rev 2966)
@@ -72,6 +72,7 @@
 static Colormap cmap;
 static int current_depth;
 static Pixmap current_icon_pixmap;	
+static Pixmap current_icon_mask_pixmap;	
 
 static Visual *current_visual;
 
@@ -185,6 +186,10 @@
 }
 
 static void freeIconPixmap(Display *disp) {
+	if (current_icon_mask_pixmap != 0) {
+		XFreePixmap(disp, current_icon_mask_pixmap);
+		current_icon_mask_pixmap = 0;
+	}
 	if (current_icon_pixmap != 0) {
 		XFreePixmap(disp, current_icon_pixmap);
 		current_icon_pixmap = 0;
@@ -349,33 +354,46 @@
 	jawt.Unlock(env);
 }
 
-static void setIcon(JNIEnv *env, Display *disp, Window window, char *data, int icon_size, int width,int height) {
-	XWMHints* win_hints;
-	freeIconPixmap(disp);
-	current_icon_pixmap = XCreatePixmap(disp, window, width, height, current_depth);
+static Pixmap createPixmapFromBuffer(JNIEnv *env, Display *disp, Window window, char *data, int data_size, int width, int height) {
+	Pixmap pixmap = XCreatePixmap(disp, window, width, height, current_depth);
 	/* We need to copy the image data since XDestroyImage will also free its data buffer, which can't be allowed
 	 * since the data buffer is managed by the jvm (it's the storage for the direct ByteBuffer)
 	 */
-	char *icon_copy = (char *)malloc(sizeof(*icon_copy)*icon_size);
+	char *icon_copy = (char *)malloc(sizeof(*icon_copy)*data_size);
 
 	if (icon_copy == NULL) {
+		XFreePixmap(disp, pixmap);
 		throwException(env, "malloc failed");
-		return;
+		return 0;
 	}
-	memcpy(icon_copy, data, icon_size);
+	memcpy(icon_copy, data, data_size);
 	XImage *image = XCreateImage(disp, current_visual, current_depth, ZPixmap, 0, icon_copy, width, height, 32, 0);
 	if (image == NULL) {
-		freeIconPixmap(disp);
+		XFreePixmap(disp, pixmap);
 		free(icon_copy);
 		throwException(env, "XCreateImage failed");
-		return;
+		return 0;
 	}
 	
-	GC gc = XCreateGC(disp, current_icon_pixmap, 0, NULL);
-	XPutImage(disp, current_icon_pixmap, gc, image, 0, 0, 0, 0, width, height);
+	GC gc = XCreateGC(disp, pixmap, 0, NULL);
+	XPutImage(disp, pixmap, gc, image, 0, 0, 0, 0, width, height);
 	XFreeGC(disp, gc);
 	XDestroyImage(image);
 	// We won't free icon_copy because it is freed by XDestroyImage
+	return pixmap;
+}
+
+static void setIcon(JNIEnv *env, Display *disp, Window window, char *rgb_data, char *mask_data, int icon_size, int width, int height) {
+	XWMHints* win_hints;
+	freeIconPixmap(disp);
+	current_icon_pixmap = createPixmapFromBuffer(env, disp, window, rgb_data, icon_size, width, height);
+	if ((*env)->ExceptionCheck(env))
+		return;
+	current_icon_mask_pixmap = createPixmapFromBuffer(env, disp, window, mask_data, icon_size, width, height);
+	if ((*env)->ExceptionCheck(env)) {
+		freeIconPixmap(disp);
+		return;
+	}
 	
 	win_hints = XAllocWMHints();
 	if (win_hints == NULL) {
@@ -383,8 +401,9 @@
 		return;
 	}
 	
-	win_hints->flags = IconPixmapHint;               
+	win_hints->flags = IconPixmapHint | IconMaskHint;
 	win_hints->icon_pixmap = current_icon_pixmap;
+	win_hints->icon_mask = current_icon_mask_pixmap;
 	
 	XSetWMHints(disp, window, win_hints);
 	XFree(win_hints);
@@ -392,13 +411,14 @@
 }
 
 JNIEXPORT void JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nSetWindowIcon
-  (JNIEnv *env, jclass clazz, jlong display, jlong window_ptr, jobject iconBuffer, jint icon_size, jint width, jint height)
+  (JNIEnv *env, jclass clazz, jlong display, jlong window_ptr, jobject icon_rgb_buffer, jobject icon_mask_buffer, jint icon_size, jint width, jint height)
 {
 	Display *disp = (Display *)(intptr_t)display;
 	Window window = (Window)window_ptr;
-	char *imgData = (char *)(*env)->GetDirectBufferAddress(env, iconBuffer);
+	char *rgb_data= (char *)(*env)->GetDirectBufferAddress(env, icon_rgb_buffer);
+	char *mask_data= (char *)(*env)->GetDirectBufferAddress(env, icon_mask_buffer);
 
-	setIcon(env, disp, window, imgData, icon_size, width, height);
+	setIcon(env, disp, window, rgb_data, mask_data, icon_size, width, height);
 }
 
 JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_LinuxDisplay_nUngrabKeyboard(JNIEnv *env, jclass unused, jlong display_ptr) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |