Skip to content

Usage in Minecraft Mods

Nick Johnson edited this page Jun 16, 2019 · 2 revisions

Using in a Minecraft mod

Minecraft uses OpenGL, meaning that we will need the imgui-core, imgui-glfw, and imgui-gl modules. In addition, Minecraft comes with LWJGL already, so it needs to be excluded from the gradle build. Our build.gradle will need these lines:

repositories {
    maven {
        url = "https://jitpack.io"
    }
}

dependencies {
    ext{
        imguiVersion = "-SNAPSHOT"
    }
    ["gl", "glfw", "core"].each {
        implementation("com.github.kotlin-graphics.imgui:imgui-$it:$imguiVersion") {
            exclude group: "org.lwjgl"
        }
    }
}

The code we will need to render to the screen requires the Minecraft Window handle, among other things.

import glm_.vec2.Vec2;
import imgui.ImGui;
import imgui.imgui.Context;
import imgui.impl.ImplGL3;
import imgui.impl.ImplGlfw;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Screen;
import net.minecraft.network.chat.Component;
import uno.glfw.GlfwWindow;

public class MyMinecraftScreen extends Screen {

    private static ImGui imgui = ImGui.INSTANCE;

    private static ImplGL3 implGl3;
    private static ImplGlfw implGlfw;

    static {
        ImguiKt.MINECRAFT_BEHAVIORS = true;
        GlfwWindow window = GlfwWindow.from(MinecraftClient.getInstance().window.getHandle());
        window.makeContextCurrent();
        new Context();
        implGlfw = new ImplGlfw(window, false, null);
        implGl3 = new ImplGL3();
    }

    public MyMinecraftScreen (Component title) {
        super(title);
    }

    @Override
    public void render(int x, int y, float partialTicks) {

        implGl3.newFrame();
        implGlfw.newFrame();

        imgui.newFrame();

        //Render things here

        imgui.text("Hello Minecraft!");

        //and stop here

        implGl3.renderDrawData(imgui.getDrawData());

    }
}
Clone this wiki locally