more work on fonts and a dynamic texture

This commit is contained in:
Speiger 2024-06-16 20:51:33 +02:00
parent c01d317fdb
commit 55957d3640
7 changed files with 211 additions and 41 deletions

View File

@ -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();
}
}
}

View File

@ -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();
}

View File

@ -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<height;yOff++) {
MemoryUtil.memSet(offset(x, y+yOff), data, width * 4L);
}
}
@Override
public int get(int index) {
ensureValid(index);
return MemoryUtil.memGetInt(index * 4L);
}
@Override
public int getR(int index) {
ensureValid(index);
return MemoryUtil.memGetByte(index * 4L);
}
@Override
public int getG(int index) {
ensureValid(index);
return MemoryUtil.memGetByte(index * 4L + 1L);
}
@Override
public int getB(int index) {
ensureValid(index);
return MemoryUtil.memGetByte(index * 4L + 2L);
}
@Override
public int getA(int index) {
ensureValid(index);
return MemoryUtil.memGetByte(index * 4L + 3L);
}
}

View File

@ -8,6 +8,7 @@ import speiger.src.collections.ints.sets.IntLinkedOpenHashSet;
import speiger.src.collections.ints.sets.IntSet;
import speiger.src.coreengine.assets.base.IAssetProvider;
import speiger.src.coreengine.math.BitUtil;
import speiger.src.coreengine.math.misc.ColorSpaces;
import speiger.src.coreengine.rendering.textures.base.BaseTexture;
import speiger.src.coreengine.rendering.textures.base.TextureMetadata;
import speiger.src.coreengine.rendering.utils.GLStateTracker;
@ -51,7 +52,8 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture {
public int width() { return width; }
@Override
public int height() { return height; }
@Override
public ColorSpaces colorspace() { return ColorSpaces.ABGR; }
@Override
public boolean isDirty() { return !dirtySections.isEmpty(); }
@ -90,6 +92,7 @@ public class DynamicTexture extends BaseTexture implements IDynamicTexture {
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(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);
}

View File

@ -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); }
}

View File

@ -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);
}

BIN
test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 B