From 30df3e578cef533cacedf737449bbd35d4ea5d11 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Fri, 15 Apr 2022 18:20:30 +0900 Subject: fix shadow and add nanovg support --- build.gradle | 57 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 11 deletions(-) (limited to 'build.gradle') diff --git a/build.gradle b/build.gradle index 7efe09c..558b4c9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,7 @@ //file:noinspection UnnecessaryQualifiedReference //file:noinspection GroovyAssignabilityCheck +//file:noinspection GradlePackageUpdate +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar plugins { id "dev.architectury.architectury-pack200" version "0.1.3" @@ -26,7 +28,7 @@ compileJava.options.encoding = 'UTF-8' loom { launchConfigs { client { - + property("fml.coreMods.load", "io.polyfrost.oneconfig.lwjgl.plugin.LoadingPlugin") } } runConfigs { @@ -42,16 +44,43 @@ loom { configurations { include implementation.extendsFrom(include) + lwjgl + lwjglNative { + transitive = false + } } +sourceSets.main.runtimeClasspath += configurations.lwjglNative + repositories { maven { url 'https://repo.woverflow.cc/' } } +task lwjglJar(type: ShadowJar) { + group = "shadow" + archiveClassifier.set("lwjgl") + configurations = [project.configurations.lwjgl] + exclude "META-INF/versions/**" + exclude "**/module-info.class" + exclude "**/package-info.class" + relocate("org.lwjgl", "org.lwjgl3") { + include "org.lwjgl.PointerBuffer" + include "org.lwjgl.BufferUtils" + } +} + dependencies { minecraft("com.mojang:minecraft:1.8.9") mappings("de.oceanlabs.mcp:mcp_stable:22-1.8.9") forge("net.minecraftforge:forge:1.8.9-11.15.1.2318-1.8.9") + + lwjgl "org.lwjgl:lwjgl:3.3.0" + lwjgl "org.lwjgl:lwjgl-tinyfd:3.3.0" + lwjgl "org.lwjgl:lwjgl-nanovg:3.3.0" + lwjglNative "org.lwjgl:lwjgl:3.3.0:natives-windows" + lwjglNative "org.lwjgl:lwjgl-tinyfd:3.3.0:natives-windows" + lwjglNative "org.lwjgl:lwjgl-nanovg:3.3.0:natives-windows" + implementation lwjglJar.outputs.files } processResources { @@ -71,28 +100,34 @@ processResources { rename '(.+_at.cfg)', 'META-INF/$1' } +/*/ sourceSets { main { output.resourcesDir = java.classesDirectory } } + */ + +shadowJar { + archiveClassifier.set('dev') + configurations = [project.configurations.include, project.configurations.lwjglNative] + duplicatesStrategy DuplicatesStrategy.EXCLUDE +} + remapJar { - archiveClassifier = "nodeps" + archiveClassifier.set('') + from(shadowJar.archiveFile) } jar { manifest.attributes( 'ModSide': 'CLIENT', 'ForceLoadAsMod': true, - "TweakOrder": "0" + "TweakOrder": "0", + "FMLCorePlugin": "io.polyfrost.oneconfig.lwjgl.plugin.LoadingPlugin", + "FMLCorePluginContainsFMLMod": "lol" ) + enabled = false } - -shadowJar { - archiveClassifier.set('') - from(remapJar.archiveFile) - configurations = [project.configurations.include] - duplicatesStrategy DuplicatesStrategy.EXCLUDE -} -assemble.dependsOn shadowJar \ No newline at end of file +jar.dependsOn(shadowJar) \ No newline at end of file -- cgit From fff43b4d2a89ae50aa1d315fc4dad65055e654be Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 16 Apr 2022 01:55:58 +0900 Subject: misc things --- build.gradle | 3 - .../java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 4 +- .../io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java | 20 ++--- .../io/polyfrost/oneconfig/test/TestNanoVGGui.java | 5 +- .../resources/assets/oneconfig/font/Inter-Bold.ttf | Bin 0 -> 316100 bytes .../assets/oneconfig/font/OFL-INTER-BOLD.txt | 93 +++++++++++++++++++++ .../assets/oneconfig/font/Roboto-Regular.ttf | Bin 145348 -> 0 bytes 7 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 src/main/resources/assets/oneconfig/font/Inter-Bold.ttf create mode 100644 src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt delete mode 100644 src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf (limited to 'build.gradle') diff --git a/build.gradle b/build.gradle index 558b4c9..996b549 100644 --- a/build.gradle +++ b/build.gradle @@ -100,15 +100,12 @@ processResources { rename '(.+_at.cfg)', 'META-INF/$1' } -/*/ sourceSets { main { output.resourcesDir = java.classesDirectory } } - */ - shadowJar { archiveClassifier.set('dev') configurations = [project.configurations.include, project.configurations.lwjglNative] diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java index d0f54f7..eb830df 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java @@ -14,6 +14,7 @@ import java.nio.file.Paths; import static org.lwjgl.BufferUtils.createByteBuffer; import static org.lwjgl.system.MemoryUtil.memSlice; +@SuppressWarnings("RedundantCast") final class IOUtil { private IOUtil() { @@ -21,7 +22,7 @@ final class IOUtil { private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) { ByteBuffer newBuffer = createByteBuffer(newCapacity); - buffer.flip(); + ((Buffer) buffer).flip(); newBuffer.put(buffer); return newBuffer; } @@ -57,7 +58,6 @@ final class IOUtil { } } - //noinspection RedundantCast ((Buffer) buffer).flip(); return memSlice(buffer); } diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java index c9d0615..f8dcafb 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java @@ -6,14 +6,13 @@ import net.minecraft.client.shader.Framebuffer; import org.lwjgl.nanovg.NVGColor; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; -import org.lwjgl.system.MemoryStack; import java.io.IOException; -import java.nio.ByteBuffer; import java.util.function.LongConsumer; import static org.lwjgl.nanovg.NanoVG.*; -import static org.lwjgl.nanovg.NanoVGGL2.*; +import static org.lwjgl.nanovg.NanoVGGL2.NVG_ANTIALIAS; +import static org.lwjgl.nanovg.NanoVGGL2.nvgCreate; public final class NanoVGUtils { private NanoVGUtils() { @@ -31,7 +30,7 @@ public final class NanoVGUtils { } if (font == -1) { try { - font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Roboto-Regular.ttf", 150 * 1024), 0); + font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Inter-Bold.ttf", 200 * 1024), 0); } catch (IOException e) { e.printStackTrace(); } @@ -80,15 +79,10 @@ public final class NanoVGUtils { nvgBeginPath(vg); nvgFontSize(vg, size); nvgFontFace(vg, "custom-font"); - nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE); - try (MemoryStack stack = MemoryStack.stackPush()) { - ByteBuffer textByte = stack.ASCII(text, false); - nvgFontBlur(vg, 0); - color(vg, color); - nvgText(vg, x, y, textByte); - } catch (Exception e) { - e.printStackTrace(); - } + nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); + color(vg, color); + nvgText(vg, x, y, text); + nvgFill(vg); } public static void color(long vg, int color) { diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java index 74d6a1e..67e0a68 100644 --- a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java +++ b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java @@ -10,10 +10,11 @@ public class TestNanoVGGui extends GuiScreen { @Override public void drawScreen(int mouseX, int mouseY, float partialTicks) { super.drawScreen(mouseX, mouseY, partialTicks); + drawRect(0, 0, width, height, Color.BLACK.getRGB()); NanoVGUtils.setupAndDraw((vg) -> { NanoVGUtils.drawRect(vg, 0, 0, 300, 300, Color.BLUE.getRGB()); - NanoVGUtils.drawRoundedRect(vg, 305, 305, 100, 100, Color.BLACK.getRGB(), 8); - NanoVGUtils.drawString(vg, "Hello!", 500, 500, Color.BLACK.getRGB(), 50); + NanoVGUtils.drawRoundedRect(vg, 305, 305, 100, 100, Color.YELLOW.getRGB(), 8); + NanoVGUtils.drawString(vg, "Hello!", 500, 500, Color.WHITE.getRGB(), 50); }); } } diff --git a/src/main/resources/assets/oneconfig/font/Inter-Bold.ttf b/src/main/resources/assets/oneconfig/font/Inter-Bold.ttf new file mode 100644 index 0000000..76a215c Binary files /dev/null and b/src/main/resources/assets/oneconfig/font/Inter-Bold.ttf differ diff --git a/src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt b/src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt new file mode 100644 index 0000000..b525cbf --- /dev/null +++ b/src/main/resources/assets/oneconfig/font/OFL-INTER-BOLD.txt @@ -0,0 +1,93 @@ +Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf b/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf deleted file mode 100644 index 3e6e2e7..0000000 Binary files a/src/main/resources/assets/oneconfig/font/Roboto-Regular.ttf and /dev/null differ -- cgit From f10f1165a7c2ea88ce7bb265d51b52eeaa64d8f8 Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 16 Apr 2022 12:33:00 +0900 Subject: nanovg optimizations + image renderer --- build.gradle | 2 + .../java/io/polyfrost/oneconfig/lwjgl/IOUtil.java | 86 ++++++++++------------ .../io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java | 39 ++++++++-- .../io/polyfrost/oneconfig/lwjgl/image/Image.java | 20 +++++ .../oneconfig/lwjgl/image/ImageLoader.java | 39 ++++++++++ .../io/polyfrost/oneconfig/test/TestNanoVGGui.java | 1 + 6 files changed, 132 insertions(+), 55 deletions(-) create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/image/Image.java create mode 100644 src/main/java/io/polyfrost/oneconfig/lwjgl/image/ImageLoader.java (limited to 'build.gradle') diff --git a/build.gradle b/build.gradle index 996b549..35e80c1 100644 --- a/build.gradle +++ b/build.gradle @@ -75,9 +75,11 @@ dependencies { forge("net.minecraftforge:forge:1.8.9-11.15.1.2318-1.8.9") lwjgl "org.lwjgl:lwjgl:3.3.0" + lwjgl "org.lwjgl:lwjgl-stb:3.3.0" lwjgl "org.lwjgl:lwjgl-tinyfd:3.3.0" lwjgl "org.lwjgl:lwjgl-nanovg:3.3.0" lwjglNative "org.lwjgl:lwjgl:3.3.0:natives-windows" + lwjglNative "org.lwjgl:lwjgl-stb:3.3.0:natives-windows" lwjglNative "org.lwjgl:lwjgl-tinyfd:3.3.0:natives-windows" lwjglNative "org.lwjgl:lwjgl-nanovg:3.3.0:natives-windows" implementation lwjglJar.outputs.files diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java index eb830df..f5105fb 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/IOUtil.java @@ -1,65 +1,53 @@ package io.polyfrost.oneconfig.lwjgl; -import java.io.IOException; -import java.io.InputStream; +import org.apache.commons.io.IOUtils; + +import java.io.*; +import java.net.URL; import java.nio.Buffer; import java.nio.ByteBuffer; -import java.nio.channels.Channels; -import java.nio.channels.ReadableByteChannel; -import java.nio.channels.SeekableByteChannel; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import static org.lwjgl.BufferUtils.createByteBuffer; -import static org.lwjgl.system.MemoryUtil.memSlice; +import java.nio.ByteOrder; -@SuppressWarnings("RedundantCast") -final class IOUtil { +public final class IOUtil { private IOUtil() { } - private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) { - ByteBuffer newBuffer = createByteBuffer(newCapacity); - ((Buffer) buffer).flip(); - newBuffer.put(buffer); - return newBuffer; - } - - static ByteBuffer resourceToByteBuffer(String resource, int bufferSize) throws IOException { - ByteBuffer buffer; - - Path path = Paths.get(resource); - if (Files.isReadable(path)) { - try (SeekableByteChannel fc = Files.newByteChannel(path)) { - buffer = createByteBuffer((int)fc.size() + 1); - while (fc.read(buffer) != -1) { - //noinspection UnnecessarySemicolon - ; - } - } + /** + * Taken from legui under MIT License + * https://github.com/SpinyOwl/legui/blob/develop/LICENSE + */ + @SuppressWarnings("RedundantCast") + public static ByteBuffer resourceToByteBuffer(String path) throws IOException { + byte[] bytes; + path = path.trim(); + if (path.startsWith("http")) { + bytes = IOUtils.toByteArray(new URL(path)); } else { - try ( - InputStream source = IOUtil.class.getResourceAsStream(resource); - ReadableByteChannel rbc = Channels.newChannel(source) - ) { - buffer = createByteBuffer(bufferSize); - - while (true) { - int bytes = rbc.read(buffer); - if (bytes == -1) { - break; - } - if (buffer.remaining() == 0) { - buffer = resizeBuffer(buffer, buffer.capacity() * 3 / 2); // 50% - } - } + InputStream stream; + File file = new File(path); + if (file.exists() && file.isFile()) { + stream = new FileInputStream(file); + } else { + stream = IOUtil.class.getResourceAsStream(path); + } + if (stream == null) { + throw new FileNotFoundException(path); } + bytes = IOUtils.toByteArray(stream); } + ByteBuffer data = ByteBuffer.allocateDirect(bytes.length).order(ByteOrder.nativeOrder()) + .put(bytes); + ((Buffer) data).flip(); + return data; + } - ((Buffer) buffer).flip(); - return memSlice(buffer); + public static ByteBuffer resourceToByteBufferNullable(String path) { + try { + return resourceToByteBuffer(path); + } catch (Exception ignored) { + return null; + } } } \ No newline at end of file diff --git a/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java index f8dcafb..fc556fe 100644 --- a/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java +++ b/src/main/java/io/polyfrost/oneconfig/lwjgl/NanoVGUtils.java @@ -1,13 +1,18 @@ package io.polyfrost.oneconfig.lwjgl; +import io.polyfrost.oneconfig.lwjgl.image.Image; +import io.polyfrost.oneconfig.lwjgl.image.ImageLoader; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.shader.Framebuffer; import org.lwjgl.nanovg.NVGColor; +import org.lwjgl.nanovg.NVGPaint; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; import java.util.function.LongConsumer; import static org.lwjgl.nanovg.NanoVG.*; @@ -20,6 +25,7 @@ public final class NanoVGUtils { } private static long vg = -1; private static int font = -1; + private static final ArrayList fontBuffers = new ArrayList<>(); public static void setupAndDraw(LongConsumer consumer) { if (vg == -1) { @@ -30,7 +36,9 @@ public final class NanoVGUtils { } if (font == -1) { try { - font = nvgCreateFontMem(vg, "custom-font", IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Inter-Bold.ttf", 200 * 1024), 0); + ByteBuffer buffer = IOUtil.resourceToByteBuffer("/assets/oneconfig/font/Inter-Bold.ttf"); + font = nvgCreateFontMem(vg, "custom-font", buffer, 0); + fontBuffers.add(buffer); } catch (IOException e) { e.printStackTrace(); } @@ -57,22 +65,26 @@ public final class NanoVGUtils { public static void drawRect(long vg, float x, float y, float width, float height, int color) { nvgBeginPath(vg); nvgRect(vg, x, y, width, height); - color(vg, color); + NVGColor nvgColor = color(vg, color); nvgFill(vg); + nvgColor.free(); } public static void drawRoundedRect(long vg, float x, float y, float width, float height, int color, float radius) { nvgBeginPath(vg); nvgRoundedRect(vg, x, y, width, height, radius); color(vg, color); + NVGColor nvgColor = color(vg, color); nvgFill(vg); + nvgColor.free(); } public static void drawCircle(long vg, float x, float y, float radius, int color) { nvgBeginPath(vg); nvgCircle(vg, x, y, radius); - color(vg, color); + NVGColor nvgColor = color(vg, color); nvgFill(vg); + nvgColor.free(); } public static void drawString(long vg, String text, float x, float y, int color, float size) { @@ -80,14 +92,29 @@ public final class NanoVGUtils { nvgFontSize(vg, size); nvgFontFace(vg, "custom-font"); nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); - color(vg, color); + NVGColor nvgColor = color(vg, color); nvgText(vg, x, y, text); nvgFill(vg); + nvgColor.free(); + } + + public static void drawImage(long vg, String fileName, float x, float y, float width, float height) { + if (ImageLoader.INSTANCE.loadImage(vg, fileName)) { + NVGPaint imagePaint = NVGPaint.calloc(); + Image image = ImageLoader.INSTANCE.getImage(fileName); + nvgBeginPath(vg); + nvgImagePattern(vg, x, y, width, height, 0, image.getReference(), 1, imagePaint); + nvgRect(vg, x, y, width, height); + nvgFillPaint(vg, imagePaint); + nvgFill(vg); + imagePaint.free(); + } } - public static void color(long vg, int color) { - NVGColor nvgColor = NVGColor.create(); + public static NVGColor color(long vg, int color) { + NVGColor nvgColor = NVGColor.calloc(); nvgRGBA((byte) (color >> 16 & 0xFF), (byte) (color >> 8 & 0xFF), (byte) (color & 0xFF), (byte) (color >> 24 & 0xFF), nvgColor); nvgFillColor(vg, nvgColor); + return nvgColor; } } 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 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); + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java index 67e0a68..3f2fa27 100644 --- a/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java +++ b/src/main/java/io/polyfrost/oneconfig/test/TestNanoVGGui.java @@ -15,6 +15,7 @@ public class TestNanoVGGui extends GuiScreen { NanoVGUtils.drawRect(vg, 0, 0, 300, 300, Color.BLUE.getRGB()); NanoVGUtils.drawRoundedRect(vg, 305, 305, 100, 100, Color.YELLOW.getRGB(), 8); NanoVGUtils.drawString(vg, "Hello!", 500, 500, Color.WHITE.getRGB(), 50); + NanoVGUtils.drawImage(vg, "/assets/oneconfig/textures/hudsettings.png", 10, 10, 400, 400); }); } } -- cgit