Allow copying of GuiImages without recreating the texture
This commit is contained in:
@@ -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<T extends AbstractGuiImage<T>>
|
||||
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<T> copyOf;
|
||||
|
||||
public AbstractGuiImage() {
|
||||
}
|
||||
|
||||
@@ -48,6 +55,18 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
||||
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
|
||||
public void draw(GuiRenderer renderer, ReadableDimension size, RenderInfo renderInfo) {
|
||||
if (texture != null) {
|
||||
@@ -63,7 +82,7 @@ public abstract class AbstractGuiImage<T extends AbstractGuiImage<T>>
|
||||
@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<T extends AbstractGuiImage<T>>
|
||||
|
||||
@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<T extends AbstractGuiImage<T>>
|
||||
|
||||
@Override
|
||||
public T setTexture(ResourceLocation resourceLocation) {
|
||||
Preconditions.checkState(copyOf == null, "Cannot change texture of copy.");
|
||||
if (texture != null) {
|
||||
texture.deleteGlTexture();
|
||||
texture = null;
|
||||
|
||||
@@ -32,6 +32,10 @@ public class GuiImage extends AbstractGuiImage<GuiImage> {
|
||||
super(container);
|
||||
}
|
||||
|
||||
public GuiImage(GuiImage copyOf) {
|
||||
super(copyOf);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GuiImage getThis() {
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user