From 55957d3640c82ed40230cd3b45390705579eafbd Mon Sep 17 00:00:00 2001 From: Speiger Date: Sun, 16 Jun 2024 20:51:33 +0200 Subject: [PATCH] more work on fonts and a dynamic texture --- .../speiger/src/coreengine/NewInputTest.java | 30 ++++- .../gui/font/providers/IFontProvider.java | 4 +- .../rendering/textures/custom/Drawable.java | 124 ++++++++++++++++++ .../textures/custom/DynamicTexture.java | 12 +- .../rendering/textures/custom/IDrawable.java | 43 ++++++ .../textures/custom/IDynamicTexture.java | 39 +----- test.png | Bin 0 -> 70 bytes 7 files changed, 211 insertions(+), 41 deletions(-) create mode 100644 src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java create mode 100644 src/main/java/speiger/src/coreengine/rendering/textures/custom/IDrawable.java create mode 100644 test.png diff --git a/src/main/java/speiger/src/coreengine/NewInputTest.java b/src/main/java/speiger/src/coreengine/NewInputTest.java index d6a08a6..6fe1b5c 100644 --- a/src/main/java/speiger/src/coreengine/NewInputTest.java +++ b/src/main/java/speiger/src/coreengine/NewInputTest.java @@ -1,7 +1,18 @@ package speiger.src.coreengine; -import org.lwjgl.glfw.GLFW; +import java.io.File; +import java.nio.ByteBuffer; +import java.nio.file.Paths; +import javax.imageio.ImageIO; + +import org.lwjgl.glfw.GLFW; +import org.lwjgl.stb.STBImage; +import org.lwjgl.system.MemoryUtil; + +import speiger.src.coreengine.assets.parsers.NativeMemoryParser; +import speiger.src.coreengine.math.misc.ColorSpaces; +import speiger.src.coreengine.math.misc.ColorUtils; import speiger.src.coreengine.rendering.input.devices.FileDrop; import speiger.src.coreengine.rendering.input.devices.Joystick; import speiger.src.coreengine.rendering.input.devices.Keyboard; @@ -28,6 +39,7 @@ public class NewInputTest { FileDrop.INSTANCE.init(bus); manager.addDevices(Mouse.INSTANCE, Keyboard.INSTANCE, Joystick.INSTANCE, FileDrop.INSTANCE); Window window = manager.builder().title("Testing Engine").build(); + testInit(); window.visible(true); while(!window.shouldClose()) { GLFW.glfwPollEvents(); @@ -40,4 +52,20 @@ public class NewInputTest { window.destroy(); manager.destroy(); } + + public void testInit() { + try { + int result = ImageIO.read(new File("test.png")).getRGB(0, 0); + ByteBuffer buffer = NativeMemoryParser.INSTANCE.parseAsset(Paths.get("test.png"), T -> {}); + int[] width = new int[1]; + int[] height = new int[1]; + int[] fileChannels = new int[1]; + ByteBuffer stbBuffer = STBImage.stbi_load_from_memory(buffer, width, height, fileChannels, 4); + int stbResult = stbBuffer.getInt(); + System.out.println("Java="+ColorUtils.toHex(result, true)+", STB="+ColorUtils.toHex(stbResult, true)+", STB_RGB="+ColorUtils.toHex(ColorSpaces.ABGR.toARGB(stbResult), true)); + } + catch(Exception e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/IFontProvider.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/IFontProvider.java index 04d5dd7..144be07 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/IFontProvider.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/providers/IFontProvider.java @@ -2,6 +2,8 @@ package speiger.src.coreengine.rendering.gui.font.providers; import speiger.src.coreengine.rendering.gui.font.glyth.GlythData; -public interface IFontProvider { +public interface IFontProvider extends AutoCloseable { public GlythData glythData(int codepoint); + @Override + void close(); } diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java b/src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java new file mode 100644 index 0000000..323d74b --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/textures/custom/Drawable.java @@ -0,0 +1,124 @@ +package speiger.src.coreengine.rendering.textures.custom; + +import org.lwjgl.stb.STBImage; +import org.lwjgl.system.MemoryUtil; + +import speiger.src.coreengine.math.misc.ColorSpaces; + +public class Drawable implements IDrawable, AutoCloseable { + int width; + int height; + long pixels; + boolean isSTB; + + public Drawable(int width, int height) { + this(width, height, MemoryUtil.nmemAllocChecked(4L * width * height), false); + } + + public Drawable(int width, int height, long pixels, boolean isSTB) { + this.width = width; + this.height = height; + this.pixels = pixels; + this.isSTB = isSTB; + } + + @Override + public ColorSpaces colorspace() { return ColorSpaces.ABGR; } + @Override + public int width() { return width; } + @Override + public int height() { return height; } + public long pixels() { return pixels; } + public boolean isSTBImage() { return isSTB; } + + @Override + public void close() { + if(pixels == 0L) return; + if(isSTB) STBImage.nstbi_image_free(pixels); + else MemoryUtil.nmemFree(pixels); + pixels = 0L; + } + + protected long offset(int x, int y) { + return ((y * width()) + x) << 2; + } + + protected void ensureValid(int index) { + ensureValid(index % width, index / width); + } + + protected void ensureValid(int x, int y) { + if(x < 0 || y < 0) throw new ArrayIndexOutOfBoundsException("Index out of bounds: X=["+x+"], Y=["+y+"]"); + 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"); + } + + @Override + public void set(int index, int data) { + ensureValid(index); + MemoryUtil.memPutInt(index * 4L, data); + } + + @Override + public void setR(int index, int red) { + ensureValid(index); + MemoryUtil.memPutByte(index * 4L, (byte)(red & 0xFF)); + } + + @Override + public void setG(int index, int green) { + ensureValid(index); + MemoryUtil.memPutByte(index * 4L + 1L, (byte)(green & 0xFF)); + } + + @Override + public void setB(int index, int blue) { + ensureValid(index); + MemoryUtil.memPutByte(index * 4L + 2L, (byte)(blue & 0xFF)); + } + + @Override + public void setA(int index, int alpha) { + ensureValid(index); + MemoryUtil.memPutByte(index * 4L + 3L, (byte)(alpha & 0xFF)); + } + + @Override + public void fill(int x, int y, int width, int height, int data) { + ensureValid(x, y); + ensureValid(x+width, y+height); + for(int yOff = 0;yOff= width || y >= height) throw new ArrayIndexOutOfBoundsException("Index out of bounds: X=["+x+"], Y=["+y+"], width=["+width+"], height=["+height+"]"); + if(data == 0L) throw new IllegalStateException("Texture Data isn't bound"); } @Override @@ -110,36 +113,41 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture { @Override public void dirty(int x, int y) { if(id() == 0) return; - dirtySections.add(BitUtil.toInt(x >> 4, y >> 4)); ensureValid(x, y); + dirtySections.add(BitUtil.toInt(x >> 4, y >> 4)); } @Override public void set(int index, int data) { + ensureValid(index); MemoryUtil.memPutInt(index * 4L, data); dirty(index); } @Override public void setR(int index, int red) { + ensureValid(index); MemoryUtil.memPutByte(index * 4L, (byte)(red & 0xFF)); dirty(index); } @Override public void setG(int index, int green) { + ensureValid(index); MemoryUtil.memPutByte(index * 4L + 1L, (byte)(green & 0xFF)); dirty(index); } @Override public void setB(int index, int blue) { + ensureValid(index); MemoryUtil.memPutByte(index * 4L + 2L, (byte)(blue & 0xFF)); dirty(index); } @Override public void setA(int index, int alpha) { + ensureValid(index); MemoryUtil.memPutByte(index * 4L + 3L, (byte)(alpha & 0xFF)); dirty(index); } diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDrawable.java b/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDrawable.java new file mode 100644 index 0000000..ede6c4e --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDrawable.java @@ -0,0 +1,43 @@ +package speiger.src.coreengine.rendering.textures.custom; + +import speiger.src.coreengine.math.misc.ColorSpaces; + +public interface IDrawable { + public ColorSpaces colorspace(); + public int width(); + public int height(); + + public void set(int index, int data); + public default void set(int x, int y, int data) { set((y * width()) + x, data); } + + public default void set(int index, int red, int green, int blue, int alpha) { set(index, colorspace().color(red, green, blue, alpha)); } + public default void set(int x, int y, int red, int green, int blue, int alpha) { set((y * width()) + x, colorspace().color(red, green, blue, alpha)); } + + public void setR(int index, int red); + public default void setR(int x, int y, int red) { setR((y * width()) + x, red); } + + public void setG(int index, int green); + public default void setG(int x, int y, int green) { setG((y * width()) + x, green); } + + public void setB(int index, int blue); + public default void setB(int x, int y, int blue) { setB((y * width()) + x, blue); } + + public void setA(int index, int alpha); + public default void setA(int x, int y, int alpha) { setA((y * width()) + x, alpha); } + + public void fill(int x, int y, int width, int height, int data); + public default void fill(int x, int y, int width, int height, int red, int green, int blue, int alpha) { fill(x, y, width, height, colorspace().color(red, green, blue, alpha)); } + + public int get(int index); + public default int get(int x, int y) { return get((y * width()) + x); } + + public int getR(int index); + public default int getR(int x, int y) { return getR((y * width()) + x); } + public int getG(int index); + public default int getG(int x, int y) { return getG((y * width()) + x); } + public int getB(int index); + public default int getB(int x, int y) { return getB((y * width()) + x); } + public int getA(int index); + public default int getA(int x, int y) { return getA((y * width()) + x); } + +} diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java b/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java index b63bc9e..2c02fae 100644 --- a/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java +++ b/src/main/java/speiger/src/coreengine/rendering/textures/custom/IDynamicTexture.java @@ -1,47 +1,12 @@ package speiger.src.coreengine.rendering.textures.custom; -import speiger.src.coreengine.math.misc.ColorSpaces; import speiger.src.coreengine.rendering.textures.base.ITexture; -public interface IDynamicTexture extends ITexture { +public interface IDynamicTexture extends ITexture, IDrawable { public boolean isDirty(); public void process(boolean full); public default void dirty(int index) { dirty(index % width(), index / width()); } - public void dirty(int x, int y); - - public void set(int index, int data); - public default void set(int x, int y, int data) { set((y * width()) + x, data); } - - public default void set(int index, int red, int green, int blue, int alpha) { set(index, ColorSpaces.ABGR.color(red, green, blue, alpha)); } - public default void set(int x, int y, int red, int green, int blue, int alpha) { set((y * width()) + x, ColorSpaces.ABGR.color(red, green, blue, alpha)); } - - public void setR(int index, int red); - public default void setR(int x, int y, int red) { setR((y * width()) + x, red); } - - public void setG(int index, int green); - public default void setG(int x, int y, int green) { setG((y * width()) + x, green); } - - public void setB(int index, int blue); - public default void setB(int x, int y, int blue) { setB((y * width()) + x, blue); } - - public void setA(int index, int alpha); - public default void setA(int x, int y, int alpha) { setA((y * width()) + x, alpha); } - - public void fill(int x, int y, int width, int height, int data); - public default void fill(int x, int y, int width, int height, int red, int green, int blue, int alpha) { fill(x, y, width, height, ColorSpaces.ABGR.color(red, green, blue, alpha)); } - - public int get(int index); - public default int get(int x, int y) { return get((y * width()) + x); } - - public int getR(int index); - public default int getR(int x, int y) { return getR((y * width()) + x); } - public int getG(int index); - public default int getG(int x, int y) { return getG((y * width()) + x); } - public int getB(int index); - public default int getB(int x, int y) { return getB((y * width()) + x); } - public int getA(int index); - public default int getA(int x, int y) { return getA((y * width()) + x); } - + public void dirty(int x, int y); } diff --git a/test.png b/test.png new file mode 100644 index 0000000000000000000000000000000000000000..9470dc66296737658a7299bd89346a68a649c3a5 GIT binary patch literal 70 zcmeAS@N?(olHy`uVBq!ia0vp^j3CUx1|;Q0k92}1TpU9x;*u-MpS{y(;JC^>DMaD) Q44?#qr>mdKI;Vst0Kv2n1ONa4 literal 0 HcmV?d00001