|
1 | 1 | package uno.buffer |
2 | 2 |
|
| 3 | + |
| 4 | +import glm_.BYTES |
| 5 | +import glm_.set |
3 | 6 | import org.lwjgl.system.MemoryUtil |
4 | 7 | import uno.kotlin.Quadruple |
5 | 8 | import uno.kotlin.Quintuple |
@@ -78,8 +81,35 @@ fun ByteBuffer.use(block: (ByteBuffer) -> Unit) { |
78 | 81 | destroy() |
79 | 82 | } |
80 | 83 |
|
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 { |
83 | 113 | block() |
84 | 114 | destroy() |
85 | 115 | } |
|
0 commit comments