Skip to content

Commit b2841ee

Browse files
committed
refactored animated texture
1 parent a79cf40 commit b2841ee

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

common/src/main/kotlin/com/lambda/graphics/texture/AnimatedTexture.kt

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,22 @@ import org.lwjgl.BufferUtils
2525
import org.lwjgl.opengl.GL11.GL_RGBA
2626
import org.lwjgl.stb.STBImage
2727
import java.nio.ByteBuffer
28-
import kotlin.properties.Delegates
2928

3029

31-
class AnimatedTexture(
32-
private val path: LambdaResource,
33-
) : Texture(null) {
34-
lateinit var frameDurations: IntArray
35-
var width by Delegates.notNull<Int>()
36-
var height by Delegates.notNull<Int>()
37-
var channels by Delegates.notNull<Int>()
38-
var frames by Delegates.notNull<Int>()
30+
class AnimatedTexture(path: LambdaResource) : Texture(null) {
31+
private val pbo: PixelBuffer
32+
private val gif: ByteBuffer // Do NOT free this pointer
33+
private val frameDurations: IntArray
34+
val width: Int
35+
val height: Int
36+
val channels: Int
37+
val frames: Int
3938

40-
val blockSize: Int
39+
private val blockSize: Int
4140
get() = width * height * channels
4241

43-
lateinit var gif: ByteBuffer // Do NOT free this pointer
44-
var pbo: PixelBuffer
45-
46-
var currentFrame = 0
47-
var lastUpload = 0L
42+
private var currentFrame = 0
43+
private var lastUpload = 0L
4844

4945
override fun bind(slot: Int) {
5046
update()
@@ -53,6 +49,9 @@ class AnimatedTexture(
5349

5450
fun update() {
5551
if (System.currentTimeMillis() - lastUpload >= frameDurations[currentFrame]) {
52+
// This is cool because instead of having a buffer for each frame we can
53+
// just move the frame's block on each update
54+
// 0 memory allocation and few cpu cycles
5655
val slice = gif
5756
.position(blockSize * currentFrame)
5857
.limit(blockSize * (currentFrame + 1))
@@ -67,7 +66,7 @@ class AnimatedTexture(
6766
}
6867
}
6968

70-
private fun readGif() {
69+
init {
7170
val bytes = path.stream.readAllBytes()
7271
val buffer = ByteBuffer.allocateDirect(bytes.size)
7372

@@ -82,19 +81,17 @@ class AnimatedTexture(
8281

8382
// The buffer contains packed frames that can be extracted as follows:
8483
// limit = width * height * channels * [frame number]
85-
gif = STBImage.stbi_load_gif_from_memory(buffer, pDelays, pWidth, pHeight, pLayers, pChannels, 4)!!
84+
gif = STBImage.stbi_load_gif_from_memory(buffer, pDelays, pWidth, pHeight, pLayers, pChannels, 4)
85+
?: throw IllegalStateException("There was an unknown error while loading the gif file")
8686

8787
width = pWidth.get()
8888
height = pHeight.get()
8989
frames = pLayers.get()
9090
channels = pChannels.get()
91-
9291
frameDurations = IntArray(frames)
92+
9393
pDelays.getIntBuffer(frames).get(frameDurations)
94-
}
9594

96-
init {
97-
readGif()
9895
pbo = PixelBuffer(width, height, format = GL_RGBA, this@AnimatedTexture)
9996
}
10097
}

0 commit comments

Comments
 (0)