aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java
blob: c9d0615ebfa13a007821fa1a3a526b157c1b2ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package io.polyfrost.oneconfig.lwjgl;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.shader.Framebuffer;
import org.lwjgl.nanovg.NVGColor;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryStack;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.function.LongConsumer;

import static org.lwjgl.nanovg.NanoVG.*;
import static org.lwjgl.nanovg.NanoVGGL2.*;

public final class NanoVGUtils {
    private NanoVGUtils() {

    }
    private static long vg = -1;
    private static int font = -1;

    public static void setupAndDraw(LongConsumer consumer) {
        if (vg == -1) {
            vg = nvgCreate(NVG_ANTIALIAS);
            if (vg == -1) {
                throw new RuntimeException("Failed to create nvg context");
            }
        }
        if (font == -1) {
            try {
                font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Roboto-Regular.ttf", 150 * 1024), 0);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (font == -1) {
                throw new RuntimeException("Failed to create custom font");
            }
        }

        Framebuffer fb = Minecraft.getMinecraft().getFramebuffer();
        if (!fb.isStencilEnabled()) {
            fb.enableStencil();
        }
        GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);

        nvgBeginFrame(vg, Display.getWidth(), Display.getHeight(), 1);

        consumer.accept(vg);

        nvgEndFrame(vg);

        GlStateManager.popAttrib();
    }

    public static void drawRect(long vg, float x, float y, float width, float height, int color) {
        nvgBeginPath(vg);
        nvgRect(vg, x, y, width, height);
        color(vg, color);
        nvgFill(vg);
    }

    public static void drawRoundedRect(long vg, float x, float y, float width, float height, int color, float radius) {
        nvgBeginPath(vg);
        nvgRoundedRect(vg, x, y, width, height, radius);
        color(vg, color);
        nvgFill(vg);
    }

    public static void drawCircle(long vg, float x, float y, float radius, int color) {
        nvgBeginPath(vg);
        nvgCircle(vg, x, y, radius);
        color(vg, color);
        nvgFill(vg);
    }

    public static void drawString(long vg, String text, float x, float y, int color, float size) {
        nvgBeginPath(vg);
        nvgFontSize(vg, size);
        nvgFontFace(vg, "custom-font");
        nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
        try (MemoryStack stack = MemoryStack.stackPush()) {
            ByteBuffer textByte = stack.ASCII(text, false);
            nvgFontBlur(vg, 0);
            color(vg, color);
            nvgText(vg, x, y, textByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void color(long vg, int color) {
        NVGColor nvgColor = NVGColor.create();
        nvgRGBA((byte) (color >> 16 & 0xFF), (byte) (color >> 8 & 0xFF), (byte) (color & 0xFF), (byte) (color >> 24 & 0xFF), nvgColor);
        nvgFillColor(vg, nvgColor);
    }
}