Skip to content

Commit

Permalink
Rewind java Buffers on array constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelydaniel committed Oct 16, 2022
1 parent a0653dd commit e0e7b26
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions kgl/src/jvmMain/kotlin/com/danielgergely/kgl/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public actual abstract class Buffer internal constructor(
public actual class FloatBuffer(buffer: FloatBuffer) : Buffer(buffer) {

public actual constructor(buffer: Array<Float>) : this(buffer.toFloatArray())
public actual constructor(buffer: FloatArray) : this(alloc(buffer.size).apply { put(buffer) })
public actual constructor(buffer: FloatArray) : this(alloc(buffer.size).apply {
put(buffer)
position(0)
})

public actual constructor(size: Int) : this(alloc(size))

private companion object {
Expand Down Expand Up @@ -64,7 +68,10 @@ public actual class FloatBuffer(buffer: FloatBuffer) : Buffer(buffer) {

public actual class ByteBuffer(buffer: ByteBuffer) : Buffer(buffer) {
public actual constructor(buffer: Array<Byte>) : this(buffer.toByteArray())
public actual constructor(buffer: ByteArray) : this(alloc(buffer.size).apply { put(buffer) })
public actual constructor(buffer: ByteArray) : this(alloc(buffer.size).apply {
put(buffer)
position(0)
})
public actual constructor(size: Int) : this(alloc(size))

private companion object {
Expand Down Expand Up @@ -110,12 +117,14 @@ public actual class ByteBuffer(buffer: ByteBuffer) : Buffer(buffer) {
public actual class IntBuffer(private val buffer: IntBuffer) : Buffer(buffer) {

public actual constructor(buffer: Array<Int>) : this(buffer.toIntArray())
public actual constructor(buffer: IntArray) : this(alloc(buffer.size).apply { put(buffer) })
public actual constructor(buffer: IntArray) : this(alloc(buffer.size).apply {
put(buffer)
position(0)
})
public actual constructor(size: Int) : this(alloc(size))

private companion object {
private fun alloc(size: Int) =
ByteBuffer.allocateDirect(size * 4).order(ByteOrder.nativeOrder()).asIntBuffer()
private fun alloc(size: Int) = ByteBuffer.allocateDirect(size * 4).order(ByteOrder.nativeOrder()).asIntBuffer()
}

public actual var position: Int
Expand Down

0 comments on commit e0e7b26

Please sign in to comment.