Skip to content

Commit 8d4ae52

Browse files
committed
fix rendering on LWJGL2 and add guard to EventHandler construction
1 parent 473cc23 commit 8d4ae52

7 files changed

Lines changed: 82 additions & 57 deletions

File tree

minecraft/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/GLPlatformImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public long memAddress(ByteBuffer buf) {
7575
//#endif
7676
}
7777

78+
@Override
79+
public void glVertexAttribIPointer(int index, int size, int type, int stride, long pointer) {
80+
//#if MC <= 1.12.2
81+
org.lwjgl.opengl.EXTGpuShader4.glVertexAttribIPointerEXT(index, size, type, stride, pointer);
82+
//#else
83+
//$$ org.lwjgl.opengl.GL30.glVertexAttribIPointer(index, size, type, stride, pointer);
84+
//#endif
85+
}
86+
7887
/**
7988
* This method is called to update the game's internally tracked OpenGL state
8089
* to match what NanoVG leaves dropped into the OpenGL context.

modules/events/src/main/java/org/polyfrost/oneconfig/api/event/v1/invoke/EventHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.polyfrost.oneconfig.api.event.v1.EventManager;
3232
import org.polyfrost.oneconfig.api.event.v1.events.Event;
3333

34+
import java.lang.reflect.Modifier;
3435
import java.util.function.Consumer;
3536
import java.util.function.Predicate;
3637

@@ -54,6 +55,9 @@ public abstract class EventHandler<E extends Event> implements Comparable<EventH
5455
*/
5556
@kotlin.OverloadResolutionByLambdaReturnType
5657
public static <E extends Event> EventHandler<E> ofRemoving(Class<E> cls, Predicate<? super E> handler) {
58+
if (Modifier.isAbstract(cls.getModifiers())) {
59+
throw new IllegalArgumentException("Cannot register to an abstract event type - is there subtypes you need to specify?");
60+
}
5761
return new EventHandler<E>() {
5862
@Override
5963
public boolean handle(E event) {
@@ -77,6 +81,9 @@ public Class<E> getEventClass() {
7781
*/
7882
@kotlin.OverloadResolutionByLambdaReturnType
7983
public static <E extends Event> EventHandler<E> of(Class<E> cls, Consumer<? super E> handler) {
84+
if (Modifier.isAbstract(cls.getModifiers())) {
85+
throw new IllegalArgumentException("Cannot register to an abstract event type - is there subtypes you need to specify?");
86+
}
8087
return new EventHandler<E>() {
8188
@Override
8289
public boolean handle(E event) {
@@ -93,6 +100,9 @@ public Class<E> getEventClass() {
93100

94101
@kotlin.OverloadResolutionByLambdaReturnType
95102
public static <E extends Event> EventHandler<E> of(Class<E> cls, Runnable handler) {
103+
if (Modifier.isAbstract(cls.getModifiers())) {
104+
throw new IllegalArgumentException("Cannot register to an abstract event type - is there subtypes you need to specify?");
105+
}
96106
return new EventHandler<E>() {
97107
@Override
98108
public boolean handle(E event) {

modules/ui/src/main/java/org/polyfrost/oneconfig/api/ui/v1/api/StbApi.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ public interface StbApi {
1212

1313
void image_write_png(String filename, int w, int h, int comp, ByteBuffer data, int strideInBytes);
1414

15-
long font_CreateFontInfo();
15+
StbFontInfo font_CreateFontInfo();
1616

1717
long font_CreatePackRange();
1818

1919
long font_CreatePackedCharArray(int capacity);
2020

2121
long font_CreatePackContext();
2222

23-
boolean font_InitFont(long info, ByteBuffer data);
23+
boolean font_InitFont(StbFontInfo info, ByteBuffer data);
2424

25-
int font_FindGlyphIndex(long info, int codepoint);
25+
int font_FindGlyphIndex(StbFontInfo info, int codepoint);
2626

27-
float font_ScaleForMappingEmToPixels(long info, float pixels);
27+
float font_ScaleForMappingEmToPixels(StbFontInfo info, float pixels);
2828

29-
void font_GetFontVMetrics(long info, int[] ascent, int[] descent, int[] lineGap);
29+
void font_GetFontVMetrics(StbFontInfo info, int[] ascent, int[] descent, int[] lineGap);
3030

31-
void font_GetGlyphHMetrics(long info, int glyphIndex, int[] advanceWidth, int[] leftSideBearing);
31+
void font_GetGlyphHMetrics(StbFontInfo info, int glyphIndex, int[] advanceWidth, int[] leftSideBearing);
3232

33-
long font_GetGlyphBitmap(long info, float scaleX, float scaleY, int glyphIndex, int[] w, int[] h, int[] x_off, int[] y_off);
33+
ByteBuffer font_GetGlyphBitmap(StbFontInfo info, float scaleX, float scaleY, int glyphIndex, int[] w, int[] h, int[] x_off, int[] y_off);
3434

35-
long font_GetGlyphSDF(long info, float scale, int glyphIndex, int padding, byte onEdgeValue, float pixelDistScale, int[] w, int[] h, int[] x_off, int[] y_off);
35+
ByteBuffer font_GetGlyphSDF(StbFontInfo info, float scale, int glyphIndex, int padding, byte onEdgeValue, float pixelDistScale, int[] w, int[] h, int[] x_off, int[] y_off);
3636

3737
boolean font_PackBegin(long packCtx, ByteBuffer pixels, int width, int height, int strideInBytes, int padding, long allocCtx);
3838

@@ -48,8 +48,6 @@ public interface StbApi {
4848

4949
void font_RangeSetChardata(long range, long packedCharArray);
5050

51-
void free(long struct);
52-
5351
long font_GetPackedGlyph(long packedCharArray, int index);
5452

5553
short glyph_x0(long glyph);
@@ -66,4 +64,9 @@ public interface StbApi {
6664

6765
float glyph_yoff(long glyph);
6866

67+
interface StbFontInfo {
68+
long address();
69+
void free();
70+
}
71+
6972
}

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.polyfrost.oneconfig.api.ui.v1.internal
33
import org.lwjgl.opengl.GL11.*
44
import org.lwjgl.opengl.GL12.*
55
import org.lwjgl.opengl.GL13.*
6-
import org.polyfrost.oneconfig.api.platform.v1.Platform
76
import org.polyfrost.polyui.unit.Vec4
87
import java.nio.ByteBuffer
98

@@ -55,20 +54,6 @@ class GLAtlasManager(val atlasWidth: Int, val atlasHeight: Int) {
5554
height: Int,
5655
pixels: ByteBuffer,
5756
format: Int = GL_RGBA
58-
): Vec4 = insert(width, height, Platform.gl().memAddress(pixels), format)
59-
60-
/**
61-
* Insert a given texture, a ByteBuffer of [GL_UNSIGNED_BYTE] pixels, of dimensions [width] and [height] into this atlas.
62-
*
63-
* Specify the format of your data with the [format] parameter.
64-
*
65-
* @return A normalised UVWH Vec4 object referring to the dimensions and position of the texture inserted into the atlas.
66-
*/
67-
fun insert(
68-
width: Int,
69-
height: Int,
70-
pixels: Long,
71-
format: Int = GL_RGBA
7257
): Vec4 {
7358
val pos = findPosition(width, height)
7459
?: throw IllegalStateException("Texture atlas full")
@@ -83,7 +68,7 @@ class GLAtlasManager(val atlasWidth: Int, val atlasHeight: Int) {
8368
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)
8469
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0)
8570
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0)
86-
nglTexSubImage2D(GL_TEXTURE_2D, 0,
71+
glTexSubImage2D(GL_TEXTURE_2D, 0,
8772
pos.x, pos.y, width, height,
8873
format, GL_UNSIGNED_BYTE,
8974
pixels

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,11 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
454454
glUniform2f(uWindow, width, height)
455455
glUniformMatrix3fv(uTransform, false, transform)
456456
glUseProgram(prevProg)
457-
glDisable(GL_SCISSOR_TEST)
458457
this.pixelRatio = pixelRatio
459458
}
460459

461460
override fun endFrame() {
462461
flush()
463-
glDisable(GL_SCISSOR_TEST)
464462
}
465463

466464
private fun flush() {
@@ -477,7 +475,9 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
477475
val prevBlendDstAlpha = glGetInteger(GL_BLEND_DST_ALPHA)
478476
val prevDepth = glGetBoolean(GL_DEPTH_TEST)
479477
val prevCull = glGetBoolean(GL_CULL_FACE)
478+
val prevScissor = glGetBoolean(GL_SCISSOR_TEST)
480479
glEnable(GL_BLEND)
480+
glDisable(GL_SCISSOR_TEST)
481481
glDisable(GL_CULL_FACE)
482482
glDisable(GL_DEPTH_TEST)
483483
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
@@ -532,6 +532,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
532532
glBlendFuncSeparate(prevBlendSrcRgb, prevBlendDstRgb, prevBlendSrcAlpha, prevBlendDstAlpha)
533533
if (prevDepth) glEnable(GL_DEPTH_TEST)
534534
if (prevCull) glEnable(GL_CULL_FACE)
535+
if (prevScissor) glEnable(GL_SCISSOR_TEST)
535536
glUseProgram(prevProg)
536537
glActiveTexture(prevActive)
537538
glBindTexture(GL_TEXTURE_2D, prevTex)
@@ -550,8 +551,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
550551

551552
private fun enableAttribui(loc: Int, size: Int, offset: Long): Long {
552553
glEnableVertexAttribArray(loc)
553-
if (GlCapabilities.isGl3Available) org.lwjgl.opengl.GL30.glVertexAttribIPointer(loc, size, GL_UNSIGNED_INT, STRIDE * 4, offset)
554-
else org.lwjgl.opengl.EXTGPUShader4.glVertexAttribIPointerEXT(loc, size, GL_UNSIGNED_INT, STRIDE * 4, offset)
554+
Platform.gl().glVertexAttribIPointer(loc, size, GL_UNSIGNED_INT, STRIDE * 4, offset)
555555
if (GlCapabilities.isGl33Available) org.lwjgl.opengl.GL33.glVertexAttribDivisor(loc, 1)
556556
else org.lwjgl.opengl.ARBInstancedArrays.glVertexAttribDivisorARB(loc, 1)
557557
return offset + size * 4L
@@ -659,12 +659,14 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
659659
private fun FloatArray.getScaledMat4(): OmniMatrix4f {
660660
// asm: scale to MC instance coordinates and mutate to a 4x4 matrix
661661
val sf = pixelRatio / OmniResolution.scaleFactor.toFloat()
662-
return OmniMatrix4f.from(floatArrayOf(
663-
this[0] * sf, this[1] * sf, 0f, 0f,
664-
this[3] * sf, this[4] * sf, 0f, 0f,
665-
0f, 0f, 1f, 0f,
666-
this[6] * sf, this[7] * sf, 0f, 1f
667-
))
662+
return OmniMatrix4f.from(
663+
floatArrayOf(
664+
this[0] * sf, this[1] * sf, 0f, 0f,
665+
this[3] * sf, this[4] * sf, 0f, 0f,
666+
0f, 0f, 1f, 0f,
667+
this[6] * sf, this[7] * sf, 0f, 1f
668+
)
669+
)
668670
}
669671

670672
override fun text(font: Font, x: Float, y: Float, text: String, color: Color, fontSize: Float) {
@@ -692,6 +694,10 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
692694
val glyph = fAtlas.get(it)
693695
// opt: early exit when we are out of the scissored region
694696
if (scissorDepth > 3 && penX > scissorStack[scissorDepth - 2]) return
697+
if (it == 32) { // space
698+
penX += glyph.xAdvance * scaleFactor
699+
return@forEachCodepoint
700+
}
695701
buffer.put(penX + glyph.xOff * scaleFactor).put(penY + glyph.yOff * scaleFactor)
696702
.put(glyph.width * scaleFactor).put(glyph.height * scaleFactor)
697703
buffer.put(0f).put(-1f).put(0f).put(0f) // zero radii (-1 optimization)
@@ -984,9 +990,9 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
984990
// }
985991

986992
stb.font_GetGlyphHMetrics(fontInfo, idx, xAdvance, null)
987-
var sdf = stb.font_GetGlyphSDF(fontInfo, scale, idx, 4, 128.toByte(), 64f, w, h, xoff, yoff)
988-
if (sdf == 0L) sdf = stb.font_GetGlyphBitmap(fontInfo, scale, scale, idx, w, h, xoff, yoff)
989-
if (sdf == 0L) sdf = Platform.gl().memAddress(BufferUtils.createByteBuffer(w[0] * h[0] * 4))
993+
val sdf = stb.font_GetGlyphSDF(fontInfo, scale, idx, 4, 128.toByte(), 64f, w, h, xoff, yoff)
994+
?: stb.font_GetGlyphBitmap(fontInfo, scale, scale, idx, w, h, xoff, yoff)
995+
?: BufferUtils.createByteBuffer(w[0] * h[0] * 4)
990996

991997
val (u, v, uw, uh) = atlas.insert(w[0], h[0], sdf, GL_RED)
992998
return floatArrayOf(
@@ -1014,7 +1020,7 @@ class GLRendererImpl(private val nsvg: NanoSvgApi, private val stb: StbApi) : Re
10141020
inline fun get(codepoint: Int) = glyphs.getOrPut(codepoint) { makeGlyph(codepoint) }
10151021

10161022
fun cleanup() {
1017-
stb.free(fontInfo)
1023+
fontInfo.free()
10181024
glyphs.clear()
10191025
}
10201026
}

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,40 @@ class StbImpl : StbApi {
2828
STBImageWrite.stbi_write_png(filename, w, h, comp, data, strideInBytes)
2929
}
3030

31-
override fun font_CreateFontInfo() = STBTTFontinfo.malloc().address()
31+
override fun font_CreateFontInfo(): StbApi.StbFontInfo = FontInfo(STBTTFontinfo.malloc())
3232

3333
override fun font_CreatePackRange() = STBTTPackRange.malloc().address()
3434

3535
override fun font_CreatePackedCharArray(capacity: Int) = STBTTPackedchar.malloc(capacity).address()
3636

3737
override fun font_CreatePackContext() = STBTTPackContext.malloc().address()
3838

39-
override fun font_InitFont(info: Long, data: ByteBuffer) = nstbtt_InitFont(info, Platform.gl().memAddress(data), 0) != 0
39+
override fun font_InitFont(info: StbApi.StbFontInfo, data: ByteBuffer) = nstbtt_InitFont(info.address(), Platform.gl().memAddress(data), 0) != 0
4040

41-
override fun font_FindGlyphIndex(info: Long, codepoint: Int) = nstbtt_FindGlyphIndex(info, codepoint)
41+
override fun font_FindGlyphIndex(info: StbApi.StbFontInfo, codepoint: Int) = nstbtt_FindGlyphIndex(info.address(), codepoint)
4242

43-
override fun font_ScaleForMappingEmToPixels(info: Long, pixels: Float) =
44-
nstbtt_ScaleForMappingEmToPixels(info, pixels)
43+
override fun font_ScaleForMappingEmToPixels(info: StbApi.StbFontInfo, pixels: Float) =
44+
nstbtt_ScaleForMappingEmToPixels(info.address(), pixels)
4545

4646
override fun font_GetFontVMetrics(
47-
info: Long,
47+
info: StbApi.StbFontInfo,
4848
ascent: IntArray,
4949
descent: IntArray,
5050
lineGap: IntArray
5151
) {
52-
nstbtt_GetFontVMetrics(info, ascent, descent, lineGap)
52+
nstbtt_GetFontVMetrics(info.address(), ascent, descent, lineGap)
5353
}
5454

55-
override fun font_GetGlyphHMetrics(info: Long, glyphIndex: Int, advanceWidth: IntArray?, leftSideBearing: IntArray?) {
56-
nstbtt_GetGlyphHMetrics(info, glyphIndex, advanceWidth, leftSideBearing)
55+
override fun font_GetGlyphHMetrics(info: StbApi.StbFontInfo, glyphIndex: Int, advanceWidth: IntArray?, leftSideBearing: IntArray?) {
56+
nstbtt_GetGlyphHMetrics(info.address(), glyphIndex, advanceWidth, leftSideBearing)
5757
}
5858

59-
override fun font_GetGlyphBitmap(info: Long, scaleX: Float, scaleY: Float, glyphIndex: Int, w: IntArray?, h: IntArray?, x_off: IntArray?, y_off: IntArray?): Long {
60-
return nstbtt_GetGlyphBitmap(info, scaleX, scaleY, glyphIndex, w, h, x_off, y_off)
59+
override fun font_GetGlyphBitmap(info: StbApi.StbFontInfo, scaleX: Float, scaleY: Float, glyphIndex: Int, w: IntArray?, h: IntArray?, x_off: IntArray?, y_off: IntArray?): ByteBuffer? {
60+
return stbtt_GetGlyphBitmap((info as FontInfo).struct, scaleX, scaleY, glyphIndex, w, h, x_off, y_off)
6161
}
6262

63-
override fun font_GetGlyphSDF(info: Long, scale: Float, glyphIndex: Int, padding: Int, onEdgeValue: Byte, pixelDistScale: Float, w: IntArray?, h: IntArray?, x_off: IntArray?, y_off: IntArray?): Long {
64-
return nstbtt_GetGlyphSDF(info, scale, glyphIndex, padding, onEdgeValue, pixelDistScale, w, h, x_off, y_off)
63+
override fun font_GetGlyphSDF(info: StbApi.StbFontInfo, scale: Float, glyphIndex: Int, padding: Int, onEdgeValue: Byte, pixelDistScale: Float, w: IntArray?, h: IntArray?, x_off: IntArray?, y_off: IntArray?): ByteBuffer? {
64+
return stbtt_GetGlyphSDF((info as FontInfo).struct, scale, glyphIndex, padding, onEdgeValue, pixelDistScale, w, h, x_off, y_off)
6565
}
6666

6767
override fun font_PackBegin(
@@ -123,10 +123,6 @@ class StbImpl : StbApi {
123123
MemoryUtil.memPutAddress(range + STBTTPackRange.CHARDATA_FOR_RANGE, packedCharArray)
124124
}
125125

126-
override fun free(struct: Long) {
127-
MemoryUtil.nmemFree(struct)
128-
}
129-
130126
override fun font_GetPackedGlyph(packedCharArray: Long, index: Int) = packedCharArray + index * STBTTPackedchar.SIZEOF
131127

132128
override fun glyph_x0(glyph: Long) = STBTTPackedchar.nx0(glyph)
@@ -142,4 +138,9 @@ class StbImpl : StbApi {
142138
override fun glyph_xoff(glyph: Long) = STBTTPackedchar.nxoff(glyph)
143139

144140
override fun glyph_yoff(glyph: Long) = STBTTPackedchar.nyoff(glyph)
141+
142+
private class FontInfo(val struct: STBTTFontinfo) : StbApi.StbFontInfo {
143+
override fun address() = struct.address()
144+
override fun free() = struct.free()
145+
}
145146
}

modules/utils/src/main/java/org/polyfrost/oneconfig/api/platform/v1/GLPlatform.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public interface GLPlatform {
4747
*/
4848
long memAddress(ByteBuffer buf);
4949

50+
/**
51+
* This method will try and run glVertexAttribIPointer on the current OpenGL context.
52+
* <br> If the context is OpenGL3+, it will run the core API method.
53+
* <br> If not, it will run the method using the EXT_gpu_shader4 extension.
54+
* <br> If this is missing, this will blow up.
55+
*
56+
* @implNote This method exists solely to fix the issue that in LWJGL2, the extension has a different class name
57+
* to that in LWJGL3. thanks, guys.
58+
*/
59+
void glVertexAttribIPointer(int index, int size, int type, int stride, long pointer);
60+
5061
void syncOpenGLContext();
5162

5263
/**

0 commit comments

Comments
 (0)