From 9a9c087b7562a6ce55ca65f312750c97434be784 Mon Sep 17 00:00:00 2001 From: stephengold Date: Mon, 24 Feb 2025 23:18:38 -0800 Subject: [PATCH] Jolt: add the newDirectShortBuffer() method --- .../com/github/stephengold/joltjni/Jolt.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/com/github/stephengold/joltjni/Jolt.java b/src/main/java/com/github/stephengold/joltjni/Jolt.java index 05edae61..ab0de60e 100644 --- a/src/main/java/com/github/stephengold/joltjni/Jolt.java +++ b/src/main/java/com/github/stephengold/joltjni/Jolt.java @@ -29,6 +29,7 @@ of this software and associated documentation files (the "Software"), to deal import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; +import java.nio.ShortBuffer; /** * Utility methods providing JNI access to Jolt Physics and the C++ {@code std} @@ -367,6 +368,25 @@ public static IntBuffer newDirectIntBuffer(int numInts) { return result; } + /** + * Create a direct {@code ShortBuffer} with native byte order and the + * specified capacity. + * + * @param numShorts the desired capacity (in ints) + * @return a new direct buffer, zeroed and rewound but not flipped + */ + public static ShortBuffer newDirectShortBuffer(int numShorts) { + ByteBuffer byteBuffer + = ByteBuffer.allocateDirect(numShorts * Short.BYTES); + byteBuffer.order(ByteOrder.nativeOrder()); + ShortBuffer result = byteBuffer.asShortBuffer(); + + assert result.capacity() == numShorts : result.capacity(); + assert result.limit() == numShorts : result.limit(); + assert result.position() == 0 : result.position(); + return result; + } + /** * Generate 3-D Perlin noise. *