Welcome on MasterOf13FPS! MasterOf13FPS

Register today or sign up if you are already a member and never miss any cool content again :)

Custom Font Renderer Issue

MasterOf13FPS

Administrator
Staff member
Administrator
MasterOf13FPS
Joined
Jul 11, 2020
Messages
0
Reaction score
26
Points
0
So, basically, I have an issue with the color codes with my custom font renderer. I don't know what's wrong.

815f982c83955713232666c019626584.png


This is my font renderer class.It is basically just the Darkstorm font renderer.
Code:
/**
     * This is the actual custom font.
     */
    private UnicodeFont unicodeFont;

    /**
     * This constructor will take in a font as it parameters.
     */
    public PeepFont(Font font) {
        super(Wrapper.getMinecraft().gameSettings, new ResourceLocation("textures/font/ascii.png"),
                Wrapper.getMinecraft().getTextureManager(), false);
        unicodeFont = new UnicodeFont(font);
        unicodeFont.addAsciiGlyphs();
        unicodeFont.getEffects().add(new ColorEffect(Color.white));
        try {
            unicodeFont.loadGlyphs();
        } catch (SlickException ex) {
            ex.printStackTrace();
        }
        String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        FONT_HEIGHT = unicodeFont.getHeight(alphabet) / 2;
    }
    
    public int drawString(String string, int x, int y, int color) {
        if(string == null)
            return 0;
        // glClear(256);
        // glMatrixMode(GL_PROJECTION);
        // glLoadIdentity();
        // IntBuffer buffer = BufferUtils.createIntBuffer(16);
        // glGetInteger(GL_VIEWPORT, buffer);
        // glOrtho(0, buffer.get(2), buffer.get(3), 0, 1000, 3000);
        // glMatrixMode(GL_MODELVIEW);
        // glLoadIdentity();
        // glTranslatef(0, 0, -2000);
        glPushMatrix();
        glScaled(0.5, 0.5, 0.5);

        boolean blend = glIsEnabled(GL_BLEND);
        boolean lighting = glIsEnabled(GL_LIGHTING);
        boolean texture = glIsEnabled(GL_TEXTURE_2D);
        if(!blend)
            glEnable(GL_BLEND);
        if(lighting)
            glDisable(GL_LIGHTING);
        if(texture)
            glDisable(GL_TEXTURE_2D);
        x *= 2;
        y *= 2;
        // glBegin(GL_LINES);
        // glVertex3d(x, y, 0);
        // glVertex3d(x + getStringWidth(string), y + FONT_HEIGHT, 0);
        // glEnd();

        unicodeFont.drawString(x, y, string, new org.newdawn.slick.Color(color));

        if(texture)
            glEnable(GL_TEXTURE_2D);
        if(lighting)
            glEnable(GL_LIGHTING);
        if(!blend)
            glDisable(GL_BLEND);
        glPopMatrix();
        return x;
    }

    @Override
    public int drawStringWithShadow(String string, float x, float y, int color) {
        return drawString(string, (int) x, (int) y, color);
    }

    @Override
    public int getCharWidth(char c) {
        return getStringWidth(Character.toString(c));
    }

    @Override
    public int getStringWidth(String string) {
        return unicodeFont.getWidth(string) / 2;
    }

    public int getStringHeight(String string) {
        return unicodeFont.getHeight(string) / 2;
    }
 
Code:
    @Override
    public int drawString(String string, int x, int y, int color) {
        if (string == null) {
            return 0;
        }
        GL11.glPushMatrix();
        GL11.glScaled(0.5, 0.5, 0.5);

        boolean blend = GL11.glIsEnabled(GL11.GL_BLEND);
        boolean lighting = GL11.glIsEnabled(GL11.GL_LIGHTING);
        boolean texture = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);
        if (!blend) {
            GL11.glEnable(GL11.GL_BLEND);
        }
        if (lighting) {
            GL11.glDisable(GL11.GL_LIGHTING);
        }
        if (texture) {
            GL11.glDisable(GL11.GL_TEXTURE_2D);
        }
        x *= 2;
        y *= 2;

        org.newdawn.slick.Color color1 = new org.newdawn.slick.Color(color);
        float xPos = x;
        for (int i = 0; i < string.length(); i++) {
            if ((string.charAt(i) == '§') && (i + 1 < string.length())) {
                char oneMore = Character.toLowerCase(string.charAt(i + 1));
                int colorCode = Minecraft.getMinecraft().fontRendererObj.getColorCode(oneMore);
                if (colorCode != 16777215) {
                    color1 = new org.newdawn.slick.Color(colorCode);
                } else if (oneMore == 'r') {
                    color1 = new org.newdawn.slick.Color(color);
                }
                i++;
            } else {
                String toRender = new String(new char[] { string.charAt(i) });
                this.font.drawString(xPos, y, toRender, color1);
                xPos += getStringWidth(toRender) * 2 + 1;
            }
        }

        if (texture) {
            GL11.glEnable(GL11.GL_TEXTURE_2D);
        }
        if (lighting) {
            GL11.glEnable(GL11.GL_LIGHTING);
        }
        if (!blend) {
            GL11.glDisable(GL11.GL_BLEND);
        }
        GlStateManager.color(0.0F, 0.0F, 0.0F);
        GL11.glPopMatrix();
        GlStateManager.bindTexture(0);
        return x;
    }
 
I crashes when getStringWidth() is called.
 
SOMEONE JUST GIVE ME A WHOLE CUSTOM FONT RENDERER CLASS, PLEASE, I AM DONE FUCKING AROUND WITH THIS.
 
Code:
package sexy.smooth.utilities;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import sexy.smooth.Smooth;
import sexy.smooth.utilities.LoggerUtil.LoggerType;

public class UnicodeFontRenderer extends FontRenderer {
    private UnicodeFont font;

    public UnicodeFontRenderer(Font awtFont) {
        super(Minecraft.getMinecraft().gameSettings, new ResourceLocation("textures/font/ascii.png"), Minecraft.getMinecraft().getTextureManager(), false);

        if (this.checkFont(awtFont)) {
            Smooth.getInstance().getLoggerUtil().log("Font: initialize '" + awtFont.getName() + "' '" + awtFont.getSize() + "'", LoggerType.LOADING);
        } else {
            Smooth.getInstance().getLoggerUtil().log("Font: You have to install '" + awtFont.getName() + "' font! Client closing...", LoggerType.ERROR);
            Minecraft.getMinecraft().shutdown();
        }

        this.font = new UnicodeFont(awtFont);
        this.font.addAsciiGlyphs();
        this.font.getEffects().add(new ColorEffect(Color.WHITE));
        try {
            this.font.loadGlyphs();
        } catch (SlickException exception) {
            throw new RuntimeException(exception);
        }
        String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        FONT_HEIGHT = this.font.getHeight(alphabet) / 2;
    }

    private boolean checkFont(Font awtFont) {
        GraphicsEnvironment g = null;
        g = GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fonts = g.getAvailableFontFamilyNames();
        for (int i = 0; i < fonts.length; i++) {
            if (fonts[i].equals(awtFont.getName())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int drawString(String string, int x, int y, int color) {
        if (string == null) {
            return 0;
        }
        GL11.glPushMatrix();
        GL11.glScaled(0.5, 0.5, 0.5);

        boolean blend = GL11.glIsEnabled(GL11.GL_BLEND);
        boolean lighting = GL11.glIsEnabled(GL11.GL_LIGHTING);
        boolean texture = GL11.glIsEnabled(GL11.GL_TEXTURE_2D);
        if (!blend) {
            GL11.glEnable(GL11.GL_BLEND);
        }
        if (lighting) {
            GL11.glDisable(GL11.GL_LIGHTING);
        }
        if (texture) {
            GL11.glDisable(GL11.GL_TEXTURE_2D);
        }
        x *= 2;
        y *= 2;

        org.newdawn.slick.Color color1 = new org.newdawn.slick.Color(color);
        float xPos = x;
        for (int i = 0; i < string.length(); i++) {
            if ((string.charAt(i) == '§') && (i + 1 < string.length())) {
                char oneMore = Character.toLowerCase(string.charAt(i + 1));
                int colorCode = Minecraft.getMinecraft().fontRendererObj.getColorCode(oneMore);
                if (colorCode != 16777215) {
                    color1 = new org.newdawn.slick.Color(colorCode);
                } else if (oneMore == 'r') {
                    color1 = new org.newdawn.slick.Color(color);
                }
                i++;
            } else {
                String toRender = new String(new char[] { string.charAt(i) });
                this.font.drawString(xPos, y, toRender, color1);
                xPos += getStringWidth(toRender) * 2 + 1;
            }
        }

        if (texture) {
            GL11.glEnable(GL11.GL_TEXTURE_2D);
        }
        if (lighting) {
            GL11.glEnable(GL11.GL_LIGHTING);
        }
        if (!blend) {
            GL11.glDisable(GL11.GL_BLEND);
        }
        GlStateManager.color(0.0F, 0.0F, 0.0F);
        GL11.glPopMatrix();
        GlStateManager.bindTexture(0);
        return x;
    }

    @Override
    public int drawCenteredString(String text, int x, int y, int color) {
        return drawString(text, x - getStringWidth(text) / 2, y, color);
    }

    @Override
    public int drawTotalCenteredString(String text, int x, int y, int color) {
        return drawString(text, x - getStringWidth(text) / 2, y - FONT_HEIGHT / 2, color);
    }

    @Override
    public int getCharWidth(char c) {
        return getStringWidth(Character.toString(c));
    }

    @Override
    public int getStringWidth(String string) {
        return font.getWidth(string) / 2;
    }

    public int getStringHeight(String string) {
        return font.getHeight(string) / 2;
    }
}
 
So, basically, I have an issue with the color codes with my custom font renderer. I don't know what's wrong.

815f982c83955713232666c019626584.png


This is my font renderer class.It is basically just the Darkstorm font renderer.
Code:
/**
     * This is the actual custom font.
     */
    private UnicodeFont unicodeFont;

    /**
     * This constructor will take in a font as it parameters.
     */
    public PeepFont(Font font) {
        super(Wrapper.getMinecraft().gameSettings, new ResourceLocation("textures/font/ascii.png"),
                Wrapper.getMinecraft().getTextureManager(), false);
        unicodeFont = new UnicodeFont(font);
        unicodeFont.addAsciiGlyphs();
        unicodeFont.getEffects().add(new ColorEffect(Color.white));
        try {
            unicodeFont.loadGlyphs();
        } catch (SlickException ex) {
            ex.printStackTrace();
        }
        String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        FONT_HEIGHT = unicodeFont.getHeight(alphabet) / 2;
    }
   
    public int drawString(String string, int x, int y, int color) {
        if(string == null)
            return 0;
        // glClear(256);
        // glMatrixMode(GL_PROJECTION);
        // glLoadIdentity();
        // IntBuffer buffer = BufferUtils.createIntBuffer(16);
        // glGetInteger(GL_VIEWPORT, buffer);
        // glOrtho(0, buffer.get(2), buffer.get(3), 0, 1000, 3000);
        // glMatrixMode(GL_MODELVIEW);
        // glLoadIdentity();
        // glTranslatef(0, 0, -2000);
        glPushMatrix();
        glScaled(0.5, 0.5, 0.5);

        boolean blend = glIsEnabled(GL_BLEND);
        boolean lighting = glIsEnabled(GL_LIGHTING);
        boolean texture = glIsEnabled(GL_TEXTURE_2D);
        if(!blend)
            glEnable(GL_BLEND);
        if(lighting)
            glDisable(GL_LIGHTING);
        if(texture)
            glDisable(GL_TEXTURE_2D);
        x *= 2;
        y *= 2;
        // glBegin(GL_LINES);
        // glVertex3d(x, y, 0);
        // glVertex3d(x + getStringWidth(string), y + FONT_HEIGHT, 0);
        // glEnd();

        unicodeFont.drawString(x, y, string, new org.newdawn.slick.Color(color));

        if(texture)
            glEnable(GL_TEXTURE_2D);
        if(lighting)
            glEnable(GL_LIGHTING);
        if(!blend)
            glDisable(GL_BLEND);
        glPopMatrix();
        return x;
    }

    @Override
    public int drawStringWithShadow(String string, float x, float y, int color) {
        return drawString(string, (int) x, (int) y, color);
    }

    @Override
    public int getCharWidth(char c) {
        return getStringWidth(Character.toString(c));
    }

    @Override
    public int getStringWidth(String string) {
        return unicodeFont.getWidth(string) / 2;
    }

    public int getStringHeight(String string) {
        return unicodeFont.getHeight(string) / 2;
    }
You need to change the file encoding to ISO-8859-1,if your in IDEA,simply go to settings editor and file encodings and select the class and change its encoding
 
shape1
shape2
shape3
shape4
shape5
shape6
Back
Top