Skip to content

Commit

Permalink
Merge pull request #31 from UselessBullets/Extended-Texturespace
Browse files Browse the repository at this point in the history
Experimental Expanded Texturespace
  • Loading branch information
MartinSVK12 authored Nov 6, 2023
2 parents d773894 + 02ac170 commit db3227d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/main/java/turniplabs/halplibe/HalpLibe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;
import net.minecraft.core.Global;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turniplabs.halplibe.helper.AchievementHelper;
import turniplabs.halplibe.helper.NetworkHelper;
import turniplabs.halplibe.util.TomlConfigHandler;
import turniplabs.halplibe.util.achievements.AchievementPage;
import turniplabs.halplibe.util.achievements.VanillaAchievementsPage;
import turniplabs.halplibe.util.network.PacketExtendedMobSpawn;
import turniplabs.halplibe.util.toml.Toml;

public class HalpLibe implements ModInitializer, PreLaunchEntrypoint {
public static final String MOD_ID = "halplibe";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final TomlConfigHandler CONFIG;
static {
// this is here to possibly fix some class loading issues but might not work anyway, delete if it causes even more problems
Toml toml = new Toml();
toml.addCategory("Experimental");
toml.addEntry("Experimental.AtlasWidth", "Dynamically resized the Terrain and Item atlases, value must be an integer greater than or equal to 32",32);

CONFIG = new TomlConfigHandler(MOD_ID, toml);

Global.TEXTURE_ATLAS_WIDTH_TILES = Math.max(32, CONFIG.getInt("Experimental.AtlasWidth"));
// Initialize Block and Item static fields
try {
Class.forName("net.minecraft.core.block.Block");
Class.forName("net.minecraft.core.item.Item");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/turniplabs/halplibe/helper/BlockCoords.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package turniplabs.halplibe.helper;

import net.minecraft.core.Global;
import turniplabs.halplibe.HalpLibe;

public class BlockCoords {
Expand All @@ -12,9 +13,9 @@ public static int[] nextCoords() {
case 0: {
int x = lastX;
int y = lastY;
if (++lastX > 31) {
if (++lastX > Global.TEXTURE_ATLAS_WIDTH_TILES-1) {
lastX = 22;
if (++lastY > 31) {
if (++lastY > Global.TEXTURE_ATLAS_WIDTH_TILES-1) {
area = 1;
lastX = 7;
lastY = 16;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/turniplabs/halplibe/helper/ItemCoords.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package turniplabs.halplibe.helper;

import net.minecraft.core.Global;
import turniplabs.halplibe.HalpLibe;

public class ItemCoords {
Expand All @@ -12,9 +13,9 @@ public static int[] nextCoords() {
case 0: {
int x = lastX;
int y = lastY;
if (++lastX > 31) {
if (++lastX > Global.TEXTURE_ATLAS_WIDTH_TILES-1) {
lastX = 16;
if (++lastY > 31) {
if (++lastY > Global.TEXTURE_ATLAS_WIDTH_TILES-1) {
area = 1;
lastX = 0;
lastY = 16;
Expand All @@ -27,7 +28,7 @@ public static int[] nextCoords() {
int y = lastY;
if (++lastX > 15) {
lastX = 0;
if (++lastY > 31) {
if (++lastY > Global.TEXTURE_ATLAS_WIDTH_TILES-1) {
area = 2;
HalpLibe.LOGGER.info("Reached the end of item texture space!");
}
Expand All @@ -36,7 +37,7 @@ public static int[] nextCoords() {
}
default:
HalpLibe.LOGGER.info("No more item texture spaces are available!");
return new int[]{15, 31};
return new int[]{15, Global.TEXTURE_ATLAS_WIDTH_TILES-1};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package turniplabs.halplibe.mixin.mixins;

import net.minecraft.client.Minecraft;
import net.minecraft.client.option.enums.MipmapType;
import net.minecraft.client.render.RenderEngine;
import net.minecraft.client.render.dynamictexture.DynamicTexture;
import net.minecraft.core.Global;
import net.minecraft.core.util.helper.Buffer;
import org.lwjgl.opengl.GL11;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -12,12 +16,19 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import turniplabs.halplibe.helper.TextureHelper;

import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.util.List;

@Mixin(value = RenderEngine.class, remap = false)
public abstract class RenderEngineMixin {
@Shadow
private List<DynamicTexture> dynamicTextures;
@Shadow protected abstract void generateMipmaps(ByteBuffer buffer, BufferedImage image, int levels, boolean smooth);

@Shadow @Final public Minecraft minecraft;
@Shadow private boolean clampTexture;
@Shadow private boolean blurTexture;
@Unique
private final RenderEngine thisAsRenderEngine = (RenderEngine)(Object)this;
@Inject(method = "initDynamicTextures", at = @At("TAIL"))
Expand All @@ -31,4 +42,51 @@ private void initDynamicTextures(CallbackInfo ci) {
}
TextureHelper.textureHandlers.forEach((handler) -> dynamicTextures.add(handler.newHandler(mc)));
}
@Inject(method = "setupTexture(Ljava/awt/image/BufferedImage;IZ)V", at = @At("HEAD"), cancellable = true)
private void adjustAtlasSize(BufferedImage img, int id, boolean mipmap, CallbackInfo ci){
// boolean doResize = false;
// for (String key : textureMap.keySet()) {
// if (key.equals("/terrain.png") || key.equals("/gui/items.png")) {
// doResize = textureMap.get(key) == id;
// if (doResize) {
// break;
// }
// }
// }
if (mipmap){ // TODO Replace with a way to directly detect if a texture is Terrain.png or Items.png
BufferedImage resizedAtlas = new BufferedImage((img.getWidth()/32) * Global.TEXTURE_ATLAS_WIDTH_TILES, (img.getHeight()/32) * Global.TEXTURE_ATLAS_WIDTH_TILES, img.getType());
if (img.getWidth() == resizedAtlas.getWidth() && img.getHeight() == resizedAtlas.getHeight()){
return; // Don't scale and transfer image if they're the same size
}
for (int x = 0; x < img.getWidth(); x++) {
for (int y = 0; y < img.getHeight(); y++) {
resizedAtlas.setRGB(x, y, img.getRGB(x, y));
}
}

GL11.glBindTexture(3553, id);
if (this.blurTexture) {
GL11.glTexParameteri(3553, 10241, 9729);
GL11.glTexParameteri(3553, 10240, 9729);
} else {
GL11.glTexParameteri(3553, 10241, 9728);
GL11.glTexParameteri(3553, 10240, 9728);
}
if (this.clampTexture) {
GL11.glTexParameteri(3553, 10242, 10496);
GL11.glTexParameteri(3553, 10243, 10496);
} else {
GL11.glTexParameteri(3553, 10242, 10497);
GL11.glTexParameteri(3553, 10243, 10497);
}
int w = resizedAtlas.getWidth();
int h = resizedAtlas.getHeight();
Buffer.put(resizedAtlas);
GL11.glTexImage2D(3553, 0, 6408, w, h, 0, 6408, 5121, Buffer.buffer);
if (mipmap) {
this.generateMipmaps(Buffer.buffer, resizedAtlas, this.minecraft.gameSettings.mipmapLevels.value, this.minecraft.gameSettings.mipmapType.value == MipmapType.SMOOTH);
}
ci.cancel();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/turniplabs/halplibe/util/TextureHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TextureHandler(String textureName, String animationSource, int textureInd
throw new RuntimeException("Invalid Height for animation! " + animationSource);
} else {
this.frameCount = image.getHeight() / image.getWidth();
System.out.println(textureName + " frame Count: " + this.frameCount);
System.out.println(animationSource + " frame Count: " + this.frameCount);
this.frames = new byte[(int) (resolution * resolution * 4 * this.frameCount * scale * scale)];

for (int frame = 0; frame < this.frameCount; ++frame) {
Expand Down

0 comments on commit db3227d

Please sign in to comment.