Skip to content

Commit 60907be

Browse files
committed
added checks and dimension properties
1 parent e58c0d2 commit 60907be

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ class AnimatedTexture(path: LambdaResource) : Texture(image = null, forceConsist
3131
private val pbo: PixelBuffer
3232
private val gif: ByteBuffer // Do NOT free this pointer
3333
private val frameDurations: IntArray
34-
val width: Int
35-
val height: Int
3634
val channels: Int
3735
val frames: Int
3836

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ open class Texture(
4545
var initialized: Boolean = false; private set
4646
val id = glGenTextures()
4747

48+
var width = -1; protected set
49+
var height = -1; protected set
50+
4851
/**
4952
* Binds the texture to a specific slot in the graphics pipeline.
5053
*/
@@ -64,8 +67,9 @@ open class Texture(
6467
// mipmaps from them
6568
setupLOD(levels = levels)
6669

67-
val width = image.width
68-
val height = image.height
70+
width = image.width
71+
height = image.height
72+
initialized = true
6973

7074
// Set this mipmap to `offset` to define the original texture
7175
setupTexture(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR)
@@ -74,11 +78,16 @@ open class Texture(
7478
}
7579

7680
open fun update(image: BufferedImage, offset: Int = 0) {
77-
if (forceConsistency && initialized)
78-
throw IllegalStateException("Client tried to update a texture, but the enforce consistency flag was present")
81+
if (!initialized) return upload(image, offset)
7982

80-
val width = image.width
81-
val height = image.height
83+
check(forceConsistency && initialized) {
84+
"Client tried to update a texture, but the enforce consistency flag was present"
85+
}
86+
87+
check(image.width + image.height > this.width + this.height && initialized) {
88+
"Client tried to update a texture with more data than allowed" +
89+
"Expected ${this.width + this.height} bytes but got ${image.width + image.height}"
90+
}
8291

8392
// Can we rebuild LOD ?
8493
glTexSubImage2D(GL_TEXTURE_2D, offset, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, readImage(image))
@@ -100,8 +109,6 @@ open class Texture(
100109
image?.let {
101110
bind()
102111
upload(it)
103-
104-
initialized = true
105112
}
106113
}
107114
}

0 commit comments

Comments
 (0)