浏览代码

added support for multiple apps

Nicolas Cannasse 1 年之前
父节点
当前提交
2119169648
共有 1 个文件被更改,包括 64 次插入0 次删除
  1. 64 0
      hxd/impl/AppContext.hx

+ 64 - 0
hxd/impl/AppContext.hx

@@ -0,0 +1,64 @@
+package hxd.impl;
+
+/**
+	Create an app context to allow multiple apps to run in parallel.
+	Requires compilation with -D multidriver
+**/
+class AppContext {
+
+	static var contexts : Array<AppContext> = [];
+
+	public var win : hxd.Window;
+	public var engine : h3d.Engine;
+	public var app : hxd.App;
+
+	public function new(app) {
+		#if !multidriver
+		throw "Needs -D multidriver";
+		#end
+		this.app = app;
+		win = hxd.Window.getInstance();
+		win.onClose = function() {
+			@:privateAccess app.dispose();
+			return true;
+		};
+		engine = h3d.Engine.getCurrent();
+		var curReady = engine.onReady;
+		engine.onReady = function() {
+			curReady();
+			reset();
+			hxd.System.setLoop(run);
+		};
+		contexts.push(this);
+		reset();
+	}
+
+	public function update() {
+		if( app.sevents == null )
+			return;
+		engine.setCurrent();
+		@:privateAccess {
+			hxd.System.loopFunc = app.mainLoop;
+			hxd.System.mainLoop();
+			hxd.System.loopFunc = run;
+		}
+		reset();
+	}
+
+	static function run() {
+		for( c in contexts )
+			c.update();
+	}
+
+	public static function reset() @:privateAccess {
+		h3d.Engine.CURRENT = null;
+		hxd.Window.inst = null;
+	}
+
+	public static function set( app : hxd.App ) {
+		for( c in contexts )
+			if( c.app == app )
+				c.engine.setCurrent();
+	}
+
+}