Jump to content

[Solved]How can I draw an image that isn't bundled with my mod to a GUI?


Recommended Posts

Posted

Hi, I'm having some troubles regarding drawing images. In my mod, I'm creating images at runtime that I want to be able to show in a gui screen. Essentially, I want to be able to load a texture either a) from an external image file directly (probably won't work) or b) via a BufferedImage object.

 

Due to the recent changes introduced with resource packs, it seems to be difficult to find any information about it and with the code almost entirely obfuscated, I've had troubles getting anywhere searching for a solution.

 

It's not valid any more, but what I'm looking for is basically an updated version of this solution: https://gist.github.com/thvortex/2138298

 

By scanning for references to BufferedImage, I found a few methods related to it in the TextureUtil class, but I don't know if they're what I'm looking for or how I should be using them.

 

I'm thankful for any help.

Posted

Thanks! I got it working by writing my own renderer based on the MapItemRenderer.

 

For future refence, here's (a slightly modified version of) my code.

package panoramakit.gui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.lwjgl.opengl.GL11;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.util.ResourceLocation;

/** 
* Takes care of loading and drawing previews to the screen.
* @author dayanto
*/
public class PreviewRenderer
{
private File file;
private BufferedImage image;
private DynamicTexture previewTexture;
private ResourceLocation resourceLocation;
private TextureManager textureManager;

public PreviewRenderer(TextureManager textureManager, File file)
{
	this.textureManager = textureManager;
	this.file = file;
}

/**
 * Attempts to load the image. Returns whether it was successful or not.
 */
public boolean loadPreview()
{
	try {
		image = ImageIO.read(file);
		previewTexture = new DynamicTexture(image);
		resourceLocation = textureManager.getDynamicTextureLocation("preivew", previewTexture);
		return true;
	} catch (IOException e) {
		e.printStackTrace();
		return false;
	}
}

public void drawCenteredImage(int xPos, int yPos, int width, int height)
{
	if(previewTexture == null) {
		boolean successful = loadPreview();
		if(!successful) return;
	}
	drawImage(xPos + (width - image.getWidth()) / 2, yPos + (height - image.getHeight()) / 2, image.getWidth(), image.getHeight());
}

private void drawImage(int xPos, int yPos, int width, int height)
{
	previewTexture.updateDynamicTexture();
	Tessellator tessellator = Tessellator.instance;
	textureManager.bindTexture(resourceLocation);
	GL11.glEnable(GL11.GL_BLEND);
	GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
	GL11.glDisable(GL11.GL_ALPHA_TEST);
	tessellator.startDrawingQuads();
	tessellator.addVertexWithUV(xPos        , yPos + height, 0, 0.0, 1.0);
	tessellator.addVertexWithUV(xPos + width, yPos + height, 0, 1.0, 1.0);
	tessellator.addVertexWithUV(xPos + width, yPos         , 0, 1.0, 0.0);
	tessellator.addVertexWithUV(xPos        , yPos         , 0, 0.0, 0.0);
	tessellator.draw();
	GL11.glEnable(GL11.GL_ALPHA_TEST);
	GL11.glDisable(GL11.GL_BLEND);
}
}

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.

Announcements



×
×
  • Create New...

Important Information

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