Skip to content

Commit 64d1ee1

Browse files
committed
withBuffer
1 parent a73325d commit 64d1ee1

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dependencies {
3434

3535
testCompile 'io.kotlintest:kotlintest:2.0.7'
3636

37-
compile 'com.github.kotlin-graphics:gln:f1eadeff51794004bea0c01546d146053d4e9c6d'
37+
compile 'com.github.kotlin-graphics:gln:ebd538491404be7e9f56f0300f1acef8e7289804'
3838

3939
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
4040

src/main/kotlin/uno/buffer/buffer.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package uno.buffer
22

3+
4+
import glm_.BYTES
5+
import glm_.set
36
import org.lwjgl.system.MemoryUtil
47
import uno.kotlin.Quadruple
58
import uno.kotlin.Quintuple
@@ -78,8 +81,35 @@ fun ByteBuffer.use(block: (ByteBuffer) -> Unit) {
7881
destroy()
7982
}
8083

81-
fun withBufferBig(size: Int, block: ByteBuffer.() -> Unit) {
82-
with(bufferBig(size)) {
84+
@Suppress("UNCHECKED_CAST")
85+
fun withBuffer(list: List<*>, block: ByteBuffer.() -> Unit) {
86+
when (list.elementAt(0)) {
87+
is Byte -> bufferBig(list.size).apply {
88+
val l = list as List<Byte>
89+
for (i in l.indices) set(i, l[i])
90+
}
91+
is Short -> bufferBig(list.size * Short.BYTES).apply {
92+
val l = list as List<Short>
93+
for (i in l.indices) putShort(i * Short.BYTES, l[i])
94+
}
95+
is Int -> bufferBig(list.size * Int.BYTES).apply {
96+
val l = list as List<Int>
97+
for (i in l.indices) putInt(i * Int.BYTES, l[i])
98+
}
99+
is Long -> bufferBig(list.size * Long.BYTES).apply {
100+
val l = list as List<Long>
101+
for (i in l.indices) putLong(i * Long.BYTES, l[i])
102+
}
103+
is Float -> bufferBig(list.size * Float.BYTES).apply {
104+
val l = list as List<Float>
105+
for (i in l.indices) putFloat(i * Float.BYTES, l[i])
106+
}
107+
is Double -> bufferBig(list.size * Double.BYTES).apply {
108+
val l = list as List<Double>
109+
for (i in l.indices) putDouble(i * Double.BYTES, l[i])
110+
}
111+
else -> throw Error("unsupported type")
112+
}.run {
83113
block()
84114
destroy()
85115
}

0 commit comments

Comments
 (0)