Skip to content

Commit

Permalink
Compatibility and warning fixes (#41)
Browse files Browse the repository at this point in the history
* Add retries with larger atlases to the stitcher code

Closes #31

* Add a dummy Controllers class

Closes #34

* Fix unsupported NestHost in the shaded jakarta jar.
  • Loading branch information
eigenraven authored Mar 14, 2023
1 parent 47431f0 commit 999b8c1
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public String getZipEntryName(ZipEntry ze) {
return null;
}
if (name.contains("module-info.class") || name.startsWith("META-INF/versions/")
|| name.contains("org/openjdk/nashorn")) {
|| name.contains("org/openjdk/nashorn")
|| name.contains("jakarta/servlet/")) {
// Triggers the continue in the loop
return "__MACOSX_ignoreme";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.renderer.texture.Stitcher;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;

import org.apache.commons.lang3.tuple.Pair;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
Expand Down Expand Up @@ -36,9 +37,9 @@ public void doStitch() {
.toArray(new Stitcher.Holder[this.setStitchHolders.size()]);
Arrays.sort(aholder);

int size = StbStitcher.packRects(aholder);
this.currentWidth = size;
this.currentHeight = size;
Pair<Integer, Integer> size = StbStitcher.packRects(aholder);
this.currentWidth = size.getLeft();
this.currentHeight = size.getRight();
}

/**
Expand Down
84 changes: 52 additions & 32 deletions src/main/java/me/eigenraven/lwjgl3ify/textures/StbStitcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.renderer.texture.Stitcher;
import net.minecraft.util.MathHelper;

import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.stb.STBRPContext;
import org.lwjgl.stb.STBRPNode;
import org.lwjgl.stb.STBRPRect;
Expand All @@ -19,7 +20,7 @@
public class StbStitcher {

// Returns size
public static int packRects(Stitcher.Holder[] holders) {
public static Pair<Integer, Integer> packRects(Stitcher.Holder[] holders) {
ProgressManager.ProgressBar bar = ProgressManager.push("Stitch setup", (holders.length + 99) / 100);
int holderSize = holders.length;

Expand All @@ -29,6 +30,7 @@ public static int packRects(Stitcher.Holder[] holders) {
// Initialize the rectangles that we'll be using in the calculation
// While that's happening, sum up the area needed to fit all of the images
int sqSize = 0;
int maxW = 16, maxH = 16;
for (int j = 0; j < holderSize; ++j) {
Stitcher.Holder holder = holders[j];
if ((j % 100) == 0 && bar.getStep() < bar.getSteps()) {
Expand All @@ -41,48 +43,66 @@ public static int packRects(Stitcher.Holder[] holders) {
// The ID here is just the array index, for easy lookup later
rectBuf.get(j).set(j, width, height, 0, 0, false);

sqSize += (width * height);
sqSize += width * height;
maxW = Math.max(maxW, width);
maxH = Math.max(maxH, height);
}

int size = MathHelper.roundUpToPowerOfTwo((int) Math.sqrt(sqSize));

// TODO: if sqSize < (size * size) / 2, then we can use size / 2 for the height to save VRAM
maxW = MathHelper.roundUpToPowerOfTwo(maxW);
maxH = MathHelper.roundUpToPowerOfTwo(maxH);
final int guessedSqSide = MathHelper.roundUpToPowerOfTwo((int) Math.ceil(Math.sqrt(sqSize)));
int atlasWidth = Math.max(guessedSqSide, maxW * 2);
int atlasHeight = Math.max(guessedSqSide, maxH * 2);

// Internal node structure needed for STB
try (STBRPNode.Buffer nodes = STBRPNode.malloc(size + 1)) {
// Initialize the rect packer
STBRectPack.stbrp_init_target(ctx, size, size, nodes);

// Perform rectangle packing
STBRectPack.stbrp_pack_rects(ctx, rectBuf);

for (STBRPRect rect : rectBuf) {
Stitcher.Holder holder = holders[rect.id()];

// Ensure that everything is properly packed!
if (!rect.was_packed()) {
throw new StitcherException(
holder,
"Could not fit " + holder.getAtlasSprite()
.getIconName() + " into " + size + "x" + size + " atlas!");
boolean doubleX = true;
for (int retries = 0; retries < 10; retries++) {
try (STBRPNode.Buffer nodes = STBRPNode.malloc(atlasWidth + 32)) {
// Initialize the rect packer
STBRectPack.stbrp_init_target(ctx, atlasWidth, atlasHeight, nodes);

// Perform rectangle packing
if (STBRectPack.stbrp_pack_rects(ctx, rectBuf) == 0) {
// Not everything was packed
if (doubleX) {
atlasWidth *= 2;
} else {
atlasHeight *= 2;
}
doubleX = !doubleX;
continue;
}

// Initialize the sprite now with the position and size that we've calculated so far
for (STBRPRect rect : rectBuf) {
Stitcher.Holder holder = holders[rect.id()];

// texture should not be rotated, so use false
holder.getAtlasSprite().initSprite(size, size, rect.x(), rect.y(), false);
}
// Ensure that everything is properly packed!
if (!rect.was_packed()) {
throw new StitcherException(
holder,
"Could not fit " + holder.getAtlasSprite()
.getIconName() + " into " + atlasWidth + "x" + atlasHeight + " atlas!");
}

// Safeguard in case our calculation was off
while (bar.getStep() < bar.getSteps()) {
Lwjgl3ify.LOG.warn("Progressbar calculation was off");
bar.step("noop");
}
// Initialize the sprite now with the position and size that we've calculated so far

// texture should not be rotated, so use false
holder.getAtlasSprite().initSprite(atlasWidth, atlasHeight, rect.x(), rect.y(), false);
}

// Safeguard in case our calculation was off
while (bar.getStep() < bar.getSteps()) {
Lwjgl3ify.LOG.warn("Progressbar calculation was off");
bar.step("noop");
}

ProgressManager.pop(bar);
ProgressManager.pop(bar);

return size;
return Pair.of(atlasWidth, atlasHeight);
}
}
throw new IllegalStateException(
"Could not fit all sprites into an atlas of size " + atlasWidth + "x" + atlasHeight);
}
}
}
72 changes: 72 additions & 0 deletions src/main/java/org/lwjglx/input/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.lwjglx.input;

public interface Controller {

public abstract int getAxisCount();

public abstract java.lang.String getAxisName(int arg0);

public abstract float getAxisValue(int arg0);

public abstract int getButtonCount();

public abstract java.lang.String getButtonName(int arg0);

public abstract float getDeadZone(int arg0);

public abstract int getIndex();

public abstract java.lang.String getName();

public abstract float getPovX();

public abstract float getPovY();

public abstract float getRXAxisDeadZone();

public abstract float getRXAxisValue();

public abstract float getRYAxisDeadZone();

public abstract float getRYAxisValue();

public abstract float getRZAxisDeadZone();

public abstract float getRZAxisValue();

public abstract int getRumblerCount();

public abstract java.lang.String getRumblerName(int arg0);

public abstract float getXAxisDeadZone();

public abstract float getXAxisValue();

public abstract float getYAxisDeadZone();

public abstract float getYAxisValue();

public abstract float getZAxisDeadZone();

public abstract float getZAxisValue();

public abstract boolean isButtonPressed(int arg0);

public abstract void poll();

public abstract void setDeadZone(int arg0, float arg1);

public abstract void setRXAxisDeadZone(float arg0);

public abstract void setRYAxisDeadZone(float arg0);

public abstract void setRZAxisDeadZone(float arg0);

public abstract void setRumblerStrength(int arg0, float arg1);

public abstract void setXAxisDeadZone(float arg0);

public abstract void setYAxisDeadZone(float arg0);

public abstract void setZAxisDeadZone(float arg0);
}
101 changes: 101 additions & 0 deletions src/main/java/org/lwjglx/input/Controllers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.lwjglx.input;

// TODO: This is a dummy implementation
public class Controllers {

public static void clearEvents() {
// no-op
}

public static void create() {
// no-op
}

public static void destroy() {
// no-op
}

public static org.lwjgl.input.Controller getController(int arg0) {
// no-op
return null;
}

public static int getControllerCount() {
// no-op
return 0;
}

public static boolean getEventButtonState() {
// no-op
return false;
}

public static int getEventControlIndex() {
// no-op
return 0;
}

public static long getEventNanoseconds() {
// no-op
return 1;
}

public static org.lwjgl.input.Controller getEventSource() {
// no-op
return null;
}

public static float getEventXAxisValue() {
// no-op
return 0.0f;
}

public static float getEventYAxisValue() {
// no-op
return 0.0f;
}

public static boolean isCreated() {
// no-op
return true;
}

public static boolean isEventAxis() {
// no-op
return false;
}

public static boolean isEventButton() {
// no-op
return false;
}

public static boolean isEventPovX() {
// no-op
return false;
}

public static boolean isEventPovY() {
// no-op
return false;
}

public static boolean isEventXAxis() {
// no-op
return false;
}

public static boolean isEventYAxis() {
// no-op
return false;
}

public static boolean next() {
// no-op
return false;
}

public static void poll() {
// no-op
}
}

0 comments on commit 999b8c1

Please sign in to comment.