Răsfoiți Sursa

added EntryPoint support

Nicolas Cannasse 9 ani în urmă
părinte
comite
d57357ddcb
2 a modificat fișierele cu 81 adăugiri și 1 ștergeri
  1. 78 0
      libs/sdl/haxe/EntryPoint.hx
  2. 3 1
      libs/sdl/sdl/Sdl.hx

+ 78 - 0
libs/sdl/haxe/EntryPoint.hx

@@ -0,0 +1,78 @@
+package haxe;
+
+private class Lock {
+	public function new() {
+	}
+	public inline function release() {
+	}
+	public inline function wait( ?t : Float ) {
+	}
+}
+private class Mutex {
+	public function new() {
+	}
+	public inline function acquire() {
+	}
+	public inline function release() {
+	}
+}
+private class Thread {
+	public static function create( f : Void -> Void ) {
+		f();
+	}
+}
+
+class EntryPoint {
+
+	static var sleepLock = new Lock();
+	static var mutex = new Mutex();
+	static var pending = new Array<Void->Void>();
+
+	public static var threadCount(default,null) : Int = 0;
+
+	public static function wakeup() {
+		#if sys
+		sleepLock.release();
+		#end
+	}
+
+	public static function runInMainThread( f : Void -> Void ) {
+		mutex.acquire();
+		pending.push(f);
+		mutex.release();
+		wakeup();
+	}
+
+	public static function addThread( f : Void -> Void ) {
+		mutex.acquire();
+		threadCount++;
+		mutex.release();
+		Thread.create(function() {
+			f();
+			mutex.acquire();
+			threadCount--;
+			if( threadCount == 0 ) wakeup();
+			mutex.release();
+		});
+	}
+
+	static function processEvents() : Float {
+		// flush all pending calls
+		while( true ) {
+			mutex.acquire();
+			var f = pending.shift();
+			mutex.release();
+			if( f == null ) break;
+			f();
+		}
+		if( !MainLoop.hasEvents() && threadCount == 0 )
+			return -1;
+		return @:privateAccess MainLoop.tick();
+	}
+
+	@:keep public static function run() @:privateAccess {
+		sdl.Sdl.loop(function() processEvents());
+		sdl.Sdl.quit();
+	}
+
+}

+ 3 - 1
libs/sdl/sdl/Sdl.hx

@@ -14,9 +14,12 @@ class Sdl {
 
 	static function initOnce() return false;
 	static function eventLoop( e : Event ) return false;
+	
+	public static var defaultEventHandler : Event -> Void;
 
 	public static function loop( callb : Void -> Void, ?onEvent : Event -> Void ) {
 		var event = new Event();
+		if( onEvent == null ) onEvent = defaultEventHandler;
 		while( true ) {
 			while( true ) {
 				if( !eventLoop(event) )
@@ -26,7 +29,6 @@ class Sdl {
 				if( onEvent != null ) onEvent(event);
 			}
 			callb();
-			@:privateAccess haxe.Timer.sync();
         }
 	}