Allow copying of GuiImages without recreating the texture

This commit is contained in:
johni0702
2015-10-10 19:54:18 +02:00
parent efde8fde67
commit 12456ae47c
2 changed files with 26 additions and 1 deletions

View File

@@ -22,6 +22,7 @@
package de.johni0702.minecraft.gui.element; package de.johni0702.minecraft.gui.element;
import com.google.common.base.Preconditions;
import de.johni0702.minecraft.gui.GuiRenderer; import de.johni0702.minecraft.gui.GuiRenderer;
import de.johni0702.minecraft.gui.RenderInfo; import de.johni0702.minecraft.gui.RenderInfo;
import de.johni0702.minecraft.gui.container.GuiContainer; import de.johni0702.minecraft.gui.container.GuiContainer;
@@ -41,6 +42,12 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
private int uWidth, vHeight; private int uWidth, vHeight;
private int textureWidth, textureHeight; private int textureWidth, textureHeight;
/**
* Reference to the copied image to prevent it from being garbage collected
* and subsequently releasing the OpenGL texture.
*/
private AbstractGuiImage<T> copyOf;
public AbstractGuiImage() { public AbstractGuiImage() {
} }
@@ -48,6 +55,18 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
super(container); super(container);
} }
public AbstractGuiImage(AbstractGuiImage<T> 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 @Override
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) { public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
if (texture != null) { if (texture != null) {
@@ -63,7 +82,7 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
super.finalize(); super.finalize();
if (texture != null) { if (texture != null && copyOf == null) {
getMinecraft().addScheduledTask(new Finalizer(texture)); getMinecraft().addScheduledTask(new Finalizer(texture));
} }
} }
@@ -75,6 +94,7 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
@Override @Override
public T setTexture(BufferedImage img) { public T setTexture(BufferedImage img) {
Preconditions.checkState(copyOf == null, "Cannot change texture of copy.");
resourceLocation = null; resourceLocation = null;
if (texture != null) { if (texture != null) {
texture.deleteGlTexture(); texture.deleteGlTexture();
@@ -87,6 +107,7 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
@Override @Override
public T setTexture(ResourceLocation resourceLocation) { public T setTexture(ResourceLocation resourceLocation) {
Preconditions.checkState(copyOf == null, "Cannot change texture of copy.");
if (texture != null) { if (texture != null) {
texture.deleteGlTexture(); texture.deleteGlTexture();
texture = null; texture = null;

View File

@@ -32,6 +32,10 @@ public class GuiImage extends AbstractGuiImage<GuiImage> {
super(container); super(container);
} }
public GuiImage(GuiImage copyOf) {
super(copyOf);
}
@Override @Override
protected GuiImage getThis() { protected GuiImage getThis() {
return this; return this;