Skip to content

Commit 4da33f5

Browse files
authored
Don't use BufferedImage (#800)
2 parents 5831d9e + 52bcd45 commit 4da33f5

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

Common/src/main/java/at/petrak/hexcasting/client/render/PatternTextureManager.java

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import net.minecraft.client.Minecraft;
55
import net.minecraft.client.renderer.texture.DynamicTexture;
66
import net.minecraft.resources.ResourceLocation;
7+
import net.minecraft.util.FastColor;
8+
import net.minecraft.util.Mth;
79
import net.minecraft.util.Tuple;
810
import net.minecraft.world.phys.Vec2;
911

10-
import java.awt.*;
11-
import java.awt.image.BufferedImage;
12+
import java.awt.geom.Line2D;
1213
import java.util.List;
1314
import java.util.*;
1415
import java.util.concurrent.*;
@@ -86,46 +87,38 @@ private static Map<String, ResourceLocation> registerTextures(String patTextureK
8687
}
8788

8889
private static NativeImage drawLines(List<Vec2> points, HexPatternPoints staticPoints, float unscaledLineWidth, int resPerUnit) {
89-
BufferedImage img = new BufferedImage((int)(staticPoints.fullWidth*resPerUnit), (int)(staticPoints.fullHeight*resPerUnit), BufferedImage.TYPE_INT_ARGB);
90-
Graphics2D g2d = img.createGraphics();
91-
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
92-
93-
g2d.setColor(new Color(0xFF_FFFFFF)); // set it to white so we can reuse the texture with different colors
94-
g2d.setStroke(new BasicStroke(unscaledLineWidth * resPerUnit, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
90+
NativeImage nativeImage = new NativeImage((int)(staticPoints.fullWidth*resPerUnit), (int)(staticPoints.fullHeight*resPerUnit), true);
9591
for (int i = 0; i < points.size() - 1; i++) {
9692
Tuple<Integer, Integer> pointFrom = getTextureCoordinates(points.get(i), staticPoints, resPerUnit);
9793
Tuple<Integer, Integer> pointTo = getTextureCoordinates(points.get(i+1), staticPoints, resPerUnit);
98-
g2d.drawLine(pointFrom.getA(), pointFrom.getB(), pointTo.getA(), pointTo.getB());
94+
drawLine(nativeImage, pointFrom.getA(), pointFrom.getB(), pointTo.getA(), pointTo.getB(), unscaledLineWidth * resPerUnit);
9995
}
100-
g2d.dispose();
101-
NativeImage nativeImage = new NativeImage(img.getWidth(), img.getHeight(), true);
102-
for (int y = 0; y < img.getHeight(); y++)
103-
for (int x = 0; x < img.getWidth(); x++)
104-
nativeImage.setPixelRGBA(x, y, img.getRGB(x, y));
10596
return nativeImage;
10697
}
10798

99+
private static void drawLine(NativeImage image, int x0, int y0, int x1, int y1, float width) {
100+
var line = new Line2D.Float(x0, y0, x1, y1);
101+
var bounds = line.getBounds();
102+
double halfWidth = width / 2;
103+
for (int x = (int) (bounds.x - width - 1); x < (int) (bounds.x + bounds.width + width + 1); x++) {
104+
for (int y = (int) (bounds.y - width - 1); y < (int) (bounds.y + bounds.height + width + 1); y++) {
105+
double dist = line.ptSegDist(x, y);
106+
int alpha = (int) (Mth.clamp(halfWidth - dist + 0.5, 0, 1) * 255);
107+
if (alpha > 0) {
108+
int oldAlpha = FastColor.ARGB32.alpha(image.getPixelRGBA(x, y));
109+
int newAlpha = Math.max(oldAlpha, alpha);
110+
image.setPixelRGBA(x, y, 0xFFFFFF | (newAlpha << 24));
111+
}
112+
}
113+
}
114+
}
115+
108116
private static Tuple<Integer, Integer> getTextureCoordinates(Vec2 point, HexPatternPoints staticPoints, int resPerUnit) {
109117
int x = (int) ( point.x * resPerUnit);
110118
int y = (int) ( point.y * resPerUnit);
111119
return new Tuple<>(x, y);
112120
}
113121

114-
// keeping this around just in case we ever decide to put the dots in the textures instead of dynamic
115-
private static void drawHexagon(Graphics2D g2d, int x, int y, int radius) {
116-
int fracOfCircle = 6;
117-
Polygon hexagon = new Polygon();
118-
119-
for (int i = 0; i < fracOfCircle; i++) {
120-
double theta = (i / (double) fracOfCircle) * Math.PI * 2;
121-
int hx = (int) (x + Math.cos(theta) * radius);
122-
int hy = (int) (y + Math.sin(theta) * radius);
123-
hexagon.addPoint(hx, hy);
124-
}
125-
126-
g2d.fill(hexagon);
127-
}
128-
129122
public static void repaint() {
130123
repaintIndex++;
131124
patternTexturesToAdd.clear();

0 commit comments

Comments
 (0)