mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-15 04:54:05 +09:00
new gem on worldgen
This commit is contained in:
@@ -13,13 +13,13 @@ import net.torvald.unsafe.UnsafePtr
|
||||
* where a_n is a tile number, p_n is a placement index
|
||||
* Created by minjaesong on 2023-10-10.
|
||||
*/
|
||||
class BlockLayerI16I8 (val width: Int, val height: Int) : BlockLayer {
|
||||
class BlockLayerI16I8 (override val width: Int, override val height: Int) : BlockLayer {
|
||||
override val bytesPerBlock = BYTES_PER_BLOCK
|
||||
|
||||
// for some reason, all the efforts of saving the memory space were futile.
|
||||
|
||||
// using unsafe pointer gets you 100 fps, whereas using directbytebuffer gets you 90
|
||||
internal val ptr: UnsafePtr = UnsafeHelper.allocate(width * height * BYTES_PER_BLOCK)
|
||||
internal val ptr: UnsafePtr = UnsafeHelper.allocate(width * height * bytesPerBlock)
|
||||
|
||||
val ptrDestroyed: Boolean
|
||||
get() = ptr.destroyed
|
||||
@@ -45,7 +45,7 @@ class BlockLayerI16I8 (val width: Int, val height: Int) : BlockLayer {
|
||||
return object : Iterator<Byte> {
|
||||
private var iteratorCount = 0L
|
||||
override fun hasNext(): Boolean {
|
||||
return iteratorCount < width * height * BYTES_PER_BLOCK
|
||||
return iteratorCount < width * height * bytesPerBlock
|
||||
}
|
||||
override fun next(): Byte {
|
||||
iteratorCount += 1
|
||||
@@ -55,7 +55,7 @@ class BlockLayerI16I8 (val width: Int, val height: Int) : BlockLayer {
|
||||
}
|
||||
|
||||
override fun unsafeGetTile(x: Int, y: Int): Int {
|
||||
val offset = BYTES_PER_BLOCK * (y * width + x)
|
||||
val offset = getOffset(x, y)
|
||||
val lsb = ptr[offset]
|
||||
val msb = ptr[offset + 1]
|
||||
val placement = ptr[offset + 2]
|
||||
@@ -64,7 +64,7 @@ class BlockLayerI16I8 (val width: Int, val height: Int) : BlockLayer {
|
||||
}
|
||||
|
||||
internal fun unsafeGetTile1(x: Int, y: Int): Pair<Int, Int> {
|
||||
val offset = BYTES_PER_BLOCK * (y * width + x)
|
||||
val offset = getOffset(x, y)
|
||||
val lsb = ptr[offset]
|
||||
val msb = ptr[offset + 1]
|
||||
val placement = ptr[offset + 2]
|
||||
@@ -73,12 +73,12 @@ class BlockLayerI16I8 (val width: Int, val height: Int) : BlockLayer {
|
||||
}
|
||||
|
||||
override fun unsafeToBytes(x: Int, y: Int): ByteArray {
|
||||
val offset = BYTES_PER_BLOCK * (y * width + x)
|
||||
val offset = getOffset(x, y)
|
||||
return byteArrayOf(ptr[offset + 1], ptr[offset + 0], ptr[offset + 2])
|
||||
}
|
||||
|
||||
internal fun unsafeSetTile(x: Int, y: Int, tile: Int, placement: Int) {
|
||||
val offset = BYTES_PER_BLOCK * (y * width + x)
|
||||
val offset = getOffset(x, y)
|
||||
|
||||
val lsb = tile.and(0xff).toByte()
|
||||
val msb = tile.ushr(8).and(0xff).toByte()
|
||||
@@ -96,14 +96,14 @@ class BlockLayerI16I8 (val width: Int, val height: Int) : BlockLayer {
|
||||
}
|
||||
|
||||
override fun unsafeSetTile(x: Int, y: Int, bytes: ByteArray) {
|
||||
val offset = BYTES_PER_BLOCK * (y * width + x)
|
||||
val offset = getOffset(x, y)
|
||||
ptr[offset] = bytes[1]
|
||||
ptr[offset + 1] = bytes[0]
|
||||
ptr[offset + 2] = bytes[2]
|
||||
}
|
||||
|
||||
internal fun unsafeSetTileKeepPlacement(x: Int, y: Int, tile: Int) {
|
||||
val offset = BYTES_PER_BLOCK * (y * width + x)
|
||||
val offset = getOffset(x, y)
|
||||
|
||||
val lsb = tile.and(0xff).toByte()
|
||||
val msb = tile.ushr(8).and(0xff).toByte()
|
||||
|
||||
Reference in New Issue
Block a user