aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java
diff options
context:
space:
mode:
authornextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com>2022-06-23 17:54:47 +0100
committernextdaydelivery <79922345+nxtdaydelivery@users.noreply.github.com>2022-06-23 17:54:47 +0100
commitd4463b33342ccb5ea86c2a2c9da37b9b861b6674 (patch)
treebcd001862452a4f775fc9415d2bd4f433d39714d /src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java
parentec663490bd1e2c9933844761348c20f7b1d84613 (diff)
downloadOneConfig-d4463b33342ccb5ea86c2a2c9da37b9b861b6674.tar.gz
OneConfig-d4463b33342ccb5ea86c2a2c9da37b9b861b6674.tar.bz2
OneConfig-d4463b33342ccb5ea86c2a2c9da37b9b861b6674.zip
image stuff
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java b/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java
index ee6c326..379a3e2 100644
--- a/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java
+++ b/src/main/java/cc/polyfrost/oneconfig/renderer/AssetLoader.java
@@ -3,17 +3,22 @@ package cc.polyfrost.oneconfig.renderer;
import cc.polyfrost.oneconfig.internal.assets.Images;
import cc.polyfrost.oneconfig.internal.assets.SVGs;
import cc.polyfrost.oneconfig.utils.IOUtils;
+import org.lwjgl.BufferUtils;
import org.lwjgl.nanovg.NSVGImage;
import org.lwjgl.nanovg.NanoSVG;
import org.lwjgl.nanovg.NanoVG;
import org.lwjgl.stb.STBImage;
import org.lwjgl.system.MemoryUtil;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
+import java.nio.IntBuffer;
import java.util.HashMap;
+import java.util.Objects;
/**
* Loads images and SVGs from resources into NanoVG.
@@ -36,7 +41,7 @@ public final class AssetLoader {
*
* @param vg The NanoVG context.
* @param fileName The name of the file to load.
- * @return Whether the assets was loaded successfully.
+ * @return Whether the asset was loaded successfully.
*/
public boolean loadImage(long vg, String fileName) {
if (!imageHashMap.containsKey(fileName)) {
@@ -189,4 +194,31 @@ public final class AssetLoader {
svgHashMap.remove(image);
}
}
+
+ /**
+ * Convert the given image (as a quantified path) to an IntBuffer, of its pixels, in order, stored as integers in ARGB format.
+ * Mostly an internal method; used by LWJGL.
+ * @param fileName quantified path to the image
+ * @return intBuffer of the image's pixels in ARGB format
+ */
+ public IntBuffer imageToIntBuffer(String fileName) {
+ try {
+ InputStream inputStream = this.getClass().getResourceAsStream(fileName);
+ BufferedImage img = ImageIO.read(Objects.requireNonNull(inputStream));
+ int width = img.getWidth();
+ int height = img.getHeight();
+ IntBuffer intBuffer = BufferUtils.createIntBuffer(256);
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ intBuffer.put(img.getRGB(x, y));
+ }
+ }
+ return intBuffer;
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.err.println("Failed to load asset: " + fileName);
+ return BufferUtils.createIntBuffer(256);
+ }
+
+ }
}