2003-10-14 10:29:23 -04:00
|
|
|
/*
|
2004-06-12 16:28:34 -04:00
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
2003-10-14 10:29:23 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2004-06-12 16:28:34 -04:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
2003-10-14 10:29:23 -04:00
|
|
|
* its contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* win32 mouse handling.
|
|
|
|
*
|
|
|
|
* @author elias_naur <elias_naur@users.sourceforge.net>
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
|
|
|
|
2003-05-16 14:39:46 -04:00
|
|
|
#include "Window.h"
|
2004-09-22 11:06:27 -04:00
|
|
|
#include "org_lwjgl_input_Cursor.h"
|
2006-07-12 14:32:58 -04:00
|
|
|
#include "org_lwjgl_opengl_WindowsDisplay.h"
|
2003-10-11 12:29:40 -04:00
|
|
|
#include "common_tools.h"
|
2003-05-16 14:39:46 -04:00
|
|
|
|
2006-07-12 14:32:58 -04:00
|
|
|
JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_WindowsDisplay_nCreateCursor
|
2005-02-21 09:46:47 -05:00
|
|
|
(JNIEnv *env, jobject self, jint width, jint height, jint x_hotspot, jint y_hotspot, jint num_images, jobject image_buffer, jint images_offset, jobject delay_buffer, jint delays_offset)
|
2003-05-16 14:39:46 -04:00
|
|
|
{
|
2005-02-21 09:46:47 -05:00
|
|
|
unsigned char col0, col1, col2, col3, col4, col5, col6, col7;
|
|
|
|
unsigned char mask;
|
|
|
|
BITMAPINFO bitmapInfo;
|
|
|
|
|
|
|
|
HBITMAP cursorMask;
|
|
|
|
HCURSOR *cursor_handle;
|
|
|
|
|
|
|
|
HCURSOR cursor = NULL;
|
|
|
|
ICONINFO iconInfo;
|
|
|
|
char *ptrCursorImage;
|
|
|
|
HBITMAP colorDIB;
|
|
|
|
int *srcPtr;
|
|
|
|
char *dstPtr;
|
|
|
|
int bitWidth;
|
|
|
|
int scanlinePad;
|
|
|
|
int imageSize; // Size in bits
|
|
|
|
unsigned char *maskPixels;
|
|
|
|
int pixelCount = 0;
|
|
|
|
int maskCount = 0;
|
2003-05-16 14:39:46 -04:00
|
|
|
HBITMAP colorBitmap;
|
|
|
|
int x, y;
|
2005-02-21 09:46:47 -05:00
|
|
|
jobject handle_buffer = newJavaManagedByteBuffer(env, sizeof(HCURSOR));
|
2003-05-16 14:39:46 -04:00
|
|
|
|
2005-02-21 09:46:47 -05:00
|
|
|
int *pixels;
|
|
|
|
if (handle_buffer == NULL) {
|
|
|
|
throwException(env, "Could not allocate handle");
|
|
|
|
return NULL;
|
2004-12-09 10:36:13 -05:00
|
|
|
}
|
2005-02-21 09:46:47 -05:00
|
|
|
pixels = (int *)(*env)->GetDirectBufferAddress(env, image_buffer) + images_offset;
|
|
|
|
|
|
|
|
memset(&bitmapInfo, 0, sizeof(BITMAPINFO));
|
|
|
|
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
|
|
|
bitmapInfo.bmiHeader.biWidth = width;
|
|
|
|
bitmapInfo.bmiHeader.biHeight = -height;
|
|
|
|
bitmapInfo.bmiHeader.biPlanes = 1;
|
|
|
|
|
|
|
|
bitmapInfo.bmiHeader.biBitCount = 24;
|
|
|
|
bitmapInfo.bmiHeader.biCompression = BI_RGB;
|
|
|
|
|
|
|
|
colorDIB = CreateDIBSection(GetDC(NULL), (BITMAPINFO*)&(bitmapInfo),
|
|
|
|
DIB_RGB_COLORS,
|
|
|
|
(void**)&(ptrCursorImage),
|
|
|
|
NULL, 0);
|
|
|
|
srcPtr = pixels;
|
|
|
|
dstPtr = ptrCursorImage;
|
|
|
|
if (!dstPtr) {
|
2003-05-16 14:39:46 -04:00
|
|
|
throwException(env, "Could not allocate DIB section.");
|
2005-02-21 09:46:47 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (y = 0; y < height; y++ ) {
|
|
|
|
for (x = 0; x < width; x++ ) {
|
|
|
|
dstPtr[2] = (*srcPtr >> 0x10) & 0xFF;
|
|
|
|
dstPtr[1] = (*srcPtr >> 0x08) & 0xFF;
|
|
|
|
dstPtr[0] = *srcPtr & 0xFF;
|
|
|
|
|
|
|
|
srcPtr++;
|
|
|
|
dstPtr += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
colorBitmap = CreateDIBitmap(GetDC(NULL),
|
|
|
|
(BITMAPINFOHEADER*)&bitmapInfo.bmiHeader,
|
|
|
|
CBM_INIT,
|
|
|
|
(void *)ptrCursorImage,
|
|
|
|
(BITMAPINFO*)&bitmapInfo,
|
|
|
|
DIB_RGB_COLORS);
|
|
|
|
|
|
|
|
DeleteObject(colorDIB);
|
2003-05-16 14:39:46 -04:00
|
|
|
|
|
|
|
// Convert alpha map to pixel packed mask
|
2005-02-21 09:46:47 -05:00
|
|
|
bitWidth = width >> 3;
|
|
|
|
scanlinePad = bitWidth & (sizeof(WORD) - 1);
|
|
|
|
imageSize = (bitWidth + scanlinePad)*height; // Size in bits
|
|
|
|
maskPixels = (unsigned char*)malloc(sizeof(unsigned char)*imageSize);
|
2003-05-16 14:39:46 -04:00
|
|
|
memset(maskPixels, 0, imageSize);
|
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
for (x = 0; x < bitWidth; x++) {
|
2005-02-21 09:46:47 -05:00
|
|
|
col0 = (pixels[pixelCount++] & 0x01000000) >> 17;
|
|
|
|
col1 = (pixels[pixelCount++] & 0x01000000) >> 18;
|
|
|
|
col2 = (pixels[pixelCount++] & 0x01000000) >> 19;
|
|
|
|
col3 = (pixels[pixelCount++] & 0x01000000) >> 20;
|
|
|
|
col4 = (pixels[pixelCount++] & 0x01000000) >> 21;
|
|
|
|
col5 = (pixels[pixelCount++] & 0x01000000) >> 22;
|
|
|
|
col6 = (pixels[pixelCount++] & 0x01000000) >> 23;
|
|
|
|
col7 = (pixels[pixelCount++] & 0x01000000) >> 24;
|
|
|
|
mask = col0 | col1 | col2 | col3 | col4 | col5 | col6 | col7;
|
2003-05-16 14:39:46 -04:00
|
|
|
maskPixels[maskCount++] = ~mask; // 1 is tranparant, 0 opaque
|
|
|
|
}
|
|
|
|
|
2005-02-21 09:46:47 -05:00
|
|
|
cursorMask = CreateBitmap(width, height, 1, 1, maskPixels);
|
2003-05-16 14:39:46 -04:00
|
|
|
|
|
|
|
memset(&iconInfo, 0, sizeof(ICONINFO));
|
|
|
|
iconInfo.hbmMask = cursorMask;
|
|
|
|
iconInfo.hbmColor = colorBitmap;
|
|
|
|
iconInfo.fIcon = FALSE;
|
|
|
|
iconInfo.xHotspot = x_hotspot;
|
|
|
|
iconInfo.yHotspot = y_hotspot;
|
|
|
|
cursor = CreateIconIndirect(&iconInfo);
|
|
|
|
DeleteObject(colorBitmap);
|
|
|
|
DeleteObject(cursorMask);
|
2005-02-21 09:46:47 -05:00
|
|
|
free(maskPixels);
|
2003-05-16 14:39:46 -04:00
|
|
|
|
2005-02-21 09:46:47 -05:00
|
|
|
cursor_handle = (HCURSOR *)(*env)->GetDirectBufferAddress(env, handle_buffer);
|
2004-07-25 10:51:28 -04:00
|
|
|
*cursor_handle = cursor;
|
2005-02-21 09:46:47 -05:00
|
|
|
return handle_buffer;
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|
|
|
|
|
2006-07-12 14:32:58 -04:00
|
|
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsDisplay_destroyCursor
|
2005-02-21 09:46:47 -05:00
|
|
|
(JNIEnv *env, jobject self, jobject handle_buffer)
|
2003-05-16 14:39:46 -04:00
|
|
|
{
|
2005-01-18 11:42:31 -05:00
|
|
|
HCURSOR *cursor_handle = (HCURSOR *)(*env)->GetDirectBufferAddress(env, handle_buffer);
|
2004-07-25 10:51:28 -04:00
|
|
|
DestroyCursor(*cursor_handle);
|
2003-05-16 14:39:46 -04:00
|
|
|
}
|