aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
diff options
context:
space:
mode:
authorisXander <xandersmith2008@gmail.com>2023-05-23 23:15:24 +0100
committerisXander <xandersmith2008@gmail.com>2023-05-23 23:15:24 +0100
commit3b8e4e76b4893fc910e6af1024c3710b6f87954b (patch)
tree51438e3fa038719fbf25c788e7b9ebb7e4e0a930 /common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
parent97dd0f861434ac8faf28ce80988d24ca77b091ea (diff)
downloadYetAnotherConfigLib-3b8e4e76b4893fc910e6af1024c3710b6f87954b.tar.gz
YetAnotherConfigLib-3b8e4e76b4893fc910e6af1024c3710b6f87954b.tar.bz2
YetAnotherConfigLib-3b8e4e76b4893fc910e6af1024c3710b6f87954b.zip
Fix animated image rendering
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java')
-rw-r--r--common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java30
1 files changed, 25 insertions, 5 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java b/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
index 46deb87..1c9ac2e 100644
--- a/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
+++ b/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java
@@ -28,6 +28,7 @@ import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
+import java.util.stream.IntStream;
public interface ImageRenderer {
int render(GuiGraphics graphics, int x, int y, int width);
@@ -223,8 +224,20 @@ public interface ImageRenderer {
private static AnimatedNativeImageBacked createFromImageReader(ImageReader reader, int frameDelayMS, ResourceLocation uniqueLocation) throws IOException {
int frameCount = reader.getNumImages(true);
- int frameWidth = reader.getWidth(0);
- int frameHeight = reader.getHeight(0);
+ int frameWidth = IntStream.range(reader.getMinIndex(), frameCount).map(i -> {
+ try {
+ return reader.getWidth(i);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }).max().orElseThrow();
+ int frameHeight = IntStream.range(reader.getMinIndex(), frameCount).map(i -> {
+ try {
+ return reader.getHeight(i);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }).max().orElseThrow();
// Packs the frames into an optimal 1:1 texture.
// OpenGL can only have texture axis with a max of 32768 pixels,
@@ -234,8 +247,15 @@ public interface ImageRenderer {
int rows = (int)Math.ceil(frameCount / (double)cols);
NativeImage image = new NativeImage(frameWidth * cols, frameHeight * rows, true);
+ for (int x = 0; x < frameWidth * cols; x++) {
+ for (int y = 0; y < frameHeight * rows; y++) {
+ image.setPixelRGBA(x, y, 0xFF000000);
+ }
+ }
for (int i = reader.getMinIndex(); i < frameCount - 1; i++) {
BufferedImage bi = reader.read(i);
+ int xOffset = (frameWidth - bi.getWidth()) / 2;
+ int yOffset = (frameHeight - bi.getHeight()) / 2;
for (int w = 0; w < bi.getWidth(); w++) {
for (int h = 0; h < bi.getHeight(); h++) {
int rgb = bi.getRGB(w, h);
@@ -247,8 +267,8 @@ public interface ImageRenderer {
int row = (int) Math.floor(i / (double)cols);
image.setPixelRGBA(
- bi.getWidth() * col + w,
- bi.getHeight() * row + h,
+ frameWidth * col + w + xOffset,
+ frameHeight * row + h + yOffset,
FastColor.ABGR32.color(255, b, g, r) // NativeImage uses ABGR for some reason
);
}
@@ -287,7 +307,7 @@ public interface ImageRenderer {
currentFrame++;
lastFrameTime = timeMS;
}
- if (currentFrame >= frameCount) currentFrame = 0;
+ if (currentFrame >= frameCount - 1) currentFrame = 0;
return targetHeight;
}