Jump to content

How do I properly reuse/update a DynamicTexture Object?


Whodundid

Recommended Posts

So the current way I've been using DynamicTextures is causing a constant memory leak as I have to create a new one every 10th of a second.

I am creating the DynamicTexture using a BufferedImage I am creating at run time. My only problem is that I don't know how to update the texture after it has already been created with the initial BufferedImage.

So to fix the memory leak issue, I need some way to either delete the previous DynamicTexture object or reuse the already existing one. I've looked into the MapItemRenderer as it uses the DynamicTexture but I am not sure how to implement this.

 

This is essentially what I currently have:

public static BufferedImage image = new BufferedImage(75, 75, BufferedImage.TYPE_INT_ARGB);
public static DynamicTexture redrawnImage = new DynamicTexture(image);

public static void updateImage() {
	redrawnImage.deleteGlTexture();
	//(re-create the BufferedImage 'image' here)
	redrawnImage = new DynamicTexture(image);
}

 

Link to comment
Share on other sites

I THINK I fixed it. I looked more into the MapItemRenderer and tried creating my own texture handler based off of it.

 

After running the game for a solid 10 minutes while running the redraw process (an amount of time which would have definitely ate up a bunch of memory by now), the leak seems to have been fixed.

 

This is what I came up with to fix it:

package com.Whodundid.main.util;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;

public class InGameTextureHandler {
	
	private final TextureManager textureManager;
	private final DynamicTexture texture;
	private int[] textureData;
	
	public InGameTextureHandler(TextureManager manager, DynamicTexture textureIn) {
		this.textureManager = manager;
		this.texture = textureIn;
		this.textureData = textureIn.getTextureData();
	}
	
	public void updateTextureData(BufferedImage newImage) {
		int[] newImgData = ((DataBufferInt)newImage.getRaster().getDataBuffer()).getData();
		if (this.textureData.length == newImgData.length) {
			for (int i = 0; i < newImgData.length; i++) {
				this.textureData[i] = newImgData[i];
			}
		}
		this.texture.updateDynamicTexture();
	}
	
	public ResourceLocation getTextureLocation() {
		return textureManager.getDynamicTextureLocation("texture/", texture);
	}
}

 

Then back in the class where I am re-creating the BufferedImage I do:

public static BufferedImage image = new BufferedImage(75, 75, BufferedImage.TYPE_INT_ARGB);
public static DynamicTexture redrawnImage = new DynamicTexture(image);
public static InGameTextureHandler handler = new InGameTextureHandler(Minecraft.getMinecraft.getTextureManager(), redrawnImage);

public static void updateImage() {
	//(re-create the BufferedImage 'image' here)
	handler.updateTextureData(image);
}

 

Link to comment
Share on other sites

Update:

That does not appear to have fixed it. I am really not sure what is causing it at this point.

 

EDIT:

Tried another thing which actually fixed it:

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;

public class InGameTextureHandler {
	
	private final TextureManager textureManager;
	private final DynamicTexture texture;
	private int[] textureData;
	private ResourceLocation loc;
	
	public InGameTextureHandler(TextureManager manager, DynamicTexture textureIn) {
		this.textureManager = manager;
		this.texture = textureIn;
		this.textureData = textureIn.getTextureData();
		this.loc = textureManager.getDynamicTextureLocation("texture/", texture);
	}
	
	public void updateTextureData(BufferedImage newImage) {
		int[] newImgData = ((DataBufferInt)newImage.getRaster().getDataBuffer()).getData();
		if (this.textureData.length == newImgData.length) {
			for (int i = 0; i < newImgData.length; i++) {
				this.textureData[i] = newImgData[i];
			}
		}
		this.texture.updateDynamicTexture();
	}
	
	public ResourceLocation getTextureLocation() {
		return loc;
	}
}

The problem was that it seemed to be creating a new ResourceLocation every time it returned the texture location. So to fix, I just made one and paired it with the dynamic texture.

Edited by Whodundid
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • ChemsXpress (https://chemsxpress.com): ChemsXpress is a platform specializing in the distribution of research chemicals and related products. It offers a wide range of substances for scientific and laboratory use, catering to researchers and professionals. BubatzLand (https://bubatzland.com): BubatzLand focuses on providing high-quality cannabis products. The site offers a variety of strains, edibles, and related accessories, aiming to serve the needs of cannabis enthusiasts and medicinal users. HalluShroom (https://hallushroom.com): HalluShroom specializes in the sale of psychedelic mushrooms and related products. It caters to individuals interested in exploring the benefits and experiences associated with psilocybin and other psychedelic substances. HighAsch (https://highasch.com): HighAsch offers a diverse range of cannabis products, including flowers, concentrates, and edibles. The site aims to provide premium-quality products to both recreational and medicinal users. KetaminHaus (https://ketaminhaus.com): KetaminHaus provides ketamine and related products for research and therapeutic use. The platform focuses on ensuring the availability of high-quality, safe, and effective ketamine for various applications. KokainLabor (https://kokainlabor.com): KokainLabor offers cocaine and other related substances. The site targets individuals seeking high-purity products for personal or research purposes, emphasizing quality and safety. MDPHPWelt (https://mdphpwelt.com): MDPHPWelt specializes in the distribution of MDPHP and other similar research chemicals. It aims to supply researchers with high-quality substances for scientific and experimental use. NembutaLabor (https://nembutalabor.com): NembutaLabor provides Nembutal (pentobarbital) and related products. The site focuses on supplying these substances for research and veterinary purposes, ensuring high standards of quality and safety. NembutalQuelle (https://nembutalquelle.com): NembutalQuelle is dedicated to offering Nembutal and similar barbiturates. It caters to researchers and professionals who require these substances for scientific, medical, or veterinary applications. JBSaves Ltda Brazil (https://jbsavesltdabrazil.com): JBSaves Ltda Brazil is involved in the food and agricultural industry, providing a variety of products and services related to food processing, distribution, and agricultural practices. Bello Alimentos Ltda (https://belloalimentosltda.com): Bello Alimentos Ltda offers a wide range of food products. The site focuses on high-quality, nutritious, and safe food items, catering to consumers and businesses in the food industry. São Salvador Alimentos (https://saosalvadoralimentoss.com): São Salvador Alimentos specializes in the production and distribution of food products. The site provides a variety of options aimed at meeting the dietary needs and preferences of diverse consumers. WietWereld (https://wietwereld.com): WietWereld is a platform dedicated to the cannabis community. It offers information, products, and resources related to cannabis cultivation, consumption, and culture. 14. BeuhParadise (https://beuhparadis.com): BeuhParadise offers a range of cannabis products, including flowers, edibles, and concentrates. The site aims to provide high-quality cannabis products to recreational and medicinal users, emphasizing purity and potency. 15. WietWereld (https://wietwereld.com): WietWereld is a comprehensive resource for cannabis enthusiasts. It provides products, cultivation guides, and community forums for those interested in cannabis culture and cultivation. GrasOase (https://grasoase.com): GrasOase specializes in cannabis products and accessories. The site offers a variety of strains, seeds, and related items for cannabis users, focusing on quality and customer satisfaction. https://chemsxpress.com/ https://bubatzland.com/ https://hallushroom.com/ https://highasch.com/ https://ketaminhaus.com/ https://kokainlabor.com/ https://mdphpwelt.com/ https://nembutalabor.com/ https://nembutalquelle.com/ https://jbsavesltdabrazil.com/ https://belloalimentosltda.com/ https://saosalvadoralimentoss.com/ https://wietwereld.com https://beuhparadis.com https://wietwereld.com https://grasoase.com/
    • For Example I have an Main Mod that adds new Content to Minecraft. But I'm using custom Classes etc. and I want it so that is an api integrated in the Main Mod but can be download seperately like an Module. Is this possible? If you need more Information just comment what you need to know ^^
    • Delete the config of this mod (config folder) and test it again   If there is no change, try other builds  
    • Add the full crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.