|
|
@ -10,6 +10,7 @@ import mightypork.gamecore.core.events.ShutdownEvent; |
|
|
|
import mightypork.gamecore.core.init.InitTask; |
|
|
|
import mightypork.gamecore.core.init.InitTask; |
|
|
|
import mightypork.gamecore.core.plugins.AppPlugin; |
|
|
|
import mightypork.gamecore.core.plugins.AppPlugin; |
|
|
|
import mightypork.gamecore.graphics.GraphicsModule; |
|
|
|
import mightypork.gamecore.graphics.GraphicsModule; |
|
|
|
|
|
|
|
import mightypork.gamecore.graphics.Renderable; |
|
|
|
import mightypork.gamecore.input.InputModule; |
|
|
|
import mightypork.gamecore.input.InputModule; |
|
|
|
import mightypork.utils.annotations.Stub; |
|
|
|
import mightypork.utils.annotations.Stub; |
|
|
|
import mightypork.utils.eventbus.EventBus; |
|
|
|
import mightypork.utils.eventbus.EventBus; |
|
|
@ -25,23 +26,25 @@ import mightypork.utils.logging.Log; |
|
|
|
* @author MightyPork |
|
|
|
* @author MightyPork |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class App extends BusNode { |
|
|
|
public class App extends BusNode { |
|
|
|
|
|
|
|
|
|
|
|
private static App instance; |
|
|
|
private static App instance; |
|
|
|
|
|
|
|
|
|
|
|
private final AppBackend backend; |
|
|
|
private final AppBackend backend; |
|
|
|
private final EventBus eventBus = new EventBus(); |
|
|
|
private final EventBus eventBus = new EventBus(); |
|
|
|
private boolean started = false; |
|
|
|
private boolean started = false; |
|
|
|
private boolean inited = false; |
|
|
|
private boolean inited = false; |
|
|
|
|
|
|
|
|
|
|
|
/** List of installed App plugins */ |
|
|
|
/** List of installed App plugins */ |
|
|
|
protected final DelegatingList plugins = new DelegatingList(); |
|
|
|
protected final DelegatingList plugins = new DelegatingList(); |
|
|
|
/** List of initializers */ |
|
|
|
/** List of initializers */ |
|
|
|
protected final List<InitTask> initializers = new ArrayList<>(); |
|
|
|
protected final List<InitTask> initializers = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
/** The used main loop instance */ |
|
|
|
/** The used main loop instance */ |
|
|
|
protected MainLoop mainLoop; |
|
|
|
protected MainLoop mainLoop; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Renderable mainRenderable; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create an app with given backend. |
|
|
|
* Create an app with given backend. |
|
|
|
* |
|
|
|
* |
|
|
@ -52,24 +55,24 @@ public class App extends BusNode { |
|
|
|
if (App.instance != null) { |
|
|
|
if (App.instance != null) { |
|
|
|
throw new IllegalStateException("App already initialized"); |
|
|
|
throw new IllegalStateException("App already initialized"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// store current instance in static field
|
|
|
|
// store current instance in static field
|
|
|
|
App.instance = this; |
|
|
|
App.instance = this; |
|
|
|
|
|
|
|
|
|
|
|
// join the bus
|
|
|
|
// join the bus
|
|
|
|
this.eventBus.subscribe(this); |
|
|
|
this.eventBus.subscribe(this); |
|
|
|
|
|
|
|
|
|
|
|
// create plugin registry attached to bus
|
|
|
|
// create plugin registry attached to bus
|
|
|
|
this.eventBus.subscribe(this.plugins); |
|
|
|
this.eventBus.subscribe(this.plugins); |
|
|
|
|
|
|
|
|
|
|
|
// initialize and use backend
|
|
|
|
// initialize and use backend
|
|
|
|
this.backend = backend; |
|
|
|
this.backend = backend; |
|
|
|
this.backend.bind(this); |
|
|
|
this.backend.bind(this); |
|
|
|
this.eventBus.subscribe(backend); |
|
|
|
this.eventBus.subscribe(backend); |
|
|
|
this.backend.initialize(); |
|
|
|
this.backend.initialize(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add a plugin to the app. Plugins can eg. listen to bus events and react |
|
|
|
* Add a plugin to the app. Plugins can eg. listen to bus events and react |
|
|
|
* to them. |
|
|
|
* to them. |
|
|
@ -78,17 +81,13 @@ public class App extends BusNode { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void addPlugin(AppPlugin plugin) |
|
|
|
public void addPlugin(AppPlugin plugin) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (started) { |
|
|
|
|
|
|
|
throw new IllegalStateException("App already started, cannot add plugins."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// attach to event bus
|
|
|
|
// attach to event bus
|
|
|
|
plugins.add(plugin); |
|
|
|
plugins.add(plugin); |
|
|
|
plugin.bind(this); |
|
|
|
plugin.bind(this); |
|
|
|
plugin.initialize(); |
|
|
|
plugin.initialize(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Add an initializer to the app. |
|
|
|
* Add an initializer to the app. |
|
|
|
* |
|
|
|
* |
|
|
@ -99,11 +98,11 @@ public class App extends BusNode { |
|
|
|
if (started) { |
|
|
|
if (started) { |
|
|
|
throw new IllegalStateException("App already started, cannot add initializers."); |
|
|
|
throw new IllegalStateException("App already started, cannot add initializers."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
initializers.add(initializer); |
|
|
|
initializers.add(initializer); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the main loop implementation |
|
|
|
* Set the main loop implementation |
|
|
|
* |
|
|
|
* |
|
|
@ -112,9 +111,21 @@ public class App extends BusNode { |
|
|
|
public void setMainLoop(MainLoop loop) |
|
|
|
public void setMainLoop(MainLoop loop) |
|
|
|
{ |
|
|
|
{ |
|
|
|
this.mainLoop = loop; |
|
|
|
this.mainLoop = loop; |
|
|
|
|
|
|
|
bus().subscribe(loop); // ?
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set the main renderable |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param renderable the main renderable |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setMainRenderable(Renderable renderable) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
this.mainRenderable = renderable; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get current backend |
|
|
|
* Get current backend |
|
|
|
* |
|
|
|
* |
|
|
@ -124,8 +135,8 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
return backend; |
|
|
|
return backend; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Initialize the App and start operating.<br> |
|
|
|
* Initialize the App and start operating.<br> |
|
|
|
* This method should be called after adding all required initializers and |
|
|
|
* This method should be called after adding all required initializers and |
|
|
@ -137,59 +148,60 @@ public class App extends BusNode { |
|
|
|
throw new IllegalStateException("Already started."); |
|
|
|
throw new IllegalStateException("Already started."); |
|
|
|
} |
|
|
|
} |
|
|
|
started = true; |
|
|
|
started = true; |
|
|
|
|
|
|
|
|
|
|
|
Log.i("Starting init..."); |
|
|
|
Log.i("Starting init..."); |
|
|
|
init(); |
|
|
|
init(); |
|
|
|
|
|
|
|
|
|
|
|
if (mainLoop == null) { |
|
|
|
if (mainLoop == null) { |
|
|
|
throw new IllegalStateException("The app has no main loop assigned."); |
|
|
|
throw new IllegalStateException("The app has no main loop assigned."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.i("Starting main loop..."); |
|
|
|
Log.i("Starting main loop..."); |
|
|
|
|
|
|
|
mainLoop.setRootRenderable(mainRenderable); |
|
|
|
mainLoop.start(); |
|
|
|
mainLoop.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void init() |
|
|
|
private void init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
if (inited) { |
|
|
|
if (inited) { |
|
|
|
throw new IllegalStateException("Already inited."); |
|
|
|
throw new IllegalStateException("Already inited."); |
|
|
|
} |
|
|
|
} |
|
|
|
inited = true; |
|
|
|
inited = true; |
|
|
|
|
|
|
|
|
|
|
|
// pre-init hook, just in case anyone wanted to have one.
|
|
|
|
// pre-init hook, just in case anyone wanted to have one.
|
|
|
|
Log.f2("Calling pre-init hook..."); |
|
|
|
Log.f2("Calling pre-init hook..."); |
|
|
|
preInit(); |
|
|
|
preInit(); |
|
|
|
|
|
|
|
|
|
|
|
Log.f2("Running init tasks..."); |
|
|
|
Log.f2("Running init tasks..."); |
|
|
|
|
|
|
|
|
|
|
|
// sort initializers by order.
|
|
|
|
// sort initializers by order.
|
|
|
|
final List<InitTask> orderedInitializers = InitTask.inOrder(initializers); |
|
|
|
final List<InitTask> orderedInitializers = InitTask.inOrder(initializers); |
|
|
|
|
|
|
|
|
|
|
|
for (final InitTask initTask : orderedInitializers) { |
|
|
|
for (final InitTask initTask : orderedInitializers) { |
|
|
|
Log.f1("Running init task \"" + initTask.getName() + "\"..."); |
|
|
|
Log.f1("Running init task \"" + initTask.getName() + "\"..."); |
|
|
|
|
|
|
|
|
|
|
|
initTask.bind(this); |
|
|
|
initTask.bind(this); |
|
|
|
|
|
|
|
|
|
|
|
// set the task options
|
|
|
|
// set the task options
|
|
|
|
initTask.init(); |
|
|
|
initTask.init(); |
|
|
|
|
|
|
|
|
|
|
|
initTask.before(); |
|
|
|
initTask.before(); |
|
|
|
|
|
|
|
|
|
|
|
// main task action
|
|
|
|
// main task action
|
|
|
|
initTask.run(); |
|
|
|
initTask.run(); |
|
|
|
|
|
|
|
|
|
|
|
// after hook for extra actions immeditaely after the task completes
|
|
|
|
// after hook for extra actions immeditaely after the task completes
|
|
|
|
initTask.after(); |
|
|
|
initTask.after(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// user can now start the main loop etc.
|
|
|
|
// user can now start the main loop etc.
|
|
|
|
Log.f2("Calling post-init hook..."); |
|
|
|
Log.f2("Calling post-init hook..."); |
|
|
|
postInit(); |
|
|
|
postInit(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Hook called before the initialization sequence starts. |
|
|
|
* Hook called before the initialization sequence starts. |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -197,8 +209,8 @@ public class App extends BusNode { |
|
|
|
protected void preInit() |
|
|
|
protected void preInit() |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Hook called after the initialization sequence is finished. |
|
|
|
* Hook called after the initialization sequence is finished. |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -206,8 +218,8 @@ public class App extends BusNode { |
|
|
|
protected void postInit() |
|
|
|
protected void postInit() |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Shut down the running instance.<br> |
|
|
|
* Shut down the running instance.<br> |
|
|
|
* Deinitialize backend modules and terminate the JVM. |
|
|
|
* Deinitialize backend modules and terminate the JVM. |
|
|
@ -216,9 +228,9 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (instance != null) { |
|
|
|
if (instance != null) { |
|
|
|
Log.i("Dispatching Shutdown event..."); |
|
|
|
Log.i("Dispatching Shutdown event..."); |
|
|
|
|
|
|
|
|
|
|
|
bus().send(new ShutdownEvent(new Runnable() { |
|
|
|
bus().send(new ShutdownEvent(new Runnable() { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() |
|
|
|
public void run() |
|
|
|
{ |
|
|
|
{ |
|
|
@ -231,19 +243,19 @@ public class App extends BusNode { |
|
|
|
} catch (final Throwable e) { |
|
|
|
} catch (final Throwable e) { |
|
|
|
Log.e(e); |
|
|
|
Log.e(e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.i("Shutdown completed."); |
|
|
|
Log.i("Shutdown completed."); |
|
|
|
System.exit(0); |
|
|
|
System.exit(0); |
|
|
|
} |
|
|
|
} |
|
|
|
})); |
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Log.w("App is not running."); |
|
|
|
Log.w("App is not running."); |
|
|
|
System.exit(0); |
|
|
|
System.exit(0); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get the currently running App instance. |
|
|
|
* Get the currently running App instance. |
|
|
|
* |
|
|
|
* |
|
|
@ -253,8 +265,8 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
return instance; |
|
|
|
return instance; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get graphics module from the running app's backend |
|
|
|
* Get graphics module from the running app's backend |
|
|
|
* |
|
|
|
* |
|
|
@ -264,8 +276,8 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
return instance.backend.getGraphics(); |
|
|
|
return instance.backend.getGraphics(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get audio module from the running app's backend |
|
|
|
* Get audio module from the running app's backend |
|
|
|
* |
|
|
|
* |
|
|
@ -275,8 +287,8 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
return instance.backend.getAudio(); |
|
|
|
return instance.backend.getAudio(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get input module from the running app's backend |
|
|
|
* Get input module from the running app's backend |
|
|
|
* |
|
|
|
* |
|
|
@ -286,8 +298,8 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
return instance.backend.getInput(); |
|
|
|
return instance.backend.getInput(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get event bus instance. |
|
|
|
* Get event bus instance. |
|
|
|
* |
|
|
|
* |
|
|
@ -297,8 +309,8 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
return instance.eventBus; |
|
|
|
return instance.eventBus; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get the main config, if initialized. |
|
|
|
* Get the main config, if initialized. |
|
|
|
* |
|
|
|
* |
|
|
@ -309,8 +321,8 @@ public class App extends BusNode { |
|
|
|
{ |
|
|
|
{ |
|
|
|
return cfg("main"); |
|
|
|
return cfg("main"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get a config by alias. |
|
|
|
* Get a config by alias. |
|
|
|
* |
|
|
|
* |
|
|
|