Skip to content

Commit

Permalink
Fix one more issue with transparency plus optimize memory usage a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
minecraft8997 committed Dec 3, 2023
1 parent 8a34b3b commit 9cabbd0
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 15 deletions.
45 changes: 38 additions & 7 deletions core/src/ru/mclord/classic/Block.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.mclord.classic;

import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute;
import com.badlogic.gdx.utils.Disposable;
Expand Down Expand Up @@ -52,6 +54,7 @@ public boolean doIHaveThisPermission() {
private Model model;
private boolean alphaTestEnabled = true;
private float alphaTestValue = 0.5f;
/* package-private */ boolean hasSomeTransparency;

public Block(
short id,
Expand Down Expand Up @@ -122,16 +125,43 @@ public void initGraphics() {
TextureManager manager = TextureManager.getInstance();

model = Helper.constructBlock(0.5f,
manager.getTexture(frontTextureId),
manager.getTexture(backTextureId),
manager.getTexture(bottomTextureId),
manager.getTexture(topTextureId),
manager.getTexture(leftTextureId),
manager.getTexture(rightTextureId),
checkTransparency(manager.getTexture(frontTextureId)),
checkTransparency(manager.getTexture(backTextureId)),
checkTransparency(manager.getTexture(bottomTextureId)),
checkTransparency(manager.getTexture(topTextureId)),
checkTransparency(manager.getTexture(leftTextureId)),
checkTransparency(manager.getTexture(rightTextureId)),
(alphaTestEnabled ? FloatAttribute.createAlphaTest(alphaTestValue) : null)
);
}

private Texture checkTransparency(Texture texture) {
if (hasSomeTransparency) return texture;

boolean wasNotPrepared = false;
if (!texture.getTextureData().isPrepared()) {
texture.getTextureData().prepare();

wasNotPrepared = true;
}
Pixmap pixmap = texture.getTextureData().consumePixmap();
if (wasNotPrepared) {
TextureManager.getInstance().addTemporaryPixmap(pixmap);
}
for (int i = 0; i < pixmap.getWidth(); i++) {
for (int j = 0; j < pixmap.getHeight(); j++) {
byte alpha = (byte) (pixmap.getPixel(i, j) & 0xFF);
if (alpha != -1) {
hasSomeTransparency = true;

return texture;
}
}
}

return texture;
}

public final Model getModel() {
return model;
}
Expand Down Expand Up @@ -194,7 +224,8 @@ public boolean shouldBeRenderedAt(int x, int y, int z) {
neighbors[4] = level.getBlockDefAt(x, y + 1, z);
neighbors[5] = level.getBlockDefAt(x, y - 1, z);
for (Block neighbor : neighbors) {
if (neighbor.solidity != Solidity.SOLID || neighbor.slab) return true;
if (neighbor.solidity != Solidity.SOLID ||
neighbor.slab || neighbor.hasSomeTransparency) return true;
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion core/src/ru/mclord/classic/BlockManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public void registerBlock(Block block) {
throw new IllegalArgumentException(
"Specified block ID (" + block.id + ") is already registered");
}

REGISTERED_BLOCKS.put(block.id, block);

block.onBlockRegister();
}

@ShouldBeCalledBy(thread = "main")
Expand Down
3 changes: 2 additions & 1 deletion core/src/ru/mclord/classic/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public void initGraphics() {
for (int y = 0; y < level.sizeY; y++) {
for (int z = realZ; z < realZ + CHUNK_SIZE; z++) {
Block block = level.getBlockDefAt(x, y, z);
block.initGraphics();

if (!block.shouldBeRenderedAt(x, y, z)) continue;

block.initGraphics();
ModelInstance modelInstance = new ModelInstance(block.getModel(),
(new Matrix4()).translate(x, y, z), (String[]) null);
modelCache.add(modelInstance);
Expand Down
6 changes: 6 additions & 0 deletions core/src/ru/mclord/classic/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ public static Model constructBlock(
return model;
}

/* package-private */ static void disposeBlocks() {
for (Block block : BlockManager.getInstance().enumerateBlocksFast()) {
block.dispose();
}
}

// thanks to https://stackoverflow.com/a/18157551
public static float distanceSquared(Chunk chunk, float pointX, float pointZ) {
int realChunkX = Chunk.getX(chunk);
Expand Down
1 change: 1 addition & 0 deletions core/src/ru/mclord/classic/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void initGraphics() {
chunks.put(Pair.of(chunkX, chunkZ), chunk);
}
}
Helper.disposeBlocks();

graphicsInitialized = true;
}
Expand Down
8 changes: 3 additions & 5 deletions core/src/ru/mclord/classic/McLordClassic.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public enum GameStage {

public static final boolean DEBUG = true;
public static final String APP_NAME = "McLordClassic";
public static final String VERSION = "0.1.5";
public static final int VERSION_CODE = 5;
public static final String VERSION = "0.1.6";
public static final int VERSION_CODE = 6;

private static final McLordClassic INSTANCE = new McLordClassic();

Expand Down Expand Up @@ -297,9 +297,7 @@ public void dispose() {
PluginManager.getInstance().disablePlugins();

Helper.dispose(level); level = null;
for (Block block : BlockManager.getInstance().enumerateBlocksFast()) {
block.dispose();
}
Helper.disposeBlocks();
TextureManager.getInstance().dispose();

System.out.println("Goodbye!");
Expand Down
8 changes: 8 additions & 0 deletions core/src/ru/mclord/classic/TextureManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ public static int textureColorFrom(int bufferedImageColor) {
return bufferedImageColor;
}

public void addTemporaryPixmap(Pixmap pixmap) {
temporaryPixmaps.add(pixmap);
}

public void addTemporaryTexture(Texture texture) {
temporaryTextures.add(texture);
}

public void walk(
Texture[] textures,
List<Pixmap> temporaryPixmaps,
Expand Down
2 changes: 1 addition & 1 deletion desktop/src/ru/mclord/classic/DesktopLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void main(String[] arg) throws IOException {
config.useVsync(vsync);
config.disableAudio(!audio);
config.setHdpiMode(hdpiMode);
config.setTitle("McLordClassic");
config.setTitle("McLordClassic v" + McLordClassic.VERSION);
config.setWindowIcon();

new Lwjgl3Application(McLordClassic.game().linkProperties(props), config);
Expand Down

0 comments on commit 9cabbd0

Please sign in to comment.