Jump to content

Recommended Posts

Posted

Hi, I'm trying to make it so that my picture of a lunchbox is rendered on the screen whenever you have one in your inventory. First I want to make it render on the screen any time so I know it works, however following this tutorial (http://www.minecraftforge.net/wiki/Gui_Overlay) with minor edits to make it work with 1.7.10 just makes it render oddly... Here is the code I ended up with in the end.

 

package com.sixonethree.snapshot.client.gui;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class GuiLunchbox extends Gui {
private Minecraft MinecraftInstance;

public GuiLunchbox(Minecraft MC) {
	super();
	MinecraftInstance = MC;
}

@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent event) {
	if (event.isCanceled() || event.type != ElementType.FOOD) { return; }
	int xPos = 0;
	int yPos = 0;
	GL11.glColor4f(1F, 1F, 1F, 1F);
	GL11.glDisable(GL11.GL_LIGHTING);
	MinecraftInstance.renderEngine.bindTexture(new ResourceLocation("snapshot", "textures/gui/lunchbox.png"));
	drawTexturedModalRect(xPos, yPos, 0, 0, 32, 32);
}
}

 

Here are some pictures of what happens, Picture 1 is the lunchbox texture (src/main/resources/assets/snapshot/textures/gui/lunchbox.png), Picture 2 is what happens using the code above. Picture 3 is what happens when I tweak drawTexturedModalRect(xPos, yPos, 0, 0, 32, 32); to drawTexturedModalRect(xPos, yPos, 32, 32, 32, 32);. Also if anyone knows why the food bars get affected by this as well I would like that to be fixed :)

 

XXeMPlb.png

Hk5FVqV.png

dxbkhUa.png

Posted

If you want to use a texture that is not 256x256, you should use this method instead of drawTexturedModalRect, which I grabbed from somebody else in this thread:

/**
 * Draws textured rectangles of sizes other than 256x256
 * @param x The x value of the top-left corner point on the screen where drawing to starts 
 * @param y The y value of the top-left corner point on the screen where drawing to starts
 * @param u The u (x) value of top-left corner point of the texture to start drawing from
 * @param v The v (y) value of top-left corner point of the texture to start drawing from
 * @param width The width of the rectangle to draw on screen
 * @param height The height of the rectangle to draw on screen
 * @param textureWidth The width of the whole texture
 * @param textureHeight The height of the whole texture
 */
protected void drawNonStandardTexturedRect(int x, int y, int u, int v, int width, int height, int textureWidth, int textureHeight)
    {
        double f = 1F / (double)textureWidth;
        double f1 = 1F / (double)textureHeight;
        Tessellator tessellator = Tessellator.instance;
        tessellator.startDrawingQuads();
        tessellator.addVertexWithUV(x, y + height, 0, u * f, (v + height) * f1);
        tessellator.addVertexWithUV(x + width, y + height, 0, (u + width) * f, (v + height) * f1);
        tessellator.addVertexWithUV(x + width, y, 0, (u + width) * f, v * f1);
        tessellator.addVertexWithUV(x, y, 0, u * f, v * f1);
        tessellator.draw();
    }

 

Also, you should use either Post or Pre of RenderGameOverlayEvent or else it will draw twice a frame (this will be noticed if the overlay was rendered with transparency)

Posted

Thanks to both of you, I'm now using

 

int w = MinecraftInstance.displayWidth / 2;
int h = MinecraftInstance.displayHeight / 2;
drawNonStandardTexturedRect(w - 32, h - 32, 0, 0, 32, 32, 32, 32);

 

To try and get it to draw in the bottom right of the screen, however this only works when the screen is certain sizes for some reason. If I'm in full screen it seems to be drawing it off the screen for some reason. The width and height are being divided by 2, I don't know why but that makes it work when the window is half the screen size. If you know what parameters I should use for getting it to draw in the bottom right I would appreciate it.

Posted

Use ScaledResolution

 

        ScaledResolution scaledresolution = new ScaledResolution(this.mc, this.mc.displayWidth, this.mc.displayHeight);
        int width = scaledresolution.getScaledWidth();
        int height = scaledresolution.getScaledHeight();

Posted

Thanks for your help so far, one last question (I hope) Now that I'm using RenderGameOverlayEvent.Post instead of the food textures being messed with, the bubble textures are messed up instead. There's an image below of what I mean. Here's the method part of the code, the complete code is provided underneath.

 

	@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event) {
	if (event.isCanceled() || event.type != ElementType.FOOD || !hasLunchbox() || !ConfigurationHandler.getLunchboxOnScreen()) { return; }
	GL11.glColor4f(1F, 1F, 1F, 1F);
	GL11.glDisable(GL11.GL_LIGHTING);
	MinecraftInstance.renderEngine.bindTexture(new ResourceLocation("snapshot", "textures/gui/lunchbox.png"));
	ScaledResolution Scaled = new ScaledResolution(MinecraftInstance, MinecraftInstance.displayWidth, MinecraftInstance.displayHeight);
	int Food = getFoodStored();
	int Difference = 5 * (String.valueOf(Food).length()) + 4;
	int Width = Scaled.getScaledWidth() - Difference - 32;
	int Height = Scaled.getScaledHeight() - 32;
	drawNonStandardTexturedRect(Width, Height, 0, 0, 32, 32, 32, 32);
	MinecraftInstance.fontRenderer.drawString(String.valueOf(Food), Width + 32, Height + 16, 0);
}

protected void drawNonStandardTexturedRect(int x, int y, int u, int v, int width, int height, int textureWidth, int textureHeight) {
	double f = 1F / (double) textureWidth;
	double f1 = 1F / (double) textureHeight;
	Tessellator tessellator = Tessellator.instance;
	tessellator.startDrawingQuads();
	tessellator.addVertexWithUV(x, y + height, 0, u * f, (v + height) * f1);
	tessellator.addVertexWithUV(x + width, y + height, 0, (u + width) * f, (v + height) * f1);
	tessellator.addVertexWithUV(x + width, y, 0, (u + width) * f, v * f1);
	tessellator.addVertexWithUV(x, y, 0, u * f, v * f1);
	tessellator.draw();
}

 

package com.sixonethree.snapshot.client.gui;

import java.util.ArrayList;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;

import org.lwjgl.opengl.GL11;

import com.sixonethree.snapshot.handler.ConfigurationHandler;
import com.sixonethree.snapshot.item.ItemLunchbox;

import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;

public class GuiLunchbox extends Gui {
private Minecraft MinecraftInstance;

public GuiLunchbox(Minecraft MC) {
	super();
	MinecraftInstance = MC;
}

public ItemStack[] getLocalInventory() {
	ItemStack[] Items = MinecraftInstance.thePlayer.inventory.mainInventory;
	return Items;
}

public boolean hasLunchbox() {
	ItemStack[] Items = getLocalInventory();
	for (ItemStack is : Items) {
		if (is != null) {
			if (is.getItem() != null) {
				if (is.getItem() instanceof ItemLunchbox) {
					return true;
				}
			}
		}
	}
	return false;
}

public ItemStack[] getLunchboxes() {
	ArrayList<ItemStack> Primitive = new ArrayList<ItemStack>();
	if (hasLunchbox()) {
		ItemStack[] Inventory = getLocalInventory();
		for (ItemStack Stack : Inventory) {
			if (Stack != null) {
				if (Stack.getItem() != null) {
					if (Stack.getItem() instanceof ItemLunchbox) {
						Primitive.add(Stack);
					}
				}
			}
		}
	}
	ItemStack[] Lunchboxes = new ItemStack[Primitive.size()];
	int Cur = 0;
	for (ItemStack Stack : Primitive) {
		Lunchboxes[Cur] = Stack;
		Cur ++;
	}
	return Lunchboxes;
}

public int getFoodStored() {
	int Stored = 0;
	if (hasLunchbox()) {
		for (ItemStack Lunchbox : getLunchboxes()) {
			if (Lunchbox.getTagCompound() != null) {
				float ThisStored = 0F;
				try {
					ThisStored = Lunchbox.getTagCompound().getFloat("Food_Stored");
				} catch (NullPointerException e) {
					ThisStored = 0F;
				}
				Stored = Stored + (int) (ThisStored / 2);
			}
		}
	}
	return Stored;
}

@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event) {
	if (event.isCanceled() || event.type != ElementType.FOOD || !hasLunchbox() || !ConfigurationHandler.getLunchboxOnScreen()) { return; }
	GL11.glColor4f(1F, 1F, 1F, 1F);
	GL11.glDisable(GL11.GL_LIGHTING);
	MinecraftInstance.renderEngine.bindTexture(new ResourceLocation("snapshot", "textures/gui/lunchbox.png"));
	ScaledResolution Scaled = new ScaledResolution(MinecraftInstance, MinecraftInstance.displayWidth, MinecraftInstance.displayHeight);
	int Food = getFoodStored();
	int Difference = 5 * (String.valueOf(Food).length()) + 4;
	int Width = Scaled.getScaledWidth() - Difference - 32;
	int Height = Scaled.getScaledHeight() - 32;
	drawNonStandardTexturedRect(Width, Height, 0, 0, 32, 32, 32, 32);
	MinecraftInstance.fontRenderer.drawString(String.valueOf(Food), Width + 32, Height + 16, 0);
}

protected void drawNonStandardTexturedRect(int x, int y, int u, int v, int width, int height, int textureWidth, int textureHeight) {
	double f = 1F / (double) textureWidth;
	double f1 = 1F / (double) textureHeight;
	Tessellator tessellator = Tessellator.instance;
	tessellator.startDrawingQuads();
	tessellator.addVertexWithUV(x, y + height, 0, u * f, (v + height) * f1);
	tessellator.addVertexWithUV(x + width, y + height, 0, (u + width) * f, (v + height) * f1);
	tessellator.addVertexWithUV(x + width, y, 0, (u + width) * f, v * f1);
	tessellator.addVertexWithUV(x, y, 0, u * f, v * f1);
	tessellator.draw();
}
}

 

w2j9ipQ.png

  • 3 years later...
Posted
On 02.09.2014 at 3:06 PM, 61352151511 said:

the bubble textures are messed up instead.

GL11.glColor4f(1, 1, 1, 1);
MinecraftInstance.renderEngine.bindTexture(new ResourceLocation("minecraft", "textures/gui/icons.png"));

Try adding this after all the rendering.

  • Guest locked this topic
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.