diff options
author | MoonTidez <79183852+MoonTidez@users.noreply.github.com> | 2022-05-15 19:50:30 +0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-15 17:50:30 +0200 |
commit | 6254bbc42d0611f8eaee97b6d4c7710d082c6826 (patch) | |
tree | 78337ed22153af7cb992eb98e3a569b9a372d534 /src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java | |
parent | 2c592090b9448203559e01326bc2c2d995b15d53 (diff) | |
download | OneConfig-6254bbc42d0611f8eaee97b6d4c7710d082c6826.tar.gz OneConfig-6254bbc42d0611f8eaee97b6d4c7710d082c6826.tar.bz2 OneConfig-6254bbc42d0611f8eaee97b6d4c7710d082c6826.zip |
SVG + asset reorganization (#10)
* Assets reorganization
* forgot 4 pixels in alpha grid oops
* Updated huegradient.png
Reduces file size by only use 1px height instead of 720px while maintaining quality
* we do a little renaming
* new assets mapping
* moon I hate you
* e
* moon is weird
* make it compile
* temp
* part png rendering
* svgs
* fix some positionings and stuff
Co-authored-by: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java | 84 |
1 files changed, 79 insertions, 5 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java index 479104c..128ae48 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/ImageLoader.java @@ -1,14 +1,21 @@ package cc.polyfrost.oneconfig.lwjgl.image; import cc.polyfrost.oneconfig.utils.IOUtils; +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 java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; import java.nio.ByteBuffer; import java.util.HashMap; public class ImageLoader { - private final HashMap<String, Image> imageHashMap = new HashMap<>(); + private final HashMap<String, Integer> imageHashMap = new HashMap<>(); + private final HashMap<String, Integer> SVGHashMap = new HashMap<>(); public static ImageLoader INSTANCE = new ImageLoader(); public boolean loadImage(long vg, String fileName) { @@ -27,19 +34,86 @@ public class ImageLoader { 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)); + imageHashMap.put(fileName, NanoVG.nvgCreateImageRGBA(vg, width[0], height[0], NanoVG.NVG_IMAGE_REPEATX | NanoVG.NVG_IMAGE_REPEATY | NanoVG.NVG_IMAGE_GENERATE_MIPMAPS, buffer)); return true; } return true; } + public boolean loadSVG(long vg, String fileName, float SVGWidth, float SVGHeight) { + String name = fileName + "-" + SVGWidth + "-" + SVGHeight; + if (!SVGHashMap.containsKey(name)) { + try { + InputStream inputStream = this.getClass().getResourceAsStream(fileName); + if (inputStream == null) return false; + StringBuilder resultStringBuilder = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { + String line; + while ((line = br.readLine()) != null) { + resultStringBuilder.append(line); + } + } + CharSequence s = resultStringBuilder.toString(); + NSVGImage svg = NanoSVG.nsvgParse(s, "px", 96f); + if (svg == null) return false; + long rasterizer = NanoSVG.nsvgCreateRasterizer(); - public void removeImage(String fileName) { - imageHashMap.remove(fileName); + int w = (int) svg.width(); + int h = (int) svg.height(); + float scale = Math.max(SVGWidth / w, SVGHeight / h); + w = (int) (w * scale); + h = (int) (h * scale); + + ByteBuffer image = MemoryUtil.memAlloc(w * h * 4); + NanoSVG.nsvgRasterize(rasterizer, svg, 0, 0, scale, image, w, h, w * 4); + + NanoSVG.nsvgDeleteRasterizer(rasterizer); + NanoSVG.nsvgDelete(svg); + + SVGHashMap.put(name, NanoVG.nvgCreateImageRGBA(vg, w, h, NanoVG.NVG_IMAGE_REPEATX | NanoVG.NVG_IMAGE_REPEATY | NanoVG.NVG_IMAGE_GENERATE_MIPMAPS, image)); + return true; + } catch (Exception e) { + System.err.println("Failed to parse SVG file"); + e.printStackTrace(); + return false; + } + } + return true; } - public Image getImage(String fileName) { + public int getImage(String fileName) { return imageHashMap.get(fileName); } + public void removeImage(long vg, String fileName) { + NanoVG.nvgDeleteImage(vg, imageHashMap.get(fileName)); + imageHashMap.remove(fileName); + } + + public void clearImages(long vg) { + HashMap<String, Integer> temp = new HashMap<>(imageHashMap); + for (String image : temp.keySet()) { + NanoVG.nvgDeleteImage(vg, imageHashMap.get(image)); + imageHashMap.remove(image); + } + } + + public int getSVG( String fileName, float width, float height) { + String name = fileName + "-" + width + "-" + height; + return SVGHashMap.get(name); + } + + public void removeSVG(long vg, String fileName, float width, float height) { + String name = fileName + "-" + width + "-" + height; + NanoVG.nvgDeleteImage(vg, imageHashMap.get(name)); + SVGHashMap.remove(name); + } + + public void clearSVGs(long vg) { + HashMap<String, Integer> temp = new HashMap<>(SVGHashMap); + for (String image : temp.keySet()) { + NanoVG.nvgDeleteImage(vg, SVGHashMap.get(image)); + SVGHashMap.remove(image); + } + } } |