diff options
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/renderer')
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java | 189 | ||||
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java | 451 |
2 files changed, 0 insertions, 640 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java b/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java deleted file mode 100644 index b77b8cd..0000000 --- a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java +++ /dev/null @@ -1,189 +0,0 @@ -package io.polyfrost.oneconfig.renderer; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.gui.Gui; -import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.WorldRenderer; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import net.minecraft.util.ResourceLocation; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.lwjgl.opengl.GL11; - -import java.awt.*; - -public class Renderer extends Gui { - public static final Logger renderLog = LogManager.getLogger("OneConfig Renderer"); - private static final Minecraft mc = Minecraft.getMinecraft(); - private static final FontRenderer fr = mc.fontRendererObj; - private static final Tessellator tessellator = Tessellator.getInstance(); - private static final WorldRenderer worldRenderer = tessellator.getWorldRenderer(); - - - /** - * Draw a basic rectangle. Please note that this is to be used WITH a {@link net.minecraft.client.renderer.GlStateManager#color(float, float, float)} before to color it. - */ - public static void drawRectangle(int x, int y, int width, int height) { - int right = x + width; - int bottom = y + height; - if (x < right) { - x = right; - } - if (y < bottom) { - y = bottom; - } - GlStateManager.enableBlend(); - GlStateManager.disableTexture2D(); - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); - worldRenderer.begin(7, DefaultVertexFormats.POSITION); - worldRenderer.pos(x, bottom, 0.0D).endVertex(); - worldRenderer.pos(right, bottom, 0.0D).endVertex(); - worldRenderer.pos(right, y, 0.0D).endVertex(); - worldRenderer.pos(x, y, 0.0D).endVertex(); - tessellator.draw(); - GlStateManager.enableTexture2D(); - GlStateManager.disableBlend(); - } - - public static void drawScaledString(String text, float x, float y, int color, boolean shadow, float scale) { - GlStateManager.pushMatrix(); - GlStateManager.scale(scale, scale, 1); - mc.fontRendererObj.drawString(text, x * (1 / scale), y * (1 / scale), color, shadow); - GlStateManager.popMatrix(); - } - - public static void drawScaledImage(ResourceLocation location, int x, int y, int targetX, int targetY) { - GlStateManager.enableBlend(); - GlStateManager.color(1f, 1f, 1f, 1f); - mc.getTextureManager().bindTexture(location); - Gui.drawScaledCustomSizeModalRect(x, y, 0, 0, targetX, targetY, targetX, targetY, targetX, targetY); - } - - public static void drawRegularPolygon(double x, double y, int radius, int sides, int color, double lowerAngle, double upperAngle) { - GL11.glDisable(GL11.GL_TEXTURE_2D); - color(color); - GlStateManager.enableBlend(); - GlStateManager.disableAlpha(); - worldRenderer.begin(GL11.GL_POLYGON, DefaultVertexFormats.POSITION); - worldRenderer.pos(x, y, 0).endVertex(); - //GL11.glHint(GL11.GL_POLYGON_SMOOTH_HINT, GL11.GL_NICEST); - //GL11.glEnable(GL11.GL_POLYGON_SMOOTH); - //GL11.glCullFace(GL11.GL_FRONT); - //GL11.glCullFace(GL11.GL_FRONT_AND_BACK); - //GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_FILL); - //GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE); - //GL11.glEnable(ARBMultisample.GL_MULTISAMPLE_ARB); - - - for (int i = 0; i <= sides; i++) { - double angle = ((Math.PI * 2) * i / sides) + Math.toRadians(180); - if (angle > lowerAngle && angle < upperAngle) { // >0 <4.75; >4.7 <6.3; >6.25 <7.9; >7.8 <10 80 side mode - worldRenderer.pos(x + Math.sin(angle) * radius, y + Math.cos(angle) * radius, 0).endVertex(); - } - } - tessellator.draw(); - GlStateManager.disableBlend(); - //GL11.glDisable(GL11.GL_POLYGON_SMOOTH); - GL11.glEnable(GL11.GL_TEXTURE_2D); - } - - public static void drawRegularPolygon(double x, double y, int radius, int sides, int color) { - drawRegularPolygon(x, y, radius, sides, color, 0d, 10000d); - } - - /** - * Draw a round rectangle at the given coordinates. - * - * @param radius radius of the corners - * @param color color as a rgba integer - */ - public static void drawRoundRect(int x, int y, int width, int height, int radius, int color) { - GL11.glEnable(GL11.GL_BLEND); - //GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE); - Gui.drawRect(x + radius, y, (x + width - radius), (y + radius), color); // top - Gui.drawRect(x + radius, (y + height - radius), (x + width - radius), (y + height), color); // bottom - Gui.drawRect(x, (y + radius), (x + width), (y + height - radius), color); // main - drawRegularPolygon(x + radius, y + radius, radius, 80, color, 0d, 4.75d); // top left - drawRegularPolygon(x + width - radius, y + radius, radius, 80, color, 7.8d, 10d); // top right - drawRegularPolygon(x + radius, y + height - radius, radius, 80, color, 4.7d, 6.3d); // bottom left - drawRegularPolygon(x + width - radius, y + height - radius, radius, 80, color, 6.25d, 7.9d); // bottom right - GL11.glDisable(GL11.GL_BLEND); - GL11.glColor4f(1f, 1f, 1f, 1f); - } - - public static float clamp(float number) { - return number < (float) 0.0 ? (float) 0.0 : Math.min(number, (float) 1.0); - } - - public static float easeOut(float current, float goal) { - if (Math.floor(Math.abs(goal - current) / (float) 0.01) > 0) { - return current + (goal - current) / (float) 20.0; - } else { - return goal; - } - } - - /** - * Return a java.awt.Color object from the given Integer. - * - * @param color rgba color, parsed into an integer - */ - public static Color getColorFromInt(int color) { - float f = (float) (color >> 16 & 255) / 255.0F; - float f1 = (float) (color >> 8 & 255) / 255.0F; - float f2 = (float) (color & 255) / 255.0F; - float f3 = (float) (color >> 24 & 255) / 255.0F; - return new Color(f, f1, f2, f3); - } - - /** - * Set GL color from the given Color variable. - */ - public static void color(Color color) { - GlStateManager.color(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, color.getAlpha() / 255f); - } - - /** - * Set GL color from the given color as an Integer. - */ - public static void color(int color) { - float f1 = (float) (color >> 8 & 255) / 255.0F; - float f = (float) (color >> 16 & 255) / 255.0F; - float f2 = (float) (color & 255) / 255.0F; - float f3 = (float) (color >> 24 & 255) / 255.0F; - GlStateManager.color(f, f1, f2, f3); - } - - public static void drawLine(float sx, float sy, float ex, float ey, int width, int color) { - float f = (float) (color >> 24 & 255) / 255.0F; - float f1 = (float) (color >> 16 & 255) / 255.0F; - float f2 = (float) (color >> 8 & 255) / 255.0F; - float f3 = (float) (color & 255) / 255.0F; - GlStateManager.pushMatrix(); - GlStateManager.disableTexture2D(); - GlStateManager.enableBlend(); - GlStateManager.disableAlpha(); - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); - GlStateManager.color(f1, f2, f3, f); - GL11.glLineWidth(width); - GL11.glBegin(GL11.GL_LINES); - GL11.glVertex2d(sx, sy); - GL11.glVertex2d(ex, ey); - GL11.glEnd(); - GlStateManager.disableBlend(); - GlStateManager.enableAlpha(); - GlStateManager.enableTexture2D(); - GlStateManager.popMatrix(); - } - - public static void drawDottedLine(float sx, float sy, float ex, float ey, int width, int factor, int color) { - GlStateManager.pushMatrix(); - GL11.glLineStipple(factor, (short) 0xAAAA); - GL11.glEnable(GL11.GL_LINE_STIPPLE); - drawLine(sx, sy, ex, ey, width, color); - GL11.glDisable(GL11.GL_LINE_STIPPLE); - GlStateManager.popMatrix(); - } -} diff --git a/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java b/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java deleted file mode 100644 index d26b953..0000000 --- a/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java +++ /dev/null @@ -1,451 +0,0 @@ -package io.polyfrost.oneconfig.renderer; - -import net.minecraft.client.renderer.GlStateManager; -import org.lwjgl.BufferUtils; -import org.lwjgl.opengl.GL11; -import org.lwjgl.util.glu.GLU; - -import java.awt.*; -import java.awt.image.BufferedImage; -import java.awt.image.DataBuffer; -import java.awt.image.DataBufferByte; -import java.awt.image.DataBufferInt; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.IntBuffer; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - - -/** - * A TrueType font implementation originally for Slick, then edited for Bobjob's Engine, now for Minecraft - * - * @author James Chambers (Jimmy) (original in Slick) - * @author Jeremy Adams (elias4444) (original in Slick) - * @author Kevin Glass (kevglass) (original in Slick) - * @author Peter Korzuszek (genail) (original in Slick) - * @author version edited by David Aaron Muhar (bobjob) (modified in Bobjob's Engine) - */ -public class TrueTypeFont { - public final static int ALIGN_LEFT = 0, ALIGN_RIGHT = 1, ALIGN_CENTER = 2; - /** - * Array that holds necessary information about the font characters - */ - private final IntObject[] charArray = new IntObject[256]; - - /** - * Map of user defined font characters (Character <-> IntObject) - */ - private final Map<Character, IntObject> customChars = new HashMap<>(); - - /** - * Boolean flag on whether AntiAliasing is enabled or not - */ - private final boolean antiAlias; - - /** - * Font's size - */ - private final int fontSize; - /** - * Default font texture width - */ - private final int textureWidth = 512; - /** - * Default font texture height - */ - private final int textureHeight = 512; - /** - * A reference to Java's AWT Font that we create our font texture from - */ - private final Font font; - /** - * Font's height - */ - private int fontHeight = 0; - /** - * Texture used to cache the font 0-255 characters - */ - private int fontTextureID; - - - public TrueTypeFont(Font font, boolean antiAlias, char[] additionalChars) { - this.font = font; - this.fontSize = font.getSize() + 3; - this.antiAlias = antiAlias; - - createSet(additionalChars); - - fontHeight -= 1; - if (fontHeight <= 0) fontHeight = 1; - } - - - public TrueTypeFont(Font font, boolean antiAlias) { - this(font, antiAlias, null); - } - - public static int loadImage(BufferedImage bufferedImage) { - try { - short width = (short) bufferedImage.getWidth(); - short height = (short) bufferedImage.getHeight(); - //textureLoader.bpp = bufferedImage.getColorModel().hasAlpha() ? (byte)32 : (byte)24; - int bpp = (byte) bufferedImage.getColorModel().getPixelSize(); - ByteBuffer byteBuffer; - DataBuffer db = bufferedImage.getData().getDataBuffer(); - if (db instanceof DataBufferInt) { - int[] intI = ((DataBufferInt) (bufferedImage.getData().getDataBuffer())).getData(); - byte[] newI = new byte[intI.length * 4]; - for (int i = 0; i < intI.length; i++) { - byte[] b = intToByteArray(intI[i]); - int newIndex = i * 4; - - newI[newIndex] = b[1]; - newI[newIndex + 1] = b[2]; - newI[newIndex + 2] = b[3]; - newI[newIndex + 3] = b[0]; - } - - byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)).order(ByteOrder.nativeOrder()).put(newI); - } else { - byteBuffer = ByteBuffer.allocateDirect(width * height * (bpp / 8)).order(ByteOrder.nativeOrder()).put(((DataBufferByte) (bufferedImage.getData().getDataBuffer())).getData()); - } - byteBuffer.flip(); - - - int internalFormat = GL11.GL_RGBA8, format = GL11.GL_RGBA; - IntBuffer textureId = BufferUtils.createIntBuffer(1); - - GL11.glGenTextures(textureId); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId.get(0)); - - - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP); - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP); - - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); - GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); - - GL11.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_MODULATE); - - - GLU.gluBuild2DMipmaps(GL11.GL_TEXTURE_2D, internalFormat, width, height, format, GL11.GL_UNSIGNED_BYTE, byteBuffer); - return textureId.get(0); - - } catch (Exception e) { - e.printStackTrace(); - } - return -1; - } - - public static boolean isSupported(String fontname) { - Font[] font = getFonts(); - for (int i = font.length - 1; i >= 0; i--) { - if (font[i].getName().equalsIgnoreCase(fontname)) return true; - } - return false; - } - - public static Font[] getFonts() { - return GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); - } - - public static byte[] intToByteArray(int value) { - return new byte[]{(byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value}; - } - - private BufferedImage getFontImage(char ch) { - // Create a temporary image to extract the character's size - BufferedImage tempfontImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); - Graphics2D g = (Graphics2D) tempfontImage.getGraphics(); - if (antiAlias) { - g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - } - g.setFont(font); - FontMetrics fontMetrics = g.getFontMetrics(); - int charwidth = fontMetrics.charWidth(ch) + 8; - - if (charwidth <= 0) { - charwidth = 7; - } - int charheight = fontMetrics.getHeight() + 3; - if (charheight <= 0) { - charheight = fontSize; - } - - // Create another image holding the character we are creating - BufferedImage fontImage; - fontImage = new BufferedImage(charwidth, charheight, BufferedImage.TYPE_INT_ARGB); - Graphics2D gt = (Graphics2D) fontImage.getGraphics(); - if (antiAlias) { - gt.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - } - gt.setFont(font); - - gt.setColor(Color.WHITE); - int charx = 3; - int chary = 1; - gt.drawString(String.valueOf(ch), (charx), (chary) + fontMetrics.getAscent()); - - return fontImage; - - } - - private void createSet(char[] customCharsArray) { - try { - BufferedImage imgTemp = new BufferedImage(textureWidth, textureHeight, BufferedImage.TYPE_INT_ARGB); - Graphics2D g = (Graphics2D) imgTemp.getGraphics(); - - g.setColor(new Color(0, 0, 0, 1)); - g.fillRect(0, 0, textureWidth, textureHeight); - - int customCharsLength = (customCharsArray != null) ? customCharsArray.length : 0; - int rowHeight = 0; - int positionX = 0; - int positionY = 0; - - // ignore some characters because they don't have visual representation - for (int i = 0; i < 224 + customCharsLength; i++) { - if (i >= 95 && i <= 128) continue; - - char ch = (i < 224) ? (char) (i + 32) : customCharsArray[i - 224]; - - BufferedImage fontImage = getFontImage(ch); - - IntObject newIntObject = new IntObject(); - - newIntObject.width = fontImage.getWidth(); - newIntObject.height = fontImage.getHeight(); - - if (positionX + newIntObject.width >= textureWidth) { - positionX = 0; - positionY += rowHeight; - rowHeight = 0; - } - - newIntObject.storedX = positionX; - newIntObject.storedY = positionY; - - if (newIntObject.height > fontHeight) { - fontHeight = newIntObject.height; - } - - if (newIntObject.height > rowHeight) { - rowHeight = newIntObject.height; - } - - // Draw it here - g.drawImage(fontImage, positionX, positionY, null); - - positionX += newIntObject.width; - - if (i < 224) { // standard characters - charArray[i + 32] = newIntObject; - } else { // custom characters - customChars.put(ch, newIntObject); - } - } - - fontTextureID = loadImage(imgTemp); - - //ImageIO.write(imgTemp, "png", new File("./OneConfig/bitmap.png")); - } catch (Exception e) { - System.err.println("Failed to create font."); - e.printStackTrace(); - } - } - - private void drawQuad(float drawX, float drawY, float drawX2, float drawY2, float srcX, float srcY, float srcX2, float srcY2) { - float DrawWidth = drawX2 - drawX; - float DrawHeight = drawY2 - drawY; - float TextureSrcX = srcX / textureWidth; - float TextureSrcY = srcY / textureHeight; - float SrcWidth = srcX2 - srcX; - float SrcHeight = srcY2 - srcY; - float RenderWidth = (SrcWidth / textureWidth); - float RenderHeight = (SrcHeight / textureHeight); - - GlStateManager.bindTexture(fontTextureID); - - GL11.glBegin(GL11.GL_TRIANGLE_STRIP); - GL11.glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY); // 2 - GL11.glVertex2f(drawX + DrawWidth, drawY + DrawHeight); // 1 - GL11.glTexCoord2f(TextureSrcX, TextureSrcY); // 1 - GL11.glVertex2f(drawX, drawY + DrawHeight); // 2 - GL11.glTexCoord2f(TextureSrcX + RenderWidth, TextureSrcY + RenderHeight); // 4 - GL11.glVertex2f(drawX + DrawWidth, drawY); // 3 - GL11.glTexCoord2f(TextureSrcX, TextureSrcY + RenderHeight); // 3 - GL11.glVertex2f(drawX, drawY); // 4 - GL11.glEnd(); - } - - public int getWidth(String text) { - int totalWidth = 0; - IntObject intObject; - int currentChar; - for (int i = 0; i < text.length(); i++) { - currentChar = text.charAt(i); - if (currentChar < 256) { - intObject = charArray[currentChar]; - } else { - intObject = customChars.get((char) currentChar); - } - - if (intObject != null) totalWidth += intObject.width; - } - return totalWidth; - } - - public int getHeight() { - return fontHeight; - } - - public void drawString(String text, float x, float y, float scaleX, float scaleY, int color) { - drawString(text, x, y, scaleX, scaleY, ALIGN_LEFT, color); - } - - public void drawSplitString(String text, float x, float y, int wrapWidth, int color) { - try { // time taken: 0.035ms to do complete cycle - wrapWidth += 140; // it needs this extra to work properly (why?) - List<String> splitString = new ArrayList<>(); - String[] words = text.split("\\W+"); - int totalWidth = 0; - String line = ""; - for (String word : words) { - int width = getWidth(word); - word += " "; // add the space - totalWidth += width; - line += word; - if (totalWidth >= wrapWidth) { // wrap line if it is too long - splitString.add(line); - totalWidth = 0; - line = ""; - } - } - if (!line.equals("")) { // add extra if there is any (last line) - splitString.add(line); - } - int i1 = 0; - for (String string : splitString) { - drawString(string, x, y + i1, 1f, 1f, color); // draw it - i1 += getHeight(); - } - } catch (Exception e) { // be safe kids - e.printStackTrace(); - } - } - - public void drawString(String text, float x, float y, float scaleX, float scaleY, int format, int color) { - int startIndex = 0; - int endIndex = text.length() - 1; - IntObject intObject; - int charCurrent; - - - int totalWidth = 0; - int i = startIndex, d, c; - float startY = 0; - - switch (format) { - case ALIGN_RIGHT: { - d = -1; - c = 8; - - while (i < endIndex) { - if (text.charAt(i) == '\n') startY -= fontHeight; - i++; - } - break; - } - case ALIGN_CENTER: { - for (int l = startIndex; l <= endIndex; l++) { - charCurrent = text.charAt(l); - if (charCurrent == '\n') break; - if (charCurrent < 256) { - intObject = charArray[charCurrent]; - } else { - intObject = customChars.get((char) charCurrent); - } - totalWidth += intObject.width - 9; - } - totalWidth /= -9; - } - case ALIGN_LEFT: - default: { - d = 1; - c = 9; - break; - } - - } - - GlStateManager.enableAlpha(); - GlStateManager.enableBlend(); - - Renderer.color(color); - while (i >= startIndex && i <= endIndex) { - charCurrent = text.charAt(i); - if (charCurrent < 256) { - intObject = charArray[charCurrent]; - } else { - intObject = customChars.get((char) charCurrent); - } - - if (intObject != null) { - if (d < 0) totalWidth += (intObject.width - c) * d; - drawQuad((totalWidth + intObject.width) * scaleX + x, startY * scaleY + y, totalWidth * scaleX + x, (startY + intObject.height) * scaleY + y, intObject.storedX + intObject.width, intObject.storedY + intObject.height, intObject.storedX, intObject.storedY); - if (d > 0) totalWidth += (intObject.width - c) * d; - } else if (charCurrent == '\n') { - startY += fontHeight * d; - totalWidth = 0; - if (format == ALIGN_CENTER) { - for (int l = i + 1; l <= endIndex; l++) { - charCurrent = text.charAt(l); - if (charCurrent == '\n') break; - if (charCurrent < 256) { - intObject = charArray[charCurrent]; - } else { - intObject = customChars.get((char) charCurrent); - } - totalWidth += intObject.width - 9; - } - totalWidth /= -2; - } - } - i += d; - } - GlStateManager.disableBlend(); - GlStateManager.color(1f, 1f, 1f, 1f); - } - - public void destroy() { - IntBuffer scratch = BufferUtils.createIntBuffer(1); - scratch.put(0, fontTextureID); - GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); - GL11.glDeleteTextures(scratch); - } - - private static class IntObject { - /** - * Character's width - */ - public int width; - - /** - * Character's height - */ - public int height; - - /** - * Character's stored x position - */ - public int storedX; - - /** - * Character's stored y position - */ - public int storedY; - } -} |