From 7b66d458275c22b8205f6401077448edb3c3a481 Mon Sep 17 00:00:00 2001 From: hYdos Date: Sat, 29 Feb 2020 08:58:40 +1000 Subject: [PATCH] git --- .../litecraft/world/DynamicChunkLoader.java | 2 ++ .../halotroop/litecraft/world/World.java | 8 ++---- .../hydos/ginger/engine/api/Ginger.java | 6 ++++- .../hydos/multiThreading/GingerThread.java | 3 ++- .../hydos/multiThreading/GingerThreading.java | 25 ++++++++++++++++--- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/github/halotroop/litecraft/world/DynamicChunkLoader.java b/src/main/java/com/github/halotroop/litecraft/world/DynamicChunkLoader.java index 73386fe..3ac651a 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/DynamicChunkLoader.java +++ b/src/main/java/com/github/halotroop/litecraft/world/DynamicChunkLoader.java @@ -22,6 +22,7 @@ public class DynamicChunkLoader extends GingerThread{ @Override public void run() { + started = true; List toKeep = new ArrayList<>(); // loop over rendered area, adding chunks that are needed for (int x = chunkX - this.world.renderBound; x < chunkX + this.world.renderBound; x++) @@ -50,6 +51,7 @@ public class DynamicChunkLoader extends GingerThread{ if (!alreadyRendering) this.world.chunks.put(this.world.posHash(chunk.chunkX, chunk.chunkY, chunk.chunkZ), chunk); }); + this.finished = true; } } diff --git a/src/main/java/com/github/halotroop/litecraft/world/World.java b/src/main/java/com/github/halotroop/litecraft/world/World.java index 0c8c9da..2620680 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/World.java +++ b/src/main/java/com/github/halotroop/litecraft/world/World.java @@ -10,6 +10,7 @@ import com.github.halotroop.litecraft.types.block.*; import com.github.halotroop.litecraft.world.block.BlockRenderer; import com.github.halotroop.litecraft.world.dimension.Dimension; import com.github.halotroop.litecraft.world.gen.*; +import com.github.hydos.ginger.engine.api.Ginger; import com.github.hydos.ginger.engine.elements.objects.Player; import com.github.hydos.ginger.engine.obj.ModelLoader; import com.github.hydos.ginger.engine.render.models.TexturedModel; @@ -185,12 +186,7 @@ public class World implements BlockAccess, WorldGenConstants public void updateLoadedChunks(int chunkX, int chunkY, int chunkZ) { - if(!chunkLoader.isAlive()) { - chunkLoader.chunkX = chunkX; - chunkLoader.chunkY = chunkY; - chunkLoader.chunkZ = chunkZ; - chunkLoader.start(); - } + Ginger.getInstance().threading.registerChunkThreadToWaitlist(new DynamicChunkLoader(chunkX, chunkY, chunkZ, this)); } private static final class GenerationWorld implements BlockAccess, WorldGenConstants diff --git a/src/main/java/com/github/hydos/ginger/engine/api/Ginger.java b/src/main/java/com/github/hydos/ginger/engine/api/Ginger.java index 42156c6..7c0fab8 100644 --- a/src/main/java/com/github/hydos/ginger/engine/api/Ginger.java +++ b/src/main/java/com/github/hydos/ginger/engine/api/Ginger.java @@ -17,6 +17,7 @@ import com.github.hydos.ginger.engine.render.MasterRenderer; import com.github.hydos.ginger.engine.render.tools.MousePicker; import com.github.hydos.ginger.engine.screen.Screen; import com.github.hydos.ginger.engine.utils.Loader; +import com.github.hydos.multiThreading.GingerThreading; public class Ginger { @@ -25,6 +26,7 @@ public class Ginger public MousePicker picker; public FontType globalFont; public Fbo contrastFbo; + public GingerThreading threading; Timer timer; TickListener gameTickListener = new TickListener() { @@ -104,6 +106,7 @@ public class Ginger { INSTANCE = this; gingerRegister = new GingerRegister(); + threading = new GingerThreading(); gingerRegister.registerGame(game); timer = new Timer(game.data.tickSpeed); timer.addTickListener(gameTickListener); @@ -130,11 +133,12 @@ public class Ginger public void update(GameData data) { - Window.update(); + threading.update(); data.camera.move(); GingerUtils.update(); picker.update(); ParticleMaster.update(data.camera); + Window.update(); } public static Ginger getInstance() diff --git a/src/main/java/com/github/hydos/multiThreading/GingerThread.java b/src/main/java/com/github/hydos/multiThreading/GingerThread.java index 4d3497c..6dc13d6 100644 --- a/src/main/java/com/github/hydos/multiThreading/GingerThread.java +++ b/src/main/java/com/github/hydos/multiThreading/GingerThread.java @@ -2,6 +2,7 @@ package com.github.hydos.multiThreading; public abstract class GingerThread extends Thread { - public boolean isRunning; + public boolean finished = true; public String threadName; + public boolean started = false; } diff --git a/src/main/java/com/github/hydos/multiThreading/GingerThreading.java b/src/main/java/com/github/hydos/multiThreading/GingerThreading.java index bc3e741..96622e4 100644 --- a/src/main/java/com/github/hydos/multiThreading/GingerThreading.java +++ b/src/main/java/com/github/hydos/multiThreading/GingerThreading.java @@ -4,11 +4,28 @@ import java.util.*; public class GingerThreading { - public List threads; + public List worldChunkThreadWaitlist; public GingerThreading() - { threads = new ArrayList(); } + { worldChunkThreadWaitlist = new ArrayList(); } - public void registerThread(GingerThread thread) - { threads.add(thread); } + public void registerChunkThreadToWaitlist(GingerThread thread) + { worldChunkThreadWaitlist.add(thread); } + + public void update() { + if(worldChunkThreadWaitlist.size() > 0) { + GingerThread yes = worldChunkThreadWaitlist.get(0); + if(!yes.finished) { + worldChunkThreadWaitlist.remove(0); +// yes.stop(); + }else { + if(!yes.isAlive() && !yes.started) { + yes.start(); + } + } + } + if(worldChunkThreadWaitlist.size() > 1) { + worldChunkThreadWaitlist.get(0).start(); + } + } }