Search in sources :

Example 6 with Cursor

use of org.lwjgl.input.Cursor in project lwjgl by LWJGL.

the class HWCursorTest method initNativeCursors.

private static void initNativeCursors() throws Exception {
    if ((Cursor.getCapabilities() & Cursor.CURSOR_ONE_BIT_TRANSPARENCY) == 0) {
        System.out.println("No HW cursor support!");
        System.exit(0);
    }
    cursors = new Cursor[3];
    int cursorImageCount = 1;
    int cursorWidth = Math.min(64, Cursor.getMaxCursorSize());
    int cursorHeight = cursorWidth;
    IntBuffer cursorImages;
    IntBuffer cursorDelays;
    // Create a single cursor
    // ==================================
    cursorImages = ByteBuffer.allocateDirect(cursorWidth * cursorHeight * cursorImageCount * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
    cursorDelays = null;
    for (int j = 0; j < cursorWidth; j++) {
        for (int l = 0; l < cursorHeight; l++) {
            cursorImages.put(0xffffffff);
        }
    }
    cursorImages.flip();
    cursors[0] = new Cursor(cursorWidth, cursorHeight, cursorWidth / 2, cursorHeight / 2, cursorImageCount, cursorImages, cursorDelays);
    // ----------------------------------
    // Create 3 piece animation
    // ==================================
    cursorImageCount = 3;
    cursorImages = ByteBuffer.allocateDirect(cursorWidth * cursorHeight * cursorImageCount * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
    cursorDelays = ByteBuffer.allocateDirect(cursorImageCount * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
    for (int i = 0; i < cursorImageCount; i++) {
        // make a colored square with a chocolate center
        int offColor = 0x00000000;
        int onColor = 0xffff0000;
        // change color according to cursor
        if (i == 1) {
            onColor = 0xff00ff00;
        } else if (i == 2) {
            onColor = 0xff0000ff;
        }
        // calculate size of center
        int centerSize = (cursorWidth / 5) * (i + 1);
        int centerLeft = cursorWidth / 2 - centerSize / 2;
        int centerRight = cursorWidth / 2 + centerSize / 2;
        // go!
        for (int j = 0; j < cursorWidth; j++) {
            for (int l = 0; l < cursorHeight; l++) {
                if (j >= centerLeft && j < centerRight && l >= centerLeft && l < centerRight) {
                    cursorImages.put(offColor);
                } else {
                    cursorImages.put(onColor);
                }
            }
        }
    }
    cursorDelays.put(2000).put(2000).put(2000);
    cursorDelays.flip();
    cursorImages.flip();
    cursors[1] = new Cursor(cursorWidth, cursorHeight, cursorWidth / 2, cursorHeight / 2, cursorImageCount, cursorImages, cursorDelays);
    // ----------------------------------
    // Create a 20 piece animation
    // ==================================
    cursorImageCount = 20;
    cursorImages = ByteBuffer.allocateDirect(cursorWidth * cursorHeight * cursorImageCount * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
    cursorDelays = ByteBuffer.allocateDirect(cursorImageCount * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
    cursorDelays.put(new int[] { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 });
    float step = 0xffffffff / 20.0f;
    for (int i = 0; i < cursorImageCount; i++) {
        for (int j = 0; j < cursorWidth; j++) {
            for (int l = 0; l < cursorHeight; l++) {
                cursorImages.put((int) step);
            }
        }
        step += step;
    }
    cursorImages.flip();
    cursorDelays.flip();
    cursors[2] = new Cursor(cursorWidth, cursorHeight, cursorWidth / 2, cursorHeight / 2, cursorImageCount, cursorImages, cursorDelays);
    // ----------------------------------
    Mouse.setNativeCursor(cursors[0]);
}
Also used : IntBuffer(java.nio.IntBuffer) Cursor(org.lwjgl.input.Cursor)

Aggregations

Cursor (org.lwjgl.input.Cursor)6 IntBuffer (java.nio.IntBuffer)3 JmeCursor (com.jme3.cursors.plugins.JmeCursor)2 LWJGLException (org.lwjgl.LWJGLException)2 LwjglApplication (com.badlogic.gdx.backends.lwjgl.LwjglApplication)1 LwjglApplicationConfiguration (com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration)1 CatacombSnatch (net.catacombsnatch.game.CatacombSnatch)1 PlatformDependent (net.catacombsnatch.game.PlatformDependent)1