From 30e63d79a2e58623c021761beabd527591cceb02 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 20 Jul 2024 18:03:35 +0200 Subject: [PATCH] Finishing more work on textures/fonts --- .../speiger/src/coreengine/NewInputTest.java | 14 +++++ .../textures/base/TextureMetadata.java | 1 + .../rendering/textures/custom/Drawable.java | 51 +++++++++++++++---- .../coreengine/utils/eventbus/EventBus.java | 1 + 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/main/java/speiger/src/coreengine/NewInputTest.java b/src/main/java/speiger/src/coreengine/NewInputTest.java index 6bf8100..b541247 100644 --- a/src/main/java/speiger/src/coreengine/NewInputTest.java +++ b/src/main/java/speiger/src/coreengine/NewInputTest.java @@ -14,6 +14,7 @@ import speiger.src.coreengine.rendering.input.devices.FileDrop; import speiger.src.coreengine.rendering.input.devices.Joystick; import speiger.src.coreengine.rendering.input.devices.Keyboard; import speiger.src.coreengine.rendering.input.devices.Mouse; +import speiger.src.coreengine.rendering.input.events.KeyEvent; import speiger.src.coreengine.rendering.input.window.Window; import speiger.src.coreengine.rendering.input.window.WindowManager; import speiger.src.coreengine.rendering.models.buffers.BufferAttribute; @@ -23,10 +24,12 @@ import speiger.src.coreengine.rendering.shader.SimpleShader; import speiger.src.coreengine.rendering.shader.uniform.base.TextureUniform; import speiger.src.coreengine.rendering.tesselation.buffer.VertexBuilder; import speiger.src.coreengine.rendering.tesselation.format.VertexTypes; +import speiger.src.coreengine.rendering.textures.custom.Drawable; import speiger.src.coreengine.rendering.textures.custom.DynamicTexture; import speiger.src.coreengine.rendering.utils.GLStateTracker; import speiger.src.coreengine.rendering.utils.values.GLDataType; import speiger.src.coreengine.rendering.utils.values.GLMode; +import speiger.src.coreengine.rendering.utils.values.textures.GLTextureFormat; import speiger.src.coreengine.utils.eventbus.EventBus; import speiger.src.coreengine.utils.helpers.IOUtils; @@ -67,6 +70,17 @@ public class NewInputTest { texture.process(true); window.visible(true); + bus.register(KeyEvent.Key.class, T -> { + if(T.key() == GLFW.GLFW_KEY_T) { + T.cancel(); + texture.bind(); + Drawable drawable = new Drawable(GLTextureFormat.RGBA, 8, 8); + drawable.fill(0, 0, 8, 8, 255, 255, 0, 255); + drawable.upload(6, 6, 2, 2, 4, 4); + drawable.close(); + } + }); + VertexBuilder builder = new VertexBuilder(255); builder.start(GLMode.TRIANGLES, VertexTypes.TESTING); builder.pos(-0.5F, -0.5F, 0).tex(0F, 1F).rgba(-1).endVertex(); diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/base/TextureMetadata.java b/src/main/java/speiger/src/coreengine/rendering/textures/base/TextureMetadata.java index faf4797..3af0981 100644 --- a/src/main/java/speiger/src/coreengine/rendering/textures/base/TextureMetadata.java +++ b/src/main/java/speiger/src/coreengine/rendering/textures/base/TextureMetadata.java @@ -99,6 +99,7 @@ public class TextureMetadata { for(int i = 0,m=list.size();i= width || y >= height) throw new ArrayIndexOutOfBoundsException("Index out of bounds: X=["+x+"], Y=["+y+"], width=["+width+"], height=["+height+"]"); + if(x > width || y > height) throw new ArrayIndexOutOfBoundsException("Index out of bounds: X=["+x+"], Y=["+y+"], width=["+width+"], height=["+height+"]"); if(pixels == 0L) throw new IllegalStateException("Pixel Data doesn't exist"); } - public void upload(int texture, int level, int x, int y, int texX, int texY, int width, int height) { - + public void drawFont(STBTTFontinfo info, int glyth, int sourceX, int sourceY, int targetX, int targetY, int width, int height, float scaleX, float scaleY) { + ensureValid(targetX, targetY); + ensureValid(targetX + width, targetY + height); + if(components != 1) throw new IllegalStateException("Format has to be 1 component"); + STBTruetype.nstbtt_MakeGlyphBitmapSubpixel(info.address(), pixels + offset(targetX, targetY), width, height, width(), scaleX, scaleY, sourceX, sourceY, glyth); + } + + public void upload(int targetX, int targetY, int sourceX, int sourceY, int width, int height) { + upload(GLTextureType.TEXTURE_2D, 0, targetX, targetY, sourceX, sourceY, width, height); + } + + public void upload(IGLValue textureType, int level, int targetX, int targetY, int sourceX, int sourceY, int width, int height) { + ensureValid(sourceX, sourceY); + ensureValid(sourceX + width, sourceY + height); + GLStateTracker tracker = GLStateTracker.instance(); + tracker.unpack_row_length.set(width()); + tracker.unpack_alignment.set(format.components()); + tracker.unpack_skip_pixel.set(sourceX); + tracker.unpack_skip_rows.set(sourceY); + GLFunctions.upload2DSubImage(textureType, level, targetX, targetY, width, height, format, GLDataType.UNSIGNED_BYTE, pixels); + tracker.unpack_row_length.setDefault(); + tracker.unpack_skip_pixel.setDefault(); + tracker.unpack_skip_rows.setDefault(); + tracker.unpack_alignment.setDefault(); } @Override @@ -103,8 +132,10 @@ public class Drawable implements IDrawable, AutoCloseable { if(components != 4) throw new IllegalStateException("Format has to be 4 components"); ensureValid(x, y); ensureValid(x+width, y+height); - for(int yOff = 0;yOff event) { + //TODO this is abusing undefined behavior! return listeners.computeIfAbsent(event, T -> { if(T == Event.class) return new Listeners(); else return new Listeners(getListeners(castClass(event.getSuperclass())));