Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

[fix] Force initialise ImGui in the MinecraftClient constructor at return #535

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import me.zeroeightsix.kami.KamiMod;
import me.zeroeightsix.kami.event.ScreenEvent;
import me.zeroeightsix.kami.event.TickEvent;
import me.zeroeightsix.kami.gui.KamiImgui;
import me.zeroeightsix.kami.setting.KamiConfig;
import me.zeroeightsix.kami.util.Wrapper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.profiler.Profiler;
import org.apache.logging.log4j.LogManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -31,6 +33,12 @@ public class MixinMinecraftClient {
@Shadow
private Profiler profiler;

@Inject(method = "<init>", at = @At(value = "RETURN"))
public void init(CallbackInfo info) {
KamiImgui.INSTANCE.init();
LogManager.getLogger("KAMI").info("ImGui initialised.");
}

@Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/profiler/Profiler;pop()V", ordinal = 0, shift = At.Shift.AFTER), cancellable = true)
public void tick(CallbackInfo info) {
TickEvent event;
Expand Down
66 changes: 30 additions & 36 deletions src/main/kotlin/me/zeroeightsix/kami/gui/KamiImgui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ object KamiImgui {

private const val minecraftiaLocation = "/assets/kami/Minecraftia.ttf"

init {
fun addKamiFontFromTTF(filename: String, sizePixels: Float, fontCfg: ImFontConfig): ImFont? {
val bytes = ByteStreams.toByteArray(javaClass.getResourceAsStream(filename) ?: return null)
fun init() {
fun loadFontFromResources(filename: String): ByteArray? {
return ByteStreams.toByteArray(javaClass.getResourceAsStream(filename) ?: return null)
}
fun addKamiFontFromTTF(bytes: ByteArray, sizePixels: Float, fontCfg: ImFontConfig): ImFont? {
return ImGui.getIO().fonts.addFontFromMemoryTTF(bytes, sizePixels, fontCfg)
}

Expand All @@ -55,28 +57,31 @@ object KamiImgui {
return fontCfg
}

addKamiFontFromTTF(
minecraftiaLocation,
12f,
fontCfg {
oversampleH = 1
oversampleV = 1
pixelSnapH = true
loadFontFromResources(minecraftiaLocation)?.let { bytes ->
addKamiFontFromTTF(
bytes,
12f,
fontCfg {
oversampleH = 1
oversampleV = 1
pixelSnapH = true
}
)?.let {
fonts.put("Minecraftia 12px", it)
}
)?.let {
fonts.put("Minecraftia 12px", it)
}
addKamiFontFromTTF(
minecraftiaLocation,
24f,
fontCfg {
oversampleH = 1
oversampleV = 1
pixelSnapH = true
addKamiFontFromTTF(
bytes,
24f,
fontCfg {
oversampleH = 1
oversampleV = 1
pixelSnapH = true
}
)?.let {
fonts.put("Minecraftia 24px", it)
}
)?.let {
fonts.put("Minecraftia 24px", it)
}

ImGui.getIO().fonts.addFontDefault()?.let {
fonts.put("Default", it)
}
Expand All @@ -87,22 +92,11 @@ object KamiImgui {
val defaultFontName = fontNames.getOrElse(Settings.font) { fontNames.first() }
ImGui.getIO().setFontDefault(fonts[defaultFontName])

val caps = GL.getCapabilities()
// TODO: check if this works on macOS properly.
val glslVersion = when {
caps.OpenGL32 -> {
150
}
caps.OpenGL30 -> { // apparently we might have to skip this one?
130
}
else -> 110
}

ImGui.getIO().addConfigFlags(ImGuiConfigFlags.DockingEnable)

imguiGlfw.init(mc.window.handle, false)
imguiGl.init("#version $glslVersion")
// Force 110 shaders since this is what base Minecraft uses to avoid bugs in Intel drivers.
imguiGl.init("#version 110")
}

internal fun frame(matrices: MatrixStack, block: () -> Unit) {
Expand Down Expand Up @@ -132,4 +126,4 @@ object KamiImgui {
fun mouseScroll(d: Double, e: Double) {
imguiGlfw.scrollCallback(mc.window.handle, d, e)
}
}
}