Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove AWT references to increase compatibility for Macs #30

Open
wants to merge 1 commit into
base: 1.16-forge
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/main/java/com/unascribed/blockrenderer/ClientRenderHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.unascribed.blockrenderer;

import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.ByteBuffer;
import java.text.DateFormat;
Expand All @@ -16,8 +15,6 @@
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
Expand All @@ -41,6 +38,7 @@
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.texture.NativeImage;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.util.InputMappings;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -262,7 +260,7 @@ private ITextComponent render(Minecraft mc, RenderTask task, int size, File fold
BlockRenderer.log.warn("Failed to render "+task.getId(), t);
return new TranslationTextComponent("msg.blockrenderer.render.fail");
}
BufferedImage image = readPixels(size, size);
NativeImage image = readPixels(size, size);
tearDownRenderState();
// This code would need to be refactored to perform this save off-thread and not block the
// main thread. This is a problem for later.
Expand All @@ -285,7 +283,7 @@ private CompletableFuture<Void> createFuture(List<RenderTask> tasks, int size, F
// render the entire batch gathering the images for it
// and revert the render state
setUpRenderState(Minecraft.getInstance(), size);
List<Pair<RenderTask, BufferedImage>> images = new ArrayList<>();
List<Pair<RenderTask, NativeImage>> images = new ArrayList<>();
for (RenderTask task : tasks) {
RenderSystem.clearColor(0, 0, 0, 0);
RenderSystem.clear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT, Minecraft.IS_RUNNING_ON_MAC);
Expand All @@ -302,13 +300,13 @@ private CompletableFuture<Void> createFuture(List<RenderTask> tasks, int size, F
if (pendingBulkItems) types++;
if (pendingBulkEntities) types++;
if (pendingBulkStructures) types++;
for (Pair<RenderTask, BufferedImage> image : images) {
for (Pair<RenderTask, NativeImage> image : images) {
saveImage(image.getSecond(), image.getFirst(), types > 1 ? new File(folder, image.getFirst().getCategory()) : folder, includeDateInFilename);
}
}, Util.getServerExecutor());
}

private static File saveImage(BufferedImage image, RenderTask task, File folder, boolean includeDateInFilename) {
private static File saveImage(NativeImage image, RenderTask task, File folder, boolean includeDateInFilename) {
try {
String fileName = (includeDateInFilename ? dateFormat.format(new Date()) + "_" : "") + sanitize(task.getDisplayName());
File f = new File(folder, fileName + ".png");
Expand All @@ -318,8 +316,7 @@ private static File saveImage(BufferedImage image, RenderTask task, File folder,
i++;
}
Files.createParentDirs(f);
f.createNewFile();
ImageIO.write(image, "PNG", f);
image.write(f);
return f;
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -331,16 +328,17 @@ private static String sanitize(String str) {
return str.replaceAll("[^A-Za-z0-9-_ ]", "_");
}

private static BufferedImage readPixels(int width, int height) {
private static NativeImage readPixels(int width, int height) {
ByteBuffer buf = BufferUtils.createByteBuffer(width * height * 4);
RenderSystem.readPixels(0, Minecraft.getInstance().getMainWindow().getHeight() - height, width, height, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buf);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
RenderSystem.readPixels(0, Minecraft.getInstance().getMainWindow().getHeight() - height, width, height, GL12.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
NativeImage img = new NativeImage(width, height, false);
int[] pixels = new int[width * height];
buf.asIntBuffer().get(pixels);
// Load the pixels into the BufferedImage, flipped vertically as OpenGL and
// Load the pixels into the NativeImage, flipped vertically as OpenGL and
// Minecraft disagree about which way is up
for (int y = 0; y < height; y++) {
img.setRGB(0, (height-1)-y, width, 1, pixels, y*width, width);
int off = y*width;
for (int x = 0; x < width; x++) img.setPixelRGBA(x, (height-1)-y, pixels[off++]);
}
return img;
}
Expand Down