Skip to content

Commit

Permalink
Image: Allow for null textures
Browse files Browse the repository at this point in the history
When the Image is initially created, it has to wait one more tick to create the Texture. This would cause problems if the user calls Renderer.bindTexture before that happens, resulting in a crash. We solve this by just providing 0 as the id, which will just render black for that one tick before it gets loaded.
  • Loading branch information
camnwalter authored and mattco98 committed Mar 4, 2024
1 parent f94350b commit 74e9275
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
8 changes: 1 addition & 7 deletions src/main/kotlin/com/chattriggers/ctjs/api/render/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ class Image(var image: BufferedImage?) {

fun getTextureHeight(): Int = textureHeight

fun getTexture(): NativeImageBackedTexture {
requireNotNull(texture) {
"Failed to bake Image texture"
}

return texture!!.texture
}
fun getTexture(): NativeImageBackedTexture? = texture?.texture

internal fun getIdOrRegister(): Identifier {
if (identifier == null) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/com/chattriggers/ctjs/api/render/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ object Renderer {
@JvmStatic
@JvmOverloads
fun bindTexture(texture: Image, textureIndex: Int = 0) = apply {
UGraphics.bindTexture(textureIndex, texture.getTexture().glId)
UGraphics.bindTexture(textureIndex, texture.getTexture()?.glId ?: 0)
}

@JvmStatic
fun deleteTexture(texture: Image) = apply {
UGraphics.deleteTexture(texture.getTexture().glId)
UGraphics.deleteTexture(texture.getTexture()?.glId ?: 0)
}

@JvmStatic
Expand Down Expand Up @@ -580,7 +580,7 @@ object Renderer {

scale(1f, 1f, 50f)

RenderSystem.setShaderTexture(0, image.getTexture().glId)
RenderSystem.setShaderTexture(0, image.getTexture()?.glId ?: 0)

begin(DrawMode.QUADS, VertexFormat.POSITION_TEXTURE)
pos(x, y + height, 0f).tex(0f, 1f)
Expand Down

0 comments on commit 74e9275

Please sign in to comment.