aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/lwjgl/image
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-04-16 12:33:00 +0900
committerWyvest <45589059+Wyvest@users.noreply.github.com>2022-04-16 12:33:00 +0900
commitf10f1165a7c2ea88ce7bb265d51b52eeaa64d8f8 (patch)
treefc1e827d37476b510bca7e102ffad17a46052bc6 /src/main/java/io/polyfrost/oneconfig/lwjgl/image
parentfff43b4d2a89ae50aa1d315fc4dad65055e654be (diff)
downloadOneConfig-f10f1165a7c2ea88ce7bb265d51b52eeaa64d8f8.tar.gz
OneConfig-f10f1165a7c2ea88ce7bb265d51b52eeaa64d8f8.tar.bz2
OneConfig-f10f1165a7c2ea88ce7bb265d51b52eeaa64d8f8.zip
nanovg optimizations + image renderer
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/lwjgl/image')
-rw-r--r--src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java20
-rw-r--r--src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java39
2 files changed, 59 insertions, 0 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java
new file mode 100644
index 0000000..2ed7276
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java
@@ -0,0 +1,20 @@
+package io.polyfrost.oneconfig.lwjgl.image;
+
+import java.nio.ByteBuffer;
+
+public class Image {
+ private final int reference;
+ private final ByteBuffer buffer;
+ public Image(int reference, ByteBuffer buffer) {
+ this.reference = reference;
+ this.buffer = buffer;
+ }
+
+ public ByteBuffer getBuffer() {
+ return buffer;
+ }
+
+ public int getReference() {
+ return reference;
+ }
+}
diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java
new file mode 100644
index 0000000..b0cb6d4
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java
@@ -0,0 +1,39 @@
+package io.polyfrost.oneconfig.lwjgl.image;
+
+import io.polyfrost.oneconfig.lwjgl.IOUtil;
+import org.lwjgl.nanovg.NanoVG;
+import org.lwjgl.stb.STBImage;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+
+public class ImageLoader {
+ private final HashMap<String, Image> imageHashMap = new HashMap<>();
+ public static ImageLoader INSTANCE = new ImageLoader();
+
+ public boolean loadImage(long vg, String fileName) {
+ if (!imageHashMap.containsKey(fileName)) {
+ int[] width = {0};
+ int[] height = {0};
+ int[] channels = {0};
+
+ ByteBuffer image = IOUtil.resourceToByteBufferNullable(fileName);
+ if (image == null) {
+ return false;
+ }
+
+ ByteBuffer buffer = STBImage.stbi_load_from_memory(image, width, height, channels, 4);
+ if (buffer == null) {
+ return false;
+ }
+
+ imageHashMap.put(fileName, new Image(NanoVG.nvgCreateImageRGBA(vg, width[0], height[0], NanoVG.NVG_IMAGE_REPEATX | NanoVG.NVG_IMAGE_REPEATY | NanoVG.NVG_IMAGE_GENERATE_MIPMAPS, buffer), buffer));
+ return true;
+ }
+ return true;
+ }
+
+ public Image getImage(String fileName) {
+ return imageHashMap.get(fileName);
+ }
+}