aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/thatgravyboat/skyblockhud/core/GlScissorStack.java
blob: 0e1694e8a55465487eec70052070b4f743af9820 (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
package com.thatgravyboat.skyblockhud.core;

import java.util.LinkedList;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import org.lwjgl.opengl.GL11;

public class GlScissorStack {

    private static class Bounds {

        int left;
        int top;
        int right;
        int bottom;

        public Bounds(int left, int top, int right, int bottom) {
            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;
        }

        public Bounds createSubBound(int left, int top, int right, int bottom) {
            left = Math.max(left, this.left);
            top = Math.max(top, this.top);
            right = Math.min(right, this.right);
            bottom = Math.min(bottom, this.bottom);

            if (top > bottom) {
                top = bottom;
            }
            if (left > right) {
                left = right;
            }

            return new Bounds(left, top, right, bottom);
        }

        public void set(ScaledResolution scaledResolution) {
            int height = Minecraft.getMinecraft().displayHeight;
            int scale = scaledResolution.getScaleFactor();
            GL11.glScissor(left * scale, height - bottom * scale, (right - left) * scale, (bottom - top) * scale);
        }
    }

    private static final LinkedList<Bounds> boundsStack = new LinkedList<>();

    public static void push(int left, int top, int right, int bottom, ScaledResolution scaledResolution) {
        if (right < left) {
            int temp = right;
            right = left;
            left = temp;
        }
        if (bottom < top) {
            int temp = bottom;
            bottom = top;
            top = temp;
        }
        if (boundsStack.isEmpty()) {
            boundsStack.push(new Bounds(left, top, right, bottom));
        } else {
            boundsStack.push(boundsStack.peek().createSubBound(left, top, right, bottom));
        }
        if (!boundsStack.isEmpty()) {
            boundsStack.peek().set(scaledResolution);
        }
        GL11.glEnable(GL11.GL_SCISSOR_TEST);
    }

    public static void pop(ScaledResolution scaledResolution) {
        if (!boundsStack.isEmpty()) {
            boundsStack.pop();
        }
        if (boundsStack.isEmpty()) {
            GL11.glDisable(GL11.GL_SCISSOR_TEST);
        } else {
            boundsStack.peek().set(scaledResolution);
        }
    }

    public static void clear() {
        boundsStack.clear();
        GL11.glDisable(GL11.GL_SCISSOR_TEST);
    }
}