diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-04-04 14:53:10 +0200 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-04-04 14:53:10 +0200 |
commit | ddca3a3232eff15a7130efda03e7e5c408554412 (patch) | |
tree | ef7678412590ca432bb2cd25aabfc3bf2ea4b7b0 /src/main/java/io/polyfrost/oneconfig/renderer | |
parent | 1210e38c2ff569a28b20d7d0182557fbf386d524 (diff) | |
parent | 2696141f9790fd6c8d3df1148f46d298512c4902 (diff) | |
download | OneConfig-ddca3a3232eff15a7130efda03e7e5c408554412.tar.gz OneConfig-ddca3a3232eff15a7130efda03e7e5c408554412.tar.bz2 OneConfig-ddca3a3232eff15a7130efda03e7e5c408554412.zip |
merge
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/renderer')
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java | 35 | ||||
-rw-r--r-- | src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java | 269 |
2 files changed, 142 insertions, 162 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java b/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java index 44e1b34..b77b8cd 100644 --- a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java +++ b/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java @@ -10,7 +10,7 @@ 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.*; +import org.lwjgl.opengl.GL11; import java.awt.*; @@ -22,11 +22,32 @@ public class Renderer extends Gui { private static final WorldRenderer worldRenderer = tessellator.getWorldRenderer(); - public static void drawRectangle(int left, int top, int right, int bottom, int color) { - Gui.drawRect(left, top, right, bottom, color); + /** + * 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 drawTextScale(String text, float x, float y, int color, boolean shadow, float scale) { + 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); @@ -42,7 +63,7 @@ public class Renderer extends Gui { public static void drawRegularPolygon(double x, double y, int radius, int sides, int color, double lowerAngle, double upperAngle) { GL11.glDisable(GL11.GL_TEXTURE_2D); - setGlColor(color); + color(color); GlStateManager.enableBlend(); GlStateManager.disableAlpha(); worldRenderer.begin(GL11.GL_POLYGON, DefaultVertexFormats.POSITION); @@ -120,14 +141,14 @@ public class Renderer extends Gui { /** * Set GL color from the given Color variable. */ - public static void setGlColor(Color color) { + 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 setGlColor(int color) { + 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; diff --git a/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java b/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java index 9a51e68..d26b953 100644 --- a/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java +++ b/src/main/java/io/polyfrost/oneconfig/renderer/TrueTypeFont.java @@ -29,10 +29,7 @@ import java.util.Map; * @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; + public final static int ALIGN_LEFT = 0, ALIGN_RIGHT = 1, ALIGN_CENTER = 2; /** * Array that holds necessary information about the font characters */ @@ -52,54 +49,26 @@ public class TrueTypeFont { * Font's size */ private final int fontSize; - - /** - * Font's height - */ - private int fontHeight = 0; - - /** - * Texture used to cache the font 0-255 characters - */ - private int fontTextureID; - /** * 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; - - - 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; - } + /** + * 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) { @@ -113,18 +82,86 @@ public class TrueTypeFont { 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); + 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.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } g.setFont(font); FontMetrics fontMetrics = g.getFontMetrics(); @@ -140,20 +177,17 @@ public class TrueTypeFont { // Create another image holding the character we are creating BufferedImage fontImage; - fontImage = new BufferedImage(charwidth, charheight, - BufferedImage.TYPE_INT_ARGB); + 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.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()); + gt.drawString(String.valueOf(ch), (charx), (chary) + fontMetrics.getAscent()); return fontImage; @@ -223,8 +257,7 @@ public class TrueTypeFont { } } - private void drawQuad(float drawX, float drawY, float drawX2, float drawY2, - float srcX, float srcY, float srcX2, float srcY2) { + 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; @@ -260,8 +293,7 @@ public class TrueTypeFont { intObject = customChars.get((char) currentChar); } - if (intObject != null) - totalWidth += intObject.width; + if (intObject != null) totalWidth += intObject.width; } return totalWidth; } @@ -281,18 +313,18 @@ public class TrueTypeFont { String[] words = text.split("\\W+"); int totalWidth = 0; String line = ""; - for(String word : words) { + 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 + 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) + if (!line.equals("")) { // add extra if there is any (last line) splitString.add(line); } int i1 = 0; @@ -305,10 +337,6 @@ public class TrueTypeFont { } } - - - - 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; @@ -356,7 +384,7 @@ public class TrueTypeFont { GlStateManager.enableAlpha(); GlStateManager.enableBlend(); - Renderer.setGlColor(color); + Renderer.color(color); while (i >= startIndex && i <= endIndex) { charCurrent = text.charAt(i); if (charCurrent < 256) { @@ -367,11 +395,7 @@ public class TrueTypeFont { 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); + 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; @@ -393,94 +417,7 @@ public class TrueTypeFont { i += d; } GlStateManager.disableBlend(); - GlStateManager.color(1f,1f,1f,1f); - } - - 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}; + GlStateManager.color(1f, 1f, 1f, 1f); } public void destroy() { @@ -489,4 +426,26 @@ public class TrueTypeFont { 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; + } } |