Advertisement
PaleoCrafter

RenderHelper

Apr 18th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.09 KB | None | 0 0
  1. package net.malisis.core.util;
  2.  
  3. import cpw.mods.fml.client.FMLClientHandler;
  4. import net.minecraft.client.Minecraft;
  5. import net.minecraft.client.renderer.Tessellator;
  6. import net.minecraft.util.ResourceLocation;
  7. import org.lwjgl.opengl.GL11;
  8. import org.lwjgl.opengl.GL12;
  9. import org.lwjgl.util.Color;
  10.  
  11. /**
  12.  * RenderHelper
  13.  *
  14.  * @author PaleoCrafter
  15.  */
  16. public class RenderHelper
  17. {
  18.  
  19.     public static void bindTexture(String path)
  20.     {
  21.         bindTexture(new ResourceLocation(path));
  22.     }
  23.  
  24.     public static void bindTexture(ResourceLocation path)
  25.     {
  26.         FMLClientHandler.instance().getClient().getTextureManager().bindTexture(path);
  27.     }
  28.  
  29.     public static int getColorFromRGB(int r, int g, int b)
  30.     {
  31.         return 0xFF0000 & r << 16 | 0x00FF00 & g << 8 | 0x0000FF & b;
  32.     }
  33.  
  34.     public static int getColorFromRGB(Color color)
  35.     {
  36.         return 0xFF0000 & color.getRed() << 16 | 0x00FF00 & color.getGreen() << 8 | 0x0000FF & color.getBlue();
  37.     }
  38.  
  39.     public static int getStringWidth(String text)
  40.     {
  41.         return getMC().fontRenderer.getStringWidth(text);
  42.     }
  43.  
  44.     public static Minecraft getMC()
  45.     {
  46.         return FMLClientHandler.instance().getClient();
  47.     }
  48.  
  49.     public static void drawString(String text, int x, int y, int color, boolean drawShadow, int zLevel)
  50.     {
  51.         GL11.glDisable(GL11.GL_DEPTH_TEST);
  52.         GL11.glDisable(GL12.GL_RESCALE_NORMAL);
  53.         GL11.glTranslatef(0, 0, zLevel);
  54.         getMC().fontRenderer.drawString(text, x, y, color, drawShadow);
  55.         GL11.glTranslatef(0, 0, -zLevel);
  56.         GL11.glEnable(GL12.GL_RESCALE_NORMAL);
  57.         GL11.glEnable(GL11.GL_DEPTH_TEST);
  58.     }
  59.  
  60.     public static void drawSplitString(String text, int x, int y, int color, boolean drawShadow)
  61.     {
  62.         String[] splits = text.split("<br>");
  63.         for (int i = 0; i < splits.length; i++)
  64.         {
  65.             getMC().fontRenderer.drawString(splits[i], x, y + i * 10, color, drawShadow);
  66.         }
  67.     }
  68.  
  69.     public static void drawLine(int color, int startX, int startY, int endX, int endY, float width, int zLevel)
  70.     {
  71.         drawLine(color, 1F, startX, startY, endX, endY, width, zLevel);
  72.     }
  73.  
  74.     public static void drawLine(int color, float alpha, int startX, int startY, int endX, int endY, float width, int zLevel)
  75.     {
  76.         Color rgb = RenderHelper.getRGBFromColor(color);
  77.         rgb.setAlpha((int) (alpha * 255));
  78.         drawLine(rgb, startX, startY, endX, endY, width, zLevel);
  79.     }
  80.  
  81.     public static Color getRGBFromColor(int color)
  82.     {
  83.         return new Color((0xFF0000 & color) >> 16, (0x00FF00 & color) >> 8, 0x0000FF & color);
  84.     }
  85.  
  86.     public static void drawLine(Color color, int startX, int startY, int endX, int endY, float width, int zLevel)
  87.     {
  88.         float colorMod = 1F / 255F;
  89.         GL11.glDisable(GL11.GL_TEXTURE_2D);
  90.         GL11.glEnable(GL11.GL_BLEND);
  91.         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  92.         GL11.glColor4f(colorMod * color.getRed(), colorMod * color.getGreen(), colorMod * color.getBlue(), colorMod * color.getAlpha());
  93.         GL11.glLineWidth(width);
  94.         GL11.glBegin(GL11.GL_LINES);
  95.         GL11.glVertex3f(startX, startY, zLevel);
  96.         GL11.glVertex3f(endX, endY, zLevel);
  97.         GL11.glEnd();
  98.         GL11.glDisable(GL11.GL_BLEND);
  99.         GL11.glColor4f(1F, 1F, 1F, 1F);
  100.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  101.     }
  102.  
  103.     public static void drawRectangle(int color, int x, int y, int width, int height, int zLevel)
  104.     {
  105.         drawRectangle(color, 1F, x, y, width, height, zLevel);
  106.     }
  107.  
  108.     public static void drawRectangle(int color, float alpha, int x, int y, int width, int height, int zLevel)
  109.     {
  110.         Color rgb = RenderHelper.getRGBFromColor(color);
  111.         rgb.setAlpha((int) (alpha * 255));
  112.         drawRectangle(rgb, x, y, width, height, zLevel);
  113.     }
  114.  
  115.     public static void drawRectangle(Color color, int x, int y, int width, int height, int zLevel)
  116.     {
  117.         float colorMod = 1F / 255F;
  118.         GL11.glDisable(GL11.GL_TEXTURE_2D);
  119.         GL11.glEnable(GL11.GL_BLEND);
  120.         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  121.         GL11.glColor4f(colorMod * color.getRed(), colorMod * color.getGreen(), colorMod * color.getBlue(), colorMod * color.getAlpha());
  122.         drawRectangle(x, y, 0, 0, width, height, zLevel);
  123.         GL11.glDisable(GL11.GL_BLEND);
  124.         GL11.glColor4f(1F, 1F, 1F, 1F);
  125.         GL11.glEnable(GL11.GL_TEXTURE_2D);
  126.     }
  127.  
  128.     public static void drawRectangle(ResourceLocation texture, int x, int y, float u, float v, int width, int height, int zLevel)
  129.     {
  130.         RenderHelper.bindTexture(texture);
  131.         float scaleU = 0.00390625F;
  132.         float scaleV = 0.00390625F;
  133.         if (u % 1 != 0) scaleU = 1;
  134.         if (v % 1 != 0) scaleV = 1;
  135.         Tessellator tessellator = Tessellator.instance;
  136.         tessellator.startDrawingQuads();
  137.         tessellator.addVertexWithUV(x, y + height, zLevel, u * scaleU, (v + height) * scaleV);
  138.         tessellator.addVertexWithUV(x + width, y + height, zLevel, (u + width) * scaleU, (v + height) * scaleV);
  139.         tessellator.addVertexWithUV(x + width, y, zLevel, (u + width) * scaleU, v * scaleV);
  140.         tessellator.addVertexWithUV(x, y, zLevel, u * scaleU, v * scaleV);
  141.         tessellator.draw();
  142.     }
  143.  
  144.     public static void drawRectangleStretched(ResourceLocation texture, int x, int y, float u, float v, int width, int height, float uOff, float vOff, int zLevel)
  145.     {
  146.         drawRectangleStretched(texture, x, y, u, v, width, height, u + uOff, v + vOff, true, zLevel);
  147.     }
  148.  
  149.     public static void drawRectangleStretched(ResourceLocation texture, int x, int y, float u, float v, int width, int height, float uMax, float vMax, boolean max, int zLevel)
  150.     {
  151.         if (max)
  152.         {
  153.             bindTexture(texture);
  154.             drawRectangleStretched(x, y, u, v, width, height, uMax, vMax, zLevel);
  155.         }
  156.         else
  157.         {
  158.             drawRectangleStretched(texture, x, y, u, v, width, height, uMax, vMax, zLevel);
  159.         }
  160.     }
  161.  
  162.     public static void drawRectangleStretched(int x, int y, float u, float v, int width, int height, float uMax, float vMax, int zLevel)
  163.     {
  164.         float scaleU = 0.00390625F;
  165.         float scaleV = 0.00390625F;
  166.         if (u % 1 != 0 || uMax % 1 != 0) scaleU = 1;
  167.         if (v % 1 != 0 || vMax % 1 != 0) scaleV = 1;
  168.         Tessellator tessellator = Tessellator.instance;
  169.         tessellator.startDrawingQuads();
  170.         tessellator.addVertexWithUV(x, y + height, zLevel, u * scaleU, vMax * scaleV);
  171.         tessellator.addVertexWithUV(x + width, y + height, zLevel, uMax * scaleU, vMax * scaleV);
  172.         tessellator.addVertexWithUV(x + width, y, zLevel, uMax * scaleU, v * scaleV);
  173.         tessellator.addVertexWithUV(x, y, zLevel, u * scaleU, v * scaleV);
  174.         tessellator.draw();
  175.     }
  176.  
  177.     public static void drawRectangleRepeated(ResourceLocation texture, int x, int y, float u, float v, float uvWidth, float uvHeight, int width, int height, int zLevel)
  178.     {
  179.         drawRectangleRepeated(texture, x, y, u, v, uvWidth, uvHeight, width, height, (int) uvWidth, (int) uvHeight, zLevel);
  180.     }
  181.  
  182.     public static void drawRectangleRepeated(ResourceLocation texture, int x, int y, float u, float v, float uMax, float vMax, int width, int height, int tileWidth, int tileHeight, int zLevel)
  183.     {
  184.         float uvHeight = v - vMax;
  185.         int numX = (int) Math.ceil((float) width / tileWidth);
  186.         int numY = (int) Math.ceil((float) height / tileHeight);
  187.  
  188.         for (int y2 = 0; y2 < numY; ++y2)
  189.             for (int x2 = 0; x2 < numX; ++x2)
  190.             {
  191.                 int w = tileWidth;
  192.                 int h = tileHeight;
  193.  
  194.                 float tileMaxU = uMax;
  195.                 float tileMaxV = vMax;
  196.  
  197.                 float tileV = v;
  198.  
  199.                 int tileX = w * x2;
  200.                 int tileY = h * y2 + h;
  201.  
  202.                 if (tileWidth > width)
  203.                 {
  204.                     w = width;
  205.                     tileMaxU -= 0.00390625F * (float) w / tileWidth;
  206.                     tileX = w * x2;
  207.                 }
  208.                 else if (x2 == numX - 1)
  209.                 {
  210.                     if (tileWidth > width - x2 * tileWidth)
  211.                     {
  212.                         w = width - x2 * tileWidth;
  213.                         tileMaxU -= 0.00390625F * (float) w / tileWidth;
  214.                         tileX = tileWidth * x2;
  215.                     }
  216.                 }
  217.  
  218.                 if (tileHeight > height)
  219.                 {
  220.                     h = height;
  221.                     tileMaxV -= 0.00390625F * (float) h / tileHeight;
  222.                     tileY = h * y2 + h;
  223.                 }
  224.                 else if (y2 == numY - 1)
  225.                 {
  226.                     if (tileHeight > height - (y2 - 1) * tileHeight)
  227.                     {
  228.                         h = height - (y2 - 1) * tileHeight;
  229.                         tileV += uvHeight - 0.00390625F * (float) h / tileHeight;
  230.                         tileY = tileHeight * y2 + h;
  231.                     }
  232.                 }
  233.  
  234.                 drawRectangleStretched(texture, x + tileX, y + height - tileY, u, tileV, w, h, tileMaxU, tileMaxV, true, zLevel);
  235.             }
  236.     }
  237.  
  238.     public static void drawRectangleXRepeated(ResourceLocation texture, int x, int y, float u, float v, float uvWidth, float uvHeight, int width, int height, int zLevel)
  239.     {
  240.         RenderHelper.bindTexture(texture);
  241.         float f = 0.00390625F;
  242.         float f1 = 0.00390625F;
  243.         if (u % 1 != 0) f = 1;
  244.         if (v % 1 != 0) f1 = 1;
  245.         Tessellator tessellator = Tessellator.instance;
  246.  
  247.         boolean flipX = width < 0;
  248.         if (flipX) width *= -1;
  249.  
  250.         int numX = (int) Math.ceil((float) width / uvWidth);
  251.  
  252.         for (int x2 = 0; x2 < numX; ++x2)
  253.         {
  254.             float xOffset = x2 * uvWidth;
  255.             if (flipX) xOffset = width - (x2 + 1) * uvWidth;
  256.  
  257.             tessellator.startDrawingQuads();
  258.             tessellator.addVertexWithUV(x + xOffset, y + height, zLevel, u * f, (v + uvHeight) * f1);
  259.             tessellator.addVertexWithUV(x + uvWidth + xOffset, y + height, zLevel, (u + uvWidth) * f, (v + uvHeight) * f1);
  260.             tessellator.addVertexWithUV(x + uvWidth + xOffset, y, zLevel, (u + uvWidth) * f, v * f1);
  261.             tessellator.addVertexWithUV(x + xOffset, y, zLevel, u * f, v * f1);
  262.             tessellator.draw();
  263.         }
  264.     }
  265.  
  266.     public static void drawRectangleYRepeated(ResourceLocation texture, int x, int y, float u, float v, float uvWidth, float uvHeight, int width, int height, int zLevel)
  267.     {
  268.         RenderHelper.bindTexture(texture);
  269.         float scaleU = 0.00390625F;
  270.         float scaleV = 0.00390625F;
  271.         if (u % 1 != 0) scaleU = 1;
  272.         if (v % 1 != 0) scaleV = 1;
  273.         Tessellator tessellator = Tessellator.instance;
  274.  
  275.         boolean flipY = height < 0;
  276.         if (flipY) height *= -1;
  277.  
  278.         int numY = (int) Math.ceil((float) height / uvHeight);
  279.  
  280.         for (int y2 = 0; y2 < numY; ++y2)
  281.         {
  282.             float yOffset = y2 * uvHeight;
  283.             if (flipY) yOffset = height - (y2 + 1) * uvHeight;
  284.  
  285.             tessellator.startDrawingQuads();
  286.             tessellator.addVertexWithUV(x, y + uvHeight + yOffset, zLevel, u * scaleU, (v + uvHeight) * scaleV);
  287.             tessellator.addVertexWithUV(x + width, y + uvHeight + yOffset, zLevel, (u + uvWidth) * scaleU, (v + uvHeight) * scaleV);
  288.             tessellator.addVertexWithUV(x + width, y + yOffset, zLevel, (u + uvWidth) * scaleU, v * scaleV);
  289.             tessellator.addVertexWithUV(x, y + yOffset, zLevel, u * scaleU, v * scaleV);
  290.             tessellator.draw();
  291.         }
  292.     }
  293.  
  294.     public static int computeGuiScale()
  295.     {
  296.         Minecraft mc = Minecraft.getMinecraft();
  297.         int scaleFactor = 1;
  298.  
  299.         int k = mc.gameSettings.guiScale;
  300.  
  301.         if (k == 0)
  302.         {
  303.             k = 1000;
  304.         }
  305.  
  306.         while (scaleFactor < k && mc.displayWidth / (scaleFactor + 1) >= 320 && mc.displayHeight / (scaleFactor + 1) >= 240)
  307.         {
  308.             ++scaleFactor;
  309.         }
  310.         return scaleFactor;
  311.     }
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement