diff --git a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java index 40580a85..0c3f42d3 100644 --- a/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java +++ b/src/main/java/de/johni0702/minecraft/gui/element/AbstractGuiImage.java @@ -22,6 +22,7 @@ package de.johni0702.minecraft.gui.element; +import com.google.common.base.Preconditions; import de.johni0702.minecraft.gui.GuiRenderer; import de.johni0702.minecraft.gui.RenderInfo; import de.johni0702.minecraft.gui.container.GuiContainer; @@ -41,6 +42,12 @@ public abstract class AbstractGuiImage> private int uWidth, vHeight; private int textureWidth, textureHeight; + /** + * Reference to the copied image to prevent it from being garbage collected + * and subsequently releasing the OpenGL texture. + */ + private AbstractGuiImage copyOf; + public AbstractGuiImage() { } @@ -48,6 +55,18 @@ public abstract class AbstractGuiImage> super(container); } + public AbstractGuiImage(AbstractGuiImage copyOf) { + this.texture = copyOf.texture; + this.resourceLocation = copyOf.resourceLocation; + this.u = copyOf.u; + this.v = copyOf.v; + this.uWidth = copyOf.uWidth; + this.vHeight = copyOf.vHeight; + this.textureWidth = copyOf.textureWidth; + this.textureHeight = copyOf.textureHeight; + this.copyOf = copyOf; + } + @Override public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { if (texture != null) { @@ -63,7 +82,7 @@ public abstract class AbstractGuiImage> @Override protected void finalize() throws Throwable { super.finalize(); - if (texture != null) { + if (texture != null && copyOf == null) { getMinecraft().addScheduledTask(new Finalizer(texture)); } } @@ -75,6 +94,7 @@ public abstract class AbstractGuiImage> @Override public T setTexture(BufferedImage img) { + Preconditions.checkState(copyOf == null, "Cannot change texture of copy."); resourceLocation = null; if (texture != null) { texture.deleteGlTexture(); @@ -87,6 +107,7 @@ public abstract class AbstractGuiImage> @Override public T setTexture(ResourceLocation resourceLocation) { + Preconditions.checkState(copyOf == null, "Cannot change texture of copy."); if (texture != null) { texture.deleteGlTexture(); texture = null; diff --git a/src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java b/src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java index 107520f0..00fe5928 100644 --- a/src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java +++ b/src/main/java/de/johni0702/minecraft/gui/element/GuiImage.java @@ -32,6 +32,10 @@ public class GuiImage extends AbstractGuiImage { super(container); } + public GuiImage(GuiImage copyOf) { + super(copyOf); + } + @Override protected GuiImage getThis() { return this;