Skip to content

Commit d51c10a

Browse files
committed
Add NVG image flags (closes #582)
1 parent 976dc4f commit d51c10a

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

modules/hud/src/main/kotlin/org/polyfrost/oneconfig/api/hud/v1/HudManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ object HudManager {
209209
require(hud.isReal) { "Tried to remove a non-real HUD - use unregister() for this case. You might also be calling this method too early!" }
210210
polyUI.master.removeChild(hud.getBackground() ?: hud.get(), recalculate = false)
211211
polyUI.removeExecutor(hud.tree.getMetadata("updateTicker"))
212-
212+
213213
try {
214214
hud.remove() // allow HUD to clean up resources
215215
} catch (_: Throwable) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.polyfrost.oneconfig.api.ui.v1.image
2+
3+
import org.polyfrost.polyui.data.image.FlagKey
4+
5+
object NVGImageFlags {
6+
const val NONE = 0
7+
const val REPEAT_X = 1 shl 1
8+
const val REPEAT_Y = 1 shl 2
9+
const val FLIP_Y = 1 shl 3
10+
const val PREMULTIPLIED = 1 shl 4
11+
const val NEAREST = 1 shl 5
12+
13+
@JvmField val KEY = FlagKey<Int>("nvg:sampling")
14+
}

modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/internal/NVGRendererImpl.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ import dev.deftu.omnicore.api.client.render.OmniTextureUnit
3030
import dev.deftu.omnicore.api.paddedMinecraftVersion
3131
import dev.deftu.omnicore.internal.client.textures.TextureInternals
3232
import org.apache.logging.log4j.LogManager
33+
import org.lwjgl.nanovg.NanoVG
3334
import org.lwjgl.opengl.GL11
3435
import org.lwjgl.opengl.GL20
3536
import org.lwjgl.opengl.GL30
3637
import org.polyfrost.oneconfig.api.platform.v1.Platform
3738
import org.polyfrost.oneconfig.api.ui.v1.api.NanoSvgApi
3839
import org.polyfrost.oneconfig.api.ui.v1.api.NanoVgApi
3940
import org.polyfrost.oneconfig.api.ui.v1.api.StbApi
41+
import org.polyfrost.oneconfig.api.ui.v1.image.NVGImageFlags
4042
import org.polyfrost.polyui.PolyUI
4143
import org.polyfrost.polyui.color.PolyColor
4244
import org.polyfrost.polyui.data.Font
@@ -437,8 +439,10 @@ class NVGRendererImpl(
437439
image.loadAsync(errorHandler) {
438440
queue.add { loadSvg(image, it.toDirectByteBufferNT()) }
439441
}
442+
440443
return defaultImage
441444
}
445+
442446
map.getOrPut(width.hashCode() * 31 + height.hashCode()) { resizeSvg(svg, width, height) }
443447
}
444448

@@ -447,6 +451,7 @@ class NVGRendererImpl(
447451
image.loadAsync(errorHandler) {
448452
queue.add { images[image] = loadImage(image, it.toDirectByteBuffer()) }
449453
}
454+
450455
defaultImage
451456
}
452457
}
@@ -478,7 +483,7 @@ class NVGRendererImpl(
478483
val h = IntArray(1)
479484
val d = stb.image_load_from_memory(data, w, h, IntArray(1), 4) ?: throw IllegalStateException("Failed to load image ${image.resourcePath}: ${stb.image_failure_reason()}")
480485
if (!image.size.isPositive) PolyImage.setImageSize(image, Vec2(w[0].toFloat(), h[0].toFloat()))
481-
return vg.createImage(w[0].toFloat(), h[0].toFloat(), d, 0)
486+
return vg.createImage(w[0].toFloat(), h[0].toFloat(), d, image[NVGImageFlags.KEY] ?: NVGImageFlags.NONE)
482487
}
483488

484489
private fun loadSvg(image: PolyImage, data: ByteBuffer): Int {
@@ -487,6 +492,7 @@ class NVGRendererImpl(
487492
if (!image.size.isPositive) PolyImage.setImageSize(image, Vec2(svg.width, svg.height).also {
488493
if(!it.isPositive) throw IllegalArgumentException("SVG ${image.resourcePath} has invalid size ($it), maybe it is missing/corrupted?")
489494
})
495+
490496
val o = resizeSvg(svg, svg.width, svg.height)
491497
map[image.size.hashCode()] = o
492498
svgs[image] = svg to map

0 commit comments

Comments
 (0)