Skip to content

Commit

Permalink
Support non 16x texture packs
Browse files Browse the repository at this point in the history
  • Loading branch information
minecraft8997 committed Nov 21, 2023
1 parent e3cb228 commit e4b15b5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
4 changes: 2 additions & 2 deletions core/src/ru/mclord/classic/LoadingScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public void render(float delta) {

batch.begin();
font.draw(batch, (status != null ? status : "Loading"), 32, height - 32);
for (int i = 0; i < TextureManager.TEXTURE_COUNT; i++) {
for (int i = 0; i < TextureManager.getInstance().getTextureCount(); i++) {
Texture blockTexture = TextureManager.getInstance().getTexture(i);

int x = i * TextureManager.TEXTURE_SIZE;
int x = i * TextureManager.getInstance().getTextureSize();
if (x >= width) break;
batch.draw(blockTexture, x, PROGRESS_BAR_HEIGHT);
}
Expand Down
69 changes: 37 additions & 32 deletions core/src/ru/mclord/classic/TextureManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,15 @@ default boolean shouldWalk(int i) {
}
}

public static final int TEXTURE_SIZE = 16;
public static final int IMAGE_WIDTH = 256;
public static final int IMAGE_HEIGHT = 512;
public static final int TEXTURE_COUNT;

static {
//noinspection ConstantValue
if (IMAGE_WIDTH % TEXTURE_SIZE != 0 || IMAGE_HEIGHT % TEXTURE_SIZE != 0) {
throw new IllegalStateException("Invalid " +
"texture size, image width or image height");
}

TEXTURE_COUNT = IMAGE_WIDTH * IMAGE_HEIGHT / (TEXTURE_SIZE * TEXTURE_SIZE);
}

public static final String DEFAULT_TEXTURE_PACK =
"https://static.classicube.net/default.zip";

private static final TextureManager INSTANCE = new TextureManager();
private final Texture[] textures = new Texture[TEXTURE_COUNT];
private Texture[] textures;
private final boolean searchForSkybox;
private final Texture[] skyboxTextures = new Texture[6];
/* package-private */ boolean skyboxPresented;
/* package-private */ int textureSize;
private final List<Pixmap> temporaryPixmaps = new ArrayList<>();
private final List<Texture> temporaryTextures;
private Texture emptyTexture;
Expand Down Expand Up @@ -100,23 +86,27 @@ public void load(String path, boolean allowNet, boolean allowFileSystem) {

int width = image.getWidth();
int height = image.getHeight();
/*
if (image.getWidth() != IMAGE_WIDTH || image.getHeight() != IMAGE_HEIGHT) {
throw new IllegalArgumentException("The " +
"image must be " + IMAGE_WIDTH + "x" + IMAGE_HEIGHT);

int textureSize;
int textureCount = -1;
if (height == width) {
textureCount = 256;
} else if (height == width * 2) {
textureCount = 512;
} else {
illegalTerrainDimensions();
}
*/
Pixmap emptyPixmap = new Pixmap(TEXTURE_SIZE, TEXTURE_SIZE, Pixmap.Format.RGBA8888);
//for (int i = 0; i < TEXTURE_SIZE; i++) {
// for (int j = 0; j < TEXTURE_SIZE; j++) {
//emptyPixmap.drawPixel(i, j, 0xFF000009);
// }
//}
if (width % 16 != 0) illegalTerrainDimensions();
textureSize = width / 16;

textures = new Texture[textureCount];

Pixmap emptyPixmap = new Pixmap(textureSize, textureSize, Pixmap.Format.RGBA8888);
emptyTexture = new Texture(emptyPixmap);
temporaryPixmaps.add(emptyPixmap);

walk(textures, temporaryPixmaps, textures.length, TEXTURE_SIZE,
TEXTURE_SIZE, 16, (pixmap, xOffset, yOffset, x, y) -> {
walk(textures, temporaryPixmaps, textures.length, textureSize,
textureSize, 16, (pixmap, xOffset, yOffset, x, y) -> {

int color;
int realX = xOffset + x;
Expand Down Expand Up @@ -164,6 +154,10 @@ public boolean shouldWalk(int i) {
}
}

private static void illegalTerrainDimensions() {
throw new RuntimeException("Illegal terrain.png dimensions");
}

/*
* Turns a BufferedImage pixel color into a Texture color by
* moving the alpha value (the highest byte) to the lowest byte. For example
Expand Down Expand Up @@ -247,7 +241,6 @@ public static Object createInputStreams(
byte[] bytes = new byte[(int) entry.getSize()];
wrapper.readFully(bytes);
inputStreams.put(name, new ByteArrayInputStream(bytes));
System.out.println("Added " + name);
if (inputStreams.size() == objects.length) break;
}
}
Expand Down Expand Up @@ -279,6 +272,14 @@ public Texture getEmptyTexture() {
return emptyTexture;
}

public int getTextureCount() {
return textures.length;
}

public int getTextureSize() {
return textureSize;
}

public Texture rotate90Texture(Texture texture, boolean clockwise) {
if (texture == emptyTexture) return texture;

Expand Down Expand Up @@ -343,8 +344,12 @@ public void rotate90Pixmap(Pixmap pixmap, boolean clockwise) {
@ShouldBeCalledBy(thread = "main")
public void dispose() {
Helper.dispose(emptyTexture);
for (Texture texture : textures) {
Helper.dispose(texture);
if (textures != null) {
for (Texture texture : textures) {
Helper.dispose(texture);
}

textures = null;
}
for (Texture texture : skyboxTextures) {
Helper.dispose(texture);
Expand Down

0 comments on commit e4b15b5

Please sign in to comment.