From 122eeaaf3a0709e2079640336b15f250cd9ee292 Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 23 Nov 2020 16:36:23 +0100 Subject: [PATCH] Fix bugs with previous commit --- .../bixilon/minosoft/render/GameWindow.java | 31 ++++++++---- .../render/blockModels/BlockModelLoader.java | 19 ++++---- .../blockModels/subBlocks/SubBlock.java | 2 +- .../minosoft/render/texture/InFaceUV.java | 3 +- .../render/texture/TextureLoader.java | 34 +++++++++----- util/.idea/workspace.xml | 47 ++++++++++++------- 6 files changed, 84 insertions(+), 52 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/render/GameWindow.java b/src/main/java/de/bixilon/minosoft/render/GameWindow.java index 1a4bb8e56..bb7d44fff 100644 --- a/src/main/java/de/bixilon/minosoft/render/GameWindow.java +++ b/src/main/java/de/bixilon/minosoft/render/GameWindow.java @@ -28,6 +28,7 @@ public class GameWindow { private static final Object waiter = new Object(); private static final LinkedBlockingQueue queue = new LinkedBlockingQueue<>(); private static Connection currentConnection; + private static boolean running; public static void prepare() { CountDownLatch latch = new CountDownLatch(1); @@ -46,12 +47,21 @@ public class GameWindow { } private static void mainLoop() { - try { - queue.take().run(); - } catch (Exception e) { - e.printStackTrace(); + while (!running) { + try { + queue.take().run(); + } catch (Exception e) { + e.printStackTrace(); + } } while (!glfwWindowShouldClose(openGLWindow.getWindowId())) { + if (queue.size() > 0) { + try { + queue.take().run(); + } catch (Exception e) { + e.printStackTrace(); + } + } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glfwPollEvents(); @@ -77,13 +87,12 @@ public class GameWindow { return openGLWindow; } - public static Connection getCurrentConnection() { - return currentConnection; - } - public static void setCurrentConnection(Connection currentConnection) { if (GameWindow.currentConnection == null) { - queue.add(openGLWindow::start); + queue.add(() -> { + running = true; + openGLWindow.start(); + }); } openGLWindow.setCurrentConnection(currentConnection); GameWindow.currentConnection = currentConnection; @@ -91,4 +100,8 @@ public class GameWindow { waiter.notifyAll(); } } + + public static void queue(Runnable runnable) { + queue.add(runnable); + } } diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java b/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java index 4ec374c3f..979780109 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/BlockModelLoader.java @@ -46,7 +46,7 @@ public class BlockModelLoader { TextureLoader textureLoader = new TextureLoader(getTextures(blockModels), tints); - applyTextures( blockModels, textureLoader); + applyTextures(blockModels, textureLoader); HashMap modelMap = loadBlocks(data, blockModels); return new Pair<>(modelMap, textureLoader.getTextureID()); } @@ -92,18 +92,15 @@ public class BlockModelLoader { } } - private static HashMap readTints(JsonObject json) { + private static HashMap readTints(JsonObject textures) { HashMap result = new HashMap<>(); - if (json.has("tinted_textures")) { - JsonObject textures = json.get("tinted_textures").getAsJsonObject(); - for (String textureName : textures.keySet()) { - ArrayFloatList colorValues = new ArrayFloatList(); - for (JsonElement colorValue : textures.get(textureName).getAsJsonArray()) { - colorValues.add(colorValue.getAsFloat()); - } - float[] color = colorValues.toArray(); - result.put(textureName, color); + for (String textureName : textures.keySet()) { + ArrayFloatList colorValues = new ArrayFloatList(); + for (JsonElement colorValue : textures.get(textureName).getAsJsonArray()) { + colorValues.add(colorValue.getAsFloat()); } + float[] color = colorValues.toArray(); + result.put(textureName, color); } return result; } diff --git a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java index 624518616..7b430c55c 100644 --- a/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java +++ b/src/main/java/de/bixilon/minosoft/render/blockModels/subBlocks/SubBlock.java @@ -129,7 +129,7 @@ public class SubBlock { } private ArrayFloatList prepareFace(FaceOrientation faceDirection, BlockPosition position) { - if (! uv.get(faceDirection).exists()) { + if (!uv.containsKey(faceDirection) || !uv.get(faceDirection).exists()) { return null; } ArrayFloatList result = new ArrayFloatList(); diff --git a/src/main/java/de/bixilon/minosoft/render/texture/InFaceUV.java b/src/main/java/de/bixilon/minosoft/render/texture/InFaceUV.java index 3c2d3084a..2bcaec89f 100644 --- a/src/main/java/de/bixilon/minosoft/render/texture/InFaceUV.java +++ b/src/main/java/de/bixilon/minosoft/render/texture/InFaceUV.java @@ -14,7 +14,6 @@ package de.bixilon.minosoft.render.texture; import com.google.gson.JsonArray; -import de.bixilon.minosoft.render.blockModels.BlockModelLoader; import de.bixilon.minosoft.render.blockModels.Face.RenderConstants; import org.apache.commons.collections.primitives.ArrayFloatList; @@ -73,6 +72,6 @@ public class InFaceUV { } public boolean exists() { - return realU1 == -1; + return realU1 >= 0; } } diff --git a/src/main/java/de/bixilon/minosoft/render/texture/TextureLoader.java b/src/main/java/de/bixilon/minosoft/render/texture/TextureLoader.java index 315c6d8b1..a61d4ddb4 100644 --- a/src/main/java/de/bixilon/minosoft/render/texture/TextureLoader.java +++ b/src/main/java/de/bixilon/minosoft/render/texture/TextureLoader.java @@ -16,6 +16,7 @@ package de.bixilon.minosoft.render.texture; import de.bixilon.minosoft.config.StaticConfiguration; import de.bixilon.minosoft.data.assets.AssetsManager; import de.bixilon.minosoft.logging.Log; +import de.bixilon.minosoft.render.GameWindow; import de.bixilon.minosoft.render.blockModels.Face.RenderConstants; import de.matthiasmann.twl.utils.PNGDecoder; @@ -29,6 +30,7 @@ import java.nio.ByteBuffer; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.concurrent.CountDownLatch; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL30.glGenerateMipmap; @@ -39,8 +41,10 @@ public class TextureLoader { private int textureID; private float step; private int totalTextures = 0; + private final CountDownLatch countDownLatch; public TextureLoader(HashSet textures, HashMap tints) { + countDownLatch = new CountDownLatch(1); textureCoordinates = new HashMap<>(); loadTextures(textures, tints); combineTextures(); @@ -48,7 +52,7 @@ public class TextureLoader { PNGDecoder decoder = new PNGDecoder(new FileInputStream(StaticConfiguration.HOME_DIRECTORY + "assets/allTextures.png")); ByteBuffer buf = ByteBuffer.allocateDirect(decoder.getWidth() * decoder.getHeight() * 4); decoder.decode(buf, decoder.getWidth() * 4, PNGDecoder.Format.RGBA); - textureID = bindTexture(buf, decoder.getWidth(), decoder.getHeight()); + bindTexture(buf, decoder.getWidth(), decoder.getHeight()); } catch (IOException e) { e.printStackTrace(); System.exit(5); @@ -116,17 +120,25 @@ public class TextureLoader { step = (float) 1 / (float) imageLength * RenderConstants.TEXTURE_PACK_RESOLUTION; } - private int bindTexture(ByteBuffer buf, int width, int height) { + private void bindTexture(ByteBuffer buf, int width, int height) { buf.flip(); - int textureID = glGenTextures(); - glBindTexture(GL_TEXTURE_2D, textureID); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); - glGenerateMipmap(GL_TEXTURE_2D); - //disable smoothing out of textures - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - return textureID; + GameWindow.queue(() -> { + int textureID = glGenTextures(); + glBindTexture(GL_TEXTURE_2D, textureID); + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); + glGenerateMipmap(GL_TEXTURE_2D); + //disable smoothing out of textures + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + this.textureID = textureID; + countDownLatch.countDown(); + }); + try { + countDownLatch.await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } } public float getTexture(String textureName) { diff --git a/util/.idea/workspace.xml b/util/.idea/workspace.xml index 4a8f58476..d0b051228 100644 --- a/util/.idea/workspace.xml +++ b/util/.idea/workspace.xml @@ -1,17 +1,26 @@ + + + + + + + + + + + + + + + + +