Skip to content

Commit

Permalink
Fixed dynamic textures
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jul 26, 2024
1 parent 1aa0188 commit 47cb1d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public abstract class CachingRasterTileSet implements RasterTileSet {

public static final int CACHE_SIZE = 512;
public static final int CACHE_SIZE = 5120;
public static final int LOW_ZOOM = 2;

private final LinkedList<RasterTile> tileList; // Uses for ordered access when unloading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import org.joml.Matrix4f;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static net.smyler.smylib.Preconditions.checkArgument;

Expand All @@ -28,7 +31,9 @@ public class WrappedGuiGraphics implements UiDrawContext {
private final Scissor scissor = new Gl20Scissor();
private final GlContext glState = new Blaze3dGlContext();
private final TextureManager textureManager;
private int dynamicTextureLocationCounter = 0;
private static final AtomicInteger dynamicTextureLocationCounter = new AtomicInteger(0);

private final Map<Identifier, DynamicTexture> dynamicTextureCache = new HashMap<>();

public WrappedGuiGraphics(Minecraft minecraft, GuiGraphics vanillaGraphics) {
this.vanillaGraphics = vanillaGraphics;
Expand Down Expand Up @@ -120,23 +125,27 @@ public void drawTooltip(String text, double x, double y) {

@Override
public Identifier loadDynamicTexture(BufferedImage image) {
try (NativeImage nativeImage = new NativeImage(image.getWidth(), image.getHeight(), false)) {
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
int pixel = image.getRGB(x, y);
nativeImage.setPixelRGBA(x, y, pixel);
}
NativeImage nativeImage = new NativeImage(image.getWidth(), image.getHeight(), true);
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
int pixel = image.getRGB(x, y);
pixel = (pixel & 0xFF) << 16 | (pixel & 0xFF0000) >> 16 | (pixel & 0xFF00FF00);
nativeImage.setPixelRGBA(x, y, pixel);
}
ResourceLocation location = new ResourceLocation("smylib", "dynamic_texture/" + this.dynamicTextureLocationCounter++);
DynamicTexture texture = new DynamicTexture(nativeImage);
this.textureManager.register(location, texture);
return new Identifier(location.getNamespace(), location.getPath());
}
ResourceLocation location = new ResourceLocation("smylib", "dynamic_texture/" + dynamicTextureLocationCounter.getAndIncrement());
Identifier identifier = new Identifier(location.getNamespace(), location.getPath());
DynamicTexture texture = new DynamicTexture(nativeImage);
this.textureManager.register(location, texture);
this.dynamicTextureCache.put(identifier, texture);
return identifier;
}

@Override
public void unloadDynamicTexture(Identifier texture) {
this.textureManager.release(new ResourceLocation(texture.path, texture.namespace));
DynamicTexture removed = this.dynamicTextureCache.remove(texture);
removed.close();
}

private void drawMultiPointsGeometry(double z, Color color, double... points) {
Expand Down

0 comments on commit 47cb1d3

Please sign in to comment.