Skip to content

Instantly share code, notes, and snippets.

@thvortex
Created March 20, 2012 17:14
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thvortex/2138298 to your computer and use it in GitHub Desktop.
Save thvortex/2138298 to your computer and use it in GitHub Desktop.
Drawing a textured rectangle in a Minecraft GUI
// HOW TO DRAW A TEXTURED RECTANGLE IN A MINECRAFT GUI WITH THE TEXTURE SCALED TO FIT
//
//
// 1. You need an instance of RenderEngine. Using ModLoader you can obtain it with:
//
RenderEngine renderEngine = ModLoader.getMinecraftInstance().renderEngine;
// 2a. When your mod first starts, you need to load your texture into OpenGL. If your image
// file is on the classpath (i.e. inside minecraft.jar or inside your own mod's jar) then
// you can load it easily. Your image file should contain an alpha layer with only 0 or 255
// values. Load the file using (the leading slash is important here):
int myTextureName = renderEngine.getTexture("/foobar.png");
// 2b. Alternatively, if you are loading the texture from the network or computing it on the fly,
// you'll have to provide your own code to setup a BufferedImage with your texture image
// (use TYPE_INT_ARGB for the imageType). Then you can load it with:
int myTextureName = renderEngine.allocateAndSetupTexture(myBufferedImage);
// 3. You'll need to write your own version of the Gui.drawTexturedModalRect() method
// This method can go into your own Gui class:
public void myDrawTexturedModalRect(int x, int y, int width, int height)
{
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawingQuads();
tessellator.addVertexWithUV(x , y + height, 0, 0.0, 1.0);
tessellator.addVertexWithUV(x + width, y + height, 0, 1.0, 1.0);
tessellator.addVertexWithUV(x + width, y , 0, 1.0, 0.0);
tessellator.addVertexWithUV(x , y , 0, 0.0, 0.0);
tessellator.draw();
}
// 4. Somewhere in your own Gui class's drawScreen() method, you draw the rectangle with:
renderEngine.bindTexture(myTextureName);
myDrawTextureModalRect(x, y, w, h);
// NOTE: Your texture image does not have to be square but its width and height should both
// be a power of 2 like 32x64, 16x128, 8x4, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment