Async logic framework, box2D dependencies

This commit is contained in:
Anuken
2020-04-30 20:03:11 -04:00
parent 626a06ca00
commit 8de0ca7d6e
8 changed files with 101 additions and 1 deletions

View File

@@ -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);

View File

@@ -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();

View File

@@ -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<AsyncProcess> processes = Array.with(new PhysicsProcess());
//futures to be awaited
private Array<Future<?>> 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();
}
}
}

View File

@@ -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();
}

View File

@@ -0,0 +1,19 @@
package mindustry.async;
public class PhysicsProcess implements AsyncProcess{
@Override
public void begin(){
}
@Override
public void process(){
}
@Override
public void end(){
}
}