TextureLoading: no longer save a temporary file

This commit is contained in:
Lukas 2020-12-08 18:12:39 +01:00
parent 386dc882a0
commit 9fcbe27046
2 changed files with 14 additions and 28 deletions

View File

@ -95,11 +95,6 @@
<classifier>natives-windows</classifier> <classifier>natives-windows</classifier>
<version>${lwjgl.version}</version> <version>${lwjgl.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.l33tlabs.twl</groupId>
<artifactId>pngdecoder</artifactId>
<version>1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.lwjgl</groupId> <groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId> <artifactId>lwjgl-glfw</artifactId>

View File

@ -13,18 +13,15 @@
package de.bixilon.minosoft.render.texture; package de.bixilon.minosoft.render.texture;
import de.bixilon.minosoft.config.StaticConfiguration;
import de.bixilon.minosoft.data.assets.AssetsManager; import de.bixilon.minosoft.data.assets.AssetsManager;
import de.bixilon.minosoft.logging.Log; import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.render.GameWindow; import de.bixilon.minosoft.render.GameWindow;
import de.bixilon.minosoft.render.blockModels.Face.RenderConstants; import de.bixilon.minosoft.render.blockModels.Face.RenderConstants;
import de.matthiasmann.twl.utils.PNGDecoder;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.awt.image.DataBufferInt;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.HashMap; import java.util.HashMap;
@ -42,21 +39,21 @@ public class TextureLoader {
private int textureID; private int textureID;
private float step; private float step;
private int totalTextures = 0; private int totalTextures = 0;
// ARGB -> RGBA
public TextureLoader(HashSet<String> textures, HashMap<String, float[]> tints) { public TextureLoader(HashSet<String> textures, HashMap<String, float[]> tints) {
countDownLatch = new CountDownLatch(1); countDownLatch = new CountDownLatch(1);
textureCoordinates = new HashMap<>(); textureCoordinates = new HashMap<>();
loadTextures(textures, tints); loadTextures(textures, tints);
combineTextures(); BufferedImage image = combineTextures();
try { ByteBuffer buf = ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * 4);
PNGDecoder decoder = new PNGDecoder(new FileInputStream(StaticConfiguration.HOME_DIRECTORY + "assets/allTextures.png")); int[] imageData = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
ByteBuffer buf = ByteBuffer.allocateDirect(decoder.getWidth() * decoder.getHeight() * 4); for (int data: imageData) {
decoder.decode(buf, decoder.getWidth() * 4, PNGDecoder.Format.RGBA); buf.put((byte) (( data >> 16 ) & 0xFF)); // R
bindTexture(buf, decoder.getWidth(), decoder.getHeight()); buf.put((byte) (( data >> 8 ) & 0xFF)); // G
} catch (IOException e) { buf.put((byte) (( data ) & 0xFF)); // B
e.printStackTrace(); buf.put((byte) (( data >> 24 ) & 0xFF)); // A
System.exit(5);
} }
bindTexture(buf, image.getWidth(), image.getHeight());
} }
private static void tintImage(BufferedImage image, float[] tintColor) { private static void tintImage(BufferedImage image, float[] tintColor) {
@ -96,11 +93,11 @@ public class TextureLoader {
images = modTextureMap; images = modTextureMap;
} }
private void combineTextures() { private BufferedImage combineTextures() {
// converts all single textures into a very wide image. Improves performance in opengl // converts all single textures into a very wide image. Improves performance in opengl
// TEXTURE_PACK_RESxTEXTURE_PACK_RES textures only // TEXTURE_PACK_RESxTEXTURE_PACK_RES textures only
int imageLength = Integer.highestOneBit(totalTextures * RenderConstants.TEXTURE_PACK_RESOLUTION) * 2; int imageLength = Integer.highestOneBit(totalTextures * RenderConstants.TEXTURE_PACK_RESOLUTION) * 2;
BufferedImage totalImage = new BufferedImage(imageLength, RenderConstants.TEXTURE_PACK_RESOLUTION, BufferedImage.TYPE_4BYTE_ABGR); BufferedImage totalImage = new BufferedImage(imageLength, RenderConstants.TEXTURE_PACK_RESOLUTION, BufferedImage.TYPE_INT_ARGB);
int currentPos = 0; int currentPos = 0;
for (Map.Entry<String, BufferedImage> texture : images.entrySet()) { for (Map.Entry<String, BufferedImage> texture : images.entrySet()) {
@ -112,14 +109,8 @@ public class TextureLoader {
} }
textureCoordinates.put(texture.getKey(), currentPos++); textureCoordinates.put(texture.getKey(), currentPos++);
} }
try {
File outputFile = new File(StaticConfiguration.HOME_DIRECTORY + "assets/allTextures.png");
ImageIO.write(totalImage, "png", outputFile);
} catch (IOException e) {
e.printStackTrace();
}
step = (float) 1 / (float) imageLength * RenderConstants.TEXTURE_PACK_RESOLUTION; step = (float) 1 / (float) imageLength * RenderConstants.TEXTURE_PACK_RESOLUTION;
return totalImage;
} }
private void bindTexture(ByteBuffer buf, int width, int height) { private void bindTexture(ByteBuffer buf, int width, int height) {