From 8de0ca7d6e9efa3322a4a957ee2fbdce2d6cef20 Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 30 Apr 2020 20:03:11 -0400 Subject: [PATCH] Async logic framework, box2D dependencies --- build.gradle | 2 + core/src/mindustry/ClientLauncher.java | 4 ++ core/src/mindustry/Vars.java | 3 ++ core/src/mindustry/async/AsyncLogic.java | 55 ++++++++++++++++++++ core/src/mindustry/async/AsyncProcess.java | 13 +++++ core/src/mindustry/async/PhysicsProcess.java | 19 +++++++ gradle.properties | 2 +- settings.gradle | 4 ++ 8 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 core/src/mindustry/async/AsyncLogic.java create mode 100644 core/src/mindustry/async/AsyncProcess.java create mode 100644 core/src/mindustry/async/PhysicsProcess.java diff --git a/build.gradle b/build.gradle index ee2be3dced..69f6563717 100644 --- a/build.gradle +++ b/build.gradle @@ -167,6 +167,7 @@ project(":desktop"){ dependencies{ compile project(":core") + compile arcModule("natives:natives-box2d-desktop") compile arcModule("natives:natives-desktop") compile arcModule("natives:natives-freetype-desktop") @@ -248,6 +249,7 @@ project(":core"){ compile "org.lz4:lz4-java:1.4.1" compile arcModule("arc-core") compile arcModule("extensions:freetype") + compile arcModule("extensions:box2d") compile arcModule("extensions:g3d") compile arcModule("extensions:fx") compile arcModule("extensions:arcnet") diff --git a/core/src/mindustry/ClientLauncher.java b/core/src/mindustry/ClientLauncher.java index b6fa3f290f..0442976654 100644 --- a/core/src/mindustry/ClientLauncher.java +++ b/core/src/mindustry/ClientLauncher.java @@ -132,7 +132,11 @@ public abstract class ClientLauncher extends ApplicationCore implements Platform app.post(() -> app.post(() -> app.post(() -> app.post(() -> super.resize(graphics.getWidth(), graphics.getHeight()))))); } }else{ + asyncLogic.begin(); + super.update(); + + asyncLogic.end(); } int targetfps = Core.settings.getInt("fpscap", 120); diff --git a/core/src/mindustry/Vars.java b/core/src/mindustry/Vars.java index d3fed00cba..1c06b4c4ba 100644 --- a/core/src/mindustry/Vars.java +++ b/core/src/mindustry/Vars.java @@ -11,6 +11,7 @@ import arc.util.*; import arc.util.Log.*; import arc.util.io.*; import mindustry.ai.*; +import mindustry.async.*; import mindustry.core.*; import mindustry.entities.*; import mindustry.game.*; @@ -167,6 +168,7 @@ public class Vars implements Loadable{ public static Mods mods; public static Schematics schematics = new Schematics(); public static BeControl becontrol; + public static AsyncLogic asyncLogic; public static Universe universe; public static World world; @@ -234,6 +236,7 @@ public class Vars implements Loadable{ world = new World(); universe = new Universe(); becontrol = new BeControl(); + asyncLogic = new AsyncLogic(); maps = new Maps(); spawner = new WaveSpawner(); diff --git a/core/src/mindustry/async/AsyncLogic.java b/core/src/mindustry/async/AsyncLogic.java new file mode 100644 index 0000000000..b3cf6757fd --- /dev/null +++ b/core/src/mindustry/async/AsyncLogic.java @@ -0,0 +1,55 @@ +package mindustry.async; + +import arc.struct.*; + +import java.util.concurrent.*; + +public class AsyncLogic{ + //all processes to be executed each frame + private Array processes = Array.with(new PhysicsProcess()); + + //futures to be awaited + private Array> futures = new Array<>(); + + private ExecutorService executor = Executors.newFixedThreadPool(processes.size, r -> { + Thread thread = new Thread(r, "AsyncExecutor-Thread"); + thread.setDaemon(true); + thread.setUncaughtExceptionHandler((t, e) -> { + e.printStackTrace(); + //TODO crash! + }); + return thread; + }); + + public void begin(){ + //sync begin + for(AsyncProcess p : processes){ + p.begin(); + } + + futures.clear(); + + //submit all tasks + for(AsyncProcess p : processes){ + futures.add(executor.submit(p::process)); + } + } + + public void end(){ + //wait for all threads to stop processing + for(Future future : futures){ + try{ + future.get(); + }catch(Throwable t){ + throw new RuntimeException(t); + } + } + + futures.clear(); + + //sync end (flush data) + for(AsyncProcess p : processes){ + p.end(); + } + } +} diff --git a/core/src/mindustry/async/AsyncProcess.java b/core/src/mindustry/async/AsyncProcess.java new file mode 100644 index 0000000000..c8a644108f --- /dev/null +++ b/core/src/mindustry/async/AsyncProcess.java @@ -0,0 +1,13 @@ +package mindustry.async; + +public interface AsyncProcess{ + + /** Synchronous. Called at the beginning of the main loop. */ + void begin(); + + /** Async. Called in a separate thread. */ + void process(); + + /** Sync. Called in the end of the main loop. */ + void end(); +} diff --git a/core/src/mindustry/async/PhysicsProcess.java b/core/src/mindustry/async/PhysicsProcess.java new file mode 100644 index 0000000000..f1718b5de7 --- /dev/null +++ b/core/src/mindustry/async/PhysicsProcess.java @@ -0,0 +1,19 @@ +package mindustry.async; + +public class PhysicsProcess implements AsyncProcess{ + + @Override + public void begin(){ + + } + + @Override + public void process(){ + + } + + @Override + public void end(){ + + } +} diff --git a/gradle.properties b/gradle.properties index db6841059f..17ec201c07 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=true org.gradle.jvmargs=-Xms256m -Xmx1024m -archash=2d1ea50ebc73ce8d87b275727efb785fd3e95b84 +archash=7ef1821dfd7725c97d0dbcc6a6da5c96b1fbb4b2 diff --git a/settings.gradle b/settings.gradle index e7100b94a7..23b7d0d11c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -37,6 +37,7 @@ if(!hasProperty("release")){ ':Arc:extensions:recorder', ':Arc:extensions:arcnet', ':Arc:extensions:packer', + ':Arc:extensions:box2d', ':Arc:extensions:g3d', ':Arc:extensions:fx', ':Arc:natives', @@ -46,6 +47,9 @@ if(!hasProperty("release")){ ':Arc:natives:natives-freetype-desktop', ':Arc:natives:natives-freetype-android', ':Arc:natives:natives-freetype-ios', + ':Arc:natives:natives-box2d-desktop', + ':Arc:natives:natives-box2d-android', + //':Arc:natives:natives-box2d-ios', TODO ':Arc:backends', ':Arc:backends:backend-sdl', ':Arc:backends:backend-android',