2004-06-12 22:28:34 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2002-2004 LWJGL Project
|
2004-03-01 14:06:13 +01:00
|
|
|
* All rights reserved.
|
2004-06-12 22:28:34 +02:00
|
|
|
*
|
2004-03-01 14:06:13 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
2004-06-12 22:28:34 +02:00
|
|
|
* modification, are permitted provided that the following conditions are
|
2004-03-01 14:06:13 +01:00
|
|
|
* met:
|
2004-06-12 22:28:34 +02:00
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
2004-03-01 14:06:13 +01:00
|
|
|
* 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 22:28:34 +02:00
|
|
|
* * Neither the name of 'LWJGL' nor the names of
|
|
|
|
|
* its contributors may be used to endorse or promote products derived
|
2004-03-01 14:06:13 +01:00
|
|
|
* from this software without specific prior written permission.
|
2004-06-12 22:28:34 +02:00
|
|
|
*
|
2004-03-01 14:06:13 +01:00
|
|
|
* 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
|
2004-06-12 22:28:34 +02:00
|
|
|
* 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
|
2004-03-01 14:06:13 +01:00
|
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
2004-06-12 22:28:34 +02:00
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
2004-03-01 14:06:13 +01:00
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
package org.lwjgl;
|
|
|
|
|
|
2004-03-26 12:26:04 +01:00
|
|
|
import java.nio.*;
|
2004-03-01 14:06:13 +01:00
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
import java.nio.FloatBuffer;
|
2004-10-14 22:59:13 +02:00
|
|
|
import java.nio.DoubleBuffer;
|
2004-03-01 14:06:13 +01:00
|
|
|
import java.nio.IntBuffer;
|
|
|
|
|
import java.nio.ShortBuffer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Some often-used Buffer code for creating native buffers of the appropriate size.
|
|
|
|
|
|
|
|
|
|
* @author $Author$
|
|
|
|
|
* @version $Revision$
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public final class BufferUtils {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a direct native-ordered bytebuffer with the specified size.
|
|
|
|
|
* @param size The size, in bytes
|
|
|
|
|
* @return a ByteBuffer
|
|
|
|
|
*/
|
|
|
|
|
public static ByteBuffer createByteBuffer(int size) {
|
|
|
|
|
return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct a direct native-order shortbuffer with the specified number
|
|
|
|
|
* of elements.
|
|
|
|
|
* @param size The size, in shorts
|
|
|
|
|
* @return a ShortBuffer
|
|
|
|
|
*/
|
|
|
|
|
public static ShortBuffer createShortBuffer(int size) {
|
|
|
|
|
return createByteBuffer(size << 1).asShortBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2004-10-14 22:59:13 +02:00
|
|
|
* Construct a direct native-order intbuffer with the specified number
|
2004-03-01 14:06:13 +01:00
|
|
|
* of elements.
|
2004-03-01 14:07:00 +01:00
|
|
|
* @param size The size, in ints
|
|
|
|
|
* @return an IntBuffer
|
2004-03-01 14:06:13 +01:00
|
|
|
*/
|
|
|
|
|
public static IntBuffer createIntBuffer(int size) {
|
|
|
|
|
return createByteBuffer(size << 2).asIntBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2004-10-14 22:59:13 +02:00
|
|
|
* Construct a direct native-order floatbuffer with the specified number
|
2004-03-01 14:06:13 +01:00
|
|
|
* of elements.
|
2004-03-01 14:07:00 +01:00
|
|
|
* @param size The size, in floats
|
|
|
|
|
* @return a FloatBuffer
|
2004-03-01 14:06:13 +01:00
|
|
|
*/
|
|
|
|
|
public static FloatBuffer createFloatBuffer(int size) {
|
|
|
|
|
return createByteBuffer(size << 2).asFloatBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-14 22:59:13 +02:00
|
|
|
/**
|
|
|
|
|
* Construct a direct native-order doublebuffer with the specified number
|
|
|
|
|
* of elements.
|
|
|
|
|
* @param size The size, in floats
|
|
|
|
|
* @return a FloatBuffer
|
|
|
|
|
*/
|
|
|
|
|
public static DoubleBuffer createDoubleBuffer(int size) {
|
|
|
|
|
return createByteBuffer(size << 3).asDoubleBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-26 12:26:04 +01:00
|
|
|
/**
|
|
|
|
|
* A helper function which is used to get the byte offset in an arbitrary buffer
|
|
|
|
|
* based on its position
|
|
|
|
|
* @return the position of the buffer, in BYTES
|
|
|
|
|
*/
|
|
|
|
|
public static int getOffset(Buffer buffer) {
|
|
|
|
|
if (buffer instanceof FloatBuffer || buffer instanceof IntBuffer)
|
|
|
|
|
return buffer.position() << 2;
|
|
|
|
|
else if (buffer instanceof ShortBuffer || buffer instanceof CharBuffer)
|
|
|
|
|
return buffer.position() << 1;
|
|
|
|
|
else if (buffer instanceof DoubleBuffer || buffer instanceof LongBuffer)
|
|
|
|
|
return buffer.position() << 3;
|
|
|
|
|
else
|
|
|
|
|
return buffer.position();
|
|
|
|
|
}
|
|
|
|
|
|
2004-03-01 14:06:13 +01:00
|
|
|
}
|