Переглянути джерело

moved loop logic from sdl/dx to heaps

ncannasse 7 роки тому
батько
коміт
2633823e6c
4 змінених файлів з 17 додано та 347 видалено
  1. 14 96
      libs/directx/dx/Loop.hx
  2. 0 77
      libs/directx/haxe/EntryPoint.hx
  3. 0 78
      libs/sdl/haxe/EntryPoint.hx
  4. 3 96
      libs/sdl/sdl/Sdl.hx

+ 14 - 96
libs/directx/dx/Loop.hx

@@ -2,18 +2,6 @@ package dx;
 
 
 class Loop {
 class Loop {
 
 
-	static var sentinel : hl.UI.Sentinel;
-	static var dismissErrors = false;
-
-	public static var defaultEventHandler : Event -> Bool;
-
-	/**
-		Prevent the program from reporting timeout infinite loop.
-	**/
-	public static function tick() {
-		sentinel.tick();
-	}
-
 	static function eventLoop( e : Event ) {
 	static function eventLoop( e : Event ) {
 		for( w in @:privateAccess Window.windows )
 		for( w in @:privateAccess Window.windows )
 			if( w.getNextEvent(e) )
 			if( w.getNextEvent(e) )
@@ -21,97 +9,27 @@ class Loop {
 		return false;
 		return false;
 	}
 	}
 
 
-	public static function processEvents() {
-		var event = new Event();
+	static var event = new Event();
+
+	public static function processEvents( onEvent : Event -> Bool ) {
+		while( true ) {
+			switch( hl.UI.loop(false) ) {
+			case NoMessage:
+				break;
+			case HandledMessage:
+				continue;
+			case Quit:
+				return false;
+			}
+		}
 		while( true ) {
 		while( true ) {
 			if( !eventLoop(event) )
 			if( !eventLoop(event) )
 				break;
 				break;
-			var ret = defaultEventHandler(event);
+			var ret = onEvent(event);
 			if( event.type == Quit && ret )
 			if( event.type == Quit && ret )
 				return false;
 				return false;
 		}
 		}
 		return true;
 		return true;
 	}
 	}
 
 
-	public static function run( callb : Void -> Void, ?onEvent : Event -> Bool ) {
-		var event = new Event();
-		if( onEvent == null ) onEvent = defaultEventHandler;
-		while( true ) {
-			// process windows message
-			while( true ) {
-				switch( hl.UI.loop(false) ) {
-				case NoMessage:
-					break;
-				case HandledMessage:
-					continue;
-				case Quit:
-					return;
-				}
-			}
-			// retreive captured events
-			while( true ) {
-				if( !eventLoop(event) )
-					break;
-				var ret = true;
-				if( onEvent != null ) {
-					try {
-						ret = onEvent(event);
-					} catch( e : Dynamic ) {
-						reportError(e);
-					}
-				}
-				if( event.type == Quit && ret )
-				 	return;
-			}
-			// callback our events handlers
-			try {
-				callb();
-			} catch( e : Dynamic ) {
-				reportError(e);
-			}
-			tick();
-        }
-	}
-
-	public dynamic static function reportError( e : Dynamic ) {
-
-		var stack = haxe.CallStack.toString(haxe.CallStack.exceptionStack());
-		var err = try Std.string(e) catch( _ : Dynamic ) "????";
-		Sys.println(err + stack);
-
-		if( dismissErrors )
-			return;
-
-		var f = new hl.UI.WinLog("Uncaught Exception", 500, 400);
-		f.setTextContent(err+"\n"+stack);
-		var but = new hl.UI.Button(f, "Continue");
-		but.onClick = function() {
-			hl.UI.stopLoop();
-		};
-
-		var but = new hl.UI.Button(f, "Dismiss all");
-		but.onClick = function() {
-			dismissErrors = true;
-			hl.UI.stopLoop();
-		};
-
-		var but = new hl.UI.Button(f, "Exit");
-		but.onClick = function() {
-			Sys.exit(0);
-		};
-
-		while( hl.UI.loop(true) != Quit )
-			tick();
-		f.destroy();
-	}
-
-	static function onTimeout() {
-		throw "Program timeout (infinite loop?)";
-	}
-
-	static function __init__() {
-		hl.Api.setErrorHandler(function(e) reportError(e));
-		sentinel = new hl.UI.Sentinel(30,onTimeout);
-	}
-
 }
 }

+ 0 - 77
libs/directx/haxe/EntryPoint.hx

@@ -1,77 +0,0 @@
-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 {
-		dx.Loop.run(processEvents);
-	}
-
-}

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

@@ -1,78 +0,0 @@
-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 - 96
libs/sdl/sdl/Sdl.hx

@@ -5,8 +5,6 @@ class Sdl {
 
 
 	static var initDone = false;
 	static var initDone = false;
 	static var isWin32 = false;
 	static var isWin32 = false;
-	static var sentinel : hl.UI.Sentinel;
-	static var dismissErrors = false;
 
 
 	public static var requiredGLMajor(default,null) = 3;
 	public static var requiredGLMajor(default,null) = 3;
 	public static var requiredGLMinor(default,null) = 2;
 	public static var requiredGLMinor(default,null) = 2;
@@ -36,118 +34,27 @@ class Sdl {
 		var flags = new haxe.EnumFlags<hl.UI.DialogFlags>();
 		var flags = new haxe.EnumFlags<hl.UI.DialogFlags>();
 		flags.set(IsError);
 		flags.set(IsError);
 		var msg = 'The application was unable to create an OpenGL context\nfor your $device video card.\nOpenGL $requiredGLMajor.$requiredGLMinor+ is required, please update your driver.';
 		var msg = 'The application was unable to create an OpenGL context\nfor your $device video card.\nOpenGL $requiredGLMajor.$requiredGLMinor+ is required, please update your driver.';
-		sentinel.pause = true;
 		hl.UI.dialog("OpenGL Error", msg, flags);
 		hl.UI.dialog("OpenGL Error", msg, flags);
 		Sys.exit( -1);
 		Sys.exit( -1);
 	}
 	}
 
 
 	static function glOptions( major : Int, minor : Int, depth : Int, stencil : Int, flags : Int ) {}
 	static function glOptions( major : Int, minor : Int, depth : Int, stencil : Int, flags : Int ) {}
 
 
-	static function onTimeout() {
-		throw "Program timeout (infinite loop?)";
-	}
-
-	static function __init__() {
-		hl.Api.setErrorHandler(function(e) reportError(e));
-		sentinel = new hl.UI.Sentinel(30,onTimeout);
-	}
-
 	static function initOnce() return false;
 	static function initOnce() return false;
 	static function eventLoop( e : Event ) return false;
 	static function eventLoop( e : Event ) return false;
 
 
-	public static var defaultEventHandler : Event -> Bool;
-
-	/**
-		Prevent the program from reporting timeout infinite loop.
-	**/
-	public static function tick() {
-		sentinel.tick();
-	}
-
-	public static function processEvents() {
-		var event = new Event();
+	static var event = new Event();
+	public static function processEvents( onEvent : Event -> Bool ) {
 		while( true ) {
 		while( true ) {
 			if( !eventLoop(event) )
 			if( !eventLoop(event) )
 				break;
 				break;
-			var ret = defaultEventHandler(event);
+			var ret = onEvent(event);
 			if( event.type == Quit && ret )
 			if( event.type == Quit && ret )
 				return false;
 				return false;
 		}
 		}
 		return true;
 		return true;
 	}
 	}
 
 
-	public static function loop( callb : Void -> Void, ?onEvent : Event -> Bool ) {
-		var event = new Event();
-		if( onEvent == null ) onEvent = defaultEventHandler;
-		while( true ) {
-			while( true ) {
-				if( !eventLoop(event) )
-					break;
-				var ret = true;
-				if( onEvent != null ) {
-					try {
-						ret = onEvent(event);
-					} catch( e : Dynamic ) {
-						reportError(e);
-					}
-				}
-				if( event.type == Quit && ret )
-				 	return;
-			}
-			try {
-				callb();
-			} catch( e : Dynamic ) {
-				reportError(e);
-			}
-			tick();
-        }
-	}
-
-	public dynamic static function reportError( e : Dynamic ) {
-
-		var stack = haxe.CallStack.toString(haxe.CallStack.exceptionStack());
-		var err = try Std.string(e) catch( _ : Dynamic ) "????";
-		Sys.println(err + stack);
-
-		if( dismissErrors )
-			return;
-
-		var wasFS = [];
-		for( w in @:privateAccess Window.windows ) {
-			switch( w.displayMode ) {
-			case Windowed, Borderless:
-			default:
-				wasFS.push({ w : w, mode : w.displayMode });
-				w.displayMode = Windowed;
-			}
-		}
-
-		var f = new hl.UI.WinLog("Uncaught Exception", 500, 400);
-		f.setTextContent(err+"\n"+stack);
-		var but = new hl.UI.Button(f, "Continue");
-		but.onClick = function() {
-			hl.UI.stopLoop();
-		};
-
-		var but = new hl.UI.Button(f, "Dismiss all");
-		but.onClick = function() {
-			dismissErrors = true;
-			hl.UI.stopLoop();
-		};
-
-		var but = new hl.UI.Button(f, "Exit");
-		but.onClick = function() {
-			Sys.exit(0);
-		};
-
-		while( hl.UI.loop(true) != Quit )
-			tick();
-		f.destroy();
-
-		for( w in wasFS )
-			w.w.displayMode = w.mode;
-	}
-
 	public static function quit() {
 	public static function quit() {
 	}
 	}