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

Commit

Permalink
[enhancement] only load Minecraftia resource once
Browse files Browse the repository at this point in the history
  • Loading branch information
nopjmp authored and zeroeightysix committed Mar 4, 2021
1 parent 47891de commit f5ac8ec
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/main/kotlin/me/zeroeightsix/kami/gui/KamiImgui.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ object KamiImgui {
private const val minecraftiaLocation = "/assets/kami/Minecraftia.ttf"

fun init() {
fun addKamiFontFromTTF(filename: String, sizePixels: Float, fontCfg: ImFontConfig): ImFont? {
val bytes = ByteStreams.toByteArray(javaClass.getResourceAsStream(filename) ?: return null)
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 Down Expand Up @@ -132,4 +137,4 @@ object KamiImgui {
fun mouseScroll(d: Double, e: Double) {
imguiGlfw.scrollCallback(mc.window.handle, d, e)
}
}
}

0 comments on commit f5ac8ec

Please sign in to comment.