Jump to content

[1.8.8][SOLVED]Drawing an image that isn't bundled with the mod to a gui


Recommended Posts

Posted

Hey guys, I am trying to draw an image being created dynamically at runtime to a gui. I would like to do it from a BufferedImage(preferred) or a File. The code I had been using broke when I tried updating it to 1.8.8, and I have been trying for hours to get it to work, with no success. At some point, I scrapped my old code for a more efficient pre-1.8.8 of doing it that I found while searching for a solution, then attempted to update that code. The result is this:

 

/**
* Takes care of loading and drawing images to the screen. Adapted from http://www.minecraftforge.net/forum/index.php?topic=11991.0
* @author dayanto
* @author The_Fireplace
*/
public class SkinRender
{
private File file;
private BufferedImage image;
private DynamicTexture previewTexture;
private ResourceLocation resourceLocation;
private TextureManager textureManager;

public SkinRender(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(IngameAccountSwitcher.MODID, 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){
			System.out.println("Failure");
			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.getInstance();
	WorldRenderer rend = tessellator.getWorldRenderer();
	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);
	//Begin major modification
	rend.func_181668_a(GL11.GL_QUADS, new VertexFormat().func_181721_a(new VertexFormatElement(0, VertexFormatElement.EnumType.FLOAT, VertexFormatElement.EnumUsage.POSITION, 3)));
	rend.func_181673_a(0.0, 1.0).func_181662_b(xPos, yPos+height, 0);
	rend.func_181673_a(1.0, 1.0).func_181662_b(xPos+width, yPos+height, 0);
	rend.func_181673_a(1.0, 0.0).func_181662_b(xPos+width, yPos, 0);
	rend.func_181673_a(0.0, 0.0).func_181662_b(xPos, yPos, 0);
	//End major modification
	tessellator.draw();
	GL11.glEnable(GL11.GL_ALPHA_TEST);
	GL11.glDisable(GL11.GL_BLEND);
}
}

 

As it says in the comment above the class name, it is adapted from Dayanto's solution.

 

public void drawCenteredImage(int xPos, int yPos, int width, int height)

is called in

public void drawScreen(int x, int y, float par3)

of my gui.

 

And if anyone is interested, my way of doing it back in 1.8:

 

Tessellator tess = Tessellator.getInstance();
		WorldRenderer rend = tess.getWorldRenderer();
		GL11.glEnable(GL11.GL_BLEND);
		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		x = x + width / 4;
		double w = width / 2;
		double h = height / 4;
		double fw = 32;
		double fh = 32;
		double u = 32;
		double v = 32;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		u = 160;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		y = y + height / 4;
		h = height / 8 * 3;
		fh = 48;
		u = 80;
		v = 80;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		v = 144;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, (double)y + 0, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		x = x - width / 16 * (slim ? 3 : 4);
		y = y + (slim ? height / 32 : 0);
		w = width / 16 * (slim ? 3 : 4);
		h = height / 8 * 3;
		fw = slim ? 12 : 16;
		fh = 48;
		u = 176;
		v = 80;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h - 4, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h - 4, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, (double)y - 4, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, (double)y - 4, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		v = 144;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h - 4, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h - 4, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, (double)y - 4, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, (double)y - 4, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		x = x + width / 16 * (slim ? 11 : 12);
		v = 80;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h - 4, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h - 4, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, (double)y - 4, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, (double)y - 4, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		v = 144;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h - 4, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h - 4, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, (double)y - 4, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, (double)y - 4, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		x = x - width / 2;
		y = y + height / 32 * (slim ? 11 : 12);
		w = width / 4;
		fw = 16;
		u = 16;
		v = 80;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		v = 144;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		x = x + width / 4;
		v = 80;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		v = 144;
		rend.startDrawingQuads();
		rend.addVertexWithUV(x, y + h, 0, (float)(u + fw) * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y + h, 0, (float)u * 0.00390625F, (float)(v + fh) * 0.00390625F);
		rend.addVertexWithUV(x + w, y, 0, (float)u * 0.00390625F, (float)v * 0.00390625F);
		rend.addVertexWithUV(x, y, 0, (float)(u + fw) * 0.00390625F, (float)v * 0.00390625F);
		tess.draw();
		GL11.glDisable(GL11.GL_BLEND);

 

Any help would be greatly appreciated.

 

 

UPDATE: I got it to draw the image. I still have to get it to scale up, and it causes a few other bugs, but I'm sure those can be worked out. Here is the section of code that was changed, if anyone is interested.

 

private void drawImage(int xPos, int yPos, int width, int height)
{
	previewTexture.updateDynamicTexture();
	Tessellator tessellator = Tessellator.getInstance();
	WorldRenderer rend = tessellator.getWorldRenderer();
	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);
	rend.func_181668_a(7, DefaultVertexFormats.field_181709_i);
	int l = 255 / (0 + 1);
	rend.func_181662_b(xPos, yPos+height, 0).func_181673_a(0.0D, 1.0D).func_181669_b(255, 255, 255, l).func_181675_d();
	rend.func_181662_b(xPos+width, yPos+height, 0).func_181673_a(1.0D, 1.0D).func_181669_b(255, 255, 255, l).func_181675_d();
	rend.func_181662_b(xPos+width, yPos, 0).func_181673_a(1.0D, 0.0D).func_181669_b(255, 255, 255, l).func_181675_d();
	rend.func_181662_b(xPos, yPos, 0).func_181673_a(0.0D, 0.0D).func_181669_b(255, 255, 255, l).func_181675_d();
	tessellator.draw();
	GL11.glEnable(GL11.GL_ALPHA_TEST);
	GL11.glDisable(GL11.GL_BLEND);
}

 

If I helped please press the Thank You button.

 

Check out my mods at http://www.curse.com/users/The_Fireplace/projects

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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