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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • i dont have any crash reports i only have the latest log https://mclo.gs/DONwR2g
    • Maybe it is an issue with one of the Create addons Create_The_Kitchen_Must_Grow is the last mentioned mod
    • I cannot find any information on why this is specifically happening in any of the logs and i'm getting annoyed. it manifests primarily as lag when breaking and placing blocks, or picking up items. Items will be picked up than disappear, only to appear in my inventory many seconds later, blocks will not place for over 30 seconds at a time. observable shows 100459 μs/t on minecraft:player spark profile doesnt seem to show anything strange as far as i can tell though except for that about 20% of the usage is something called crusty_chunks and another 20% is distant horizons, even when i have distant rendering disabled. I've attached the latest.log, spark profile and observable exports.
    • [16:07:39] [main/INFO]:Mixins added to allowed list: [main.ClientPacketListenerMixin] [16:07:39] [main/INFO]:Class dev.uncandango.alltheleaks.leaks.client.mods.ae2wtlib.UntrackedIssue002 will NOT be loaded as mod ae2wtlib is not present [16:07:39] [main/INFO]:Class dev.uncandango.alltheleaks.leaks.client.mods.ae2wtlib.UntrackedIssue001 will NOT be loaded as mod ae2wtlib is not present [16:07:39] [main/INFO]:Class dev.uncandango.alltheleaks.fix.common.mods.modernfix.CancelRLMixin will be loaded as it matches versions: 5.20.2+mc1.20.1 in [5.0.0,) [16:07:39] [main/INFO]:Mixins added to cancel list: [org.embeddedt.modernfix.common.mixin.perf.deduplicate_location.MixinResourceLocation] [16:07:39] [main/INFO]:Skipping feature ResourceLocation Deduplication from mod minecraft as it's feature flag is not activated! [16:07:39] [main/INFO]:Skipping feature Ingredient Deduplication from mod minecraft as it's feature flag is not activated! [16:07:39] [main/INFO]:Skipping feature Prevent Search Ignored Items from mod jei as it's feature flag is not activated! [16:07:39] [main/WARN]:Error loading class: com/jozufozu/flywheel/util/WorldAttached (java.lang.ClassNotFoundException: com.jozufozu.flywheel.util.WorldAttached) [16:07:39] [main/WARN]:@Mixin target com.jozufozu.flywheel.util.WorldAttached was not found alltheleaks.mixins.json:main.WorldAttachedMixin from mod alltheleaks [16:07:39] [main/INFO]:Loaded config for: betterfpsdist.json [16:07:39] [main/INFO]:Loaded config for: structureessentials.json [16:07:39] [main/WARN]:Error loading class: dev/tr7zw/skinlayers/render/CustomizableModelPart (java.lang.ClassNotFoundException: dev.tr7zw.skinlayers.render.CustomizableModelPart) [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.BackgroundRendererMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.BiomeAccessAccessor [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.ChunkLightProviderMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.ChunkLightProviderMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.ChunkLightProviderMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.ClientChunkManagerMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.ClientSettingsC2SPacketMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.ClientWorldAccessor [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.GameOptionsMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.GameRendererMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.IntegratedServerMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.LightingProviderMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.MinecraftClientMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.SimpleOptionAccessor [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.ValidatingIntSliderCallbacksAccessor [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.sodium.SodiumChunkManagerMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.sodium.SodiumClientPlayNetworkHandlerMixin [16:07:39] [main/INFO]:Loading mixin: de.johni0702.minecraft.bobby.mixin.sodium.SodiumGameOptionPagesMixin [16:07:40] [main/INFO]:Replaced 1 calls to Enchantment#getMaxLevel() in net/minecraft/world/entity/npc/VillagerTrades$EnchantBookForEmeralds [16:07:40] [main/INFO]:Replaced 1 calls to Enchantment#isTreasureOnly() in net/minecraft/world/entity/npc/VillagerTrades$EnchantBookForEmeralds [16:07:40] [main/INFO]:Replaced 1 calls to Enchantment#isTradeable() in net/minecraft/world/entity/npc/VillagerTrades$EnchantBookForEmeralds [16:07:40] [main/INFO]:Loaded config for: recipeessentials.json [16:07:40] [main/INFO]:Patching FishingHook#catchingFish
  • Topics

×
×
  • Create New...

Important Information

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