Bläddra i källkod

fixes for multi canvas rendering/events

Nicolas Cannnasse 8 år sedan
förälder
incheckning
f9db56483f
10 ändrade filer med 78 tillägg och 58 borttagningar
  1. 2 2
      h2d/Console.hx
  2. 0 1
      h2d/Scene.hx
  3. 8 6
      h3d/Engine.hx
  4. 10 8
      h3d/impl/GlDriver.hx
  5. 3 3
      h3d/mat/Texture.hx
  6. 2 1
      h3d/scene/Scene.hx
  7. 6 3
      hxd/SceneEvents.hx
  8. 41 27
      hxd/Stage.js.hx
  9. 1 1
      hxd/System.js.hx
  10. 5 6
      hxd/res/DefaultFont.hx

+ 2 - 2
h2d/Console.hx

@@ -70,11 +70,11 @@ class Console extends h2d.Sprite {
 
 	override function onAdd() {
 		super.onAdd();
-		hxd.Stage.getInstance().addEventTarget(onEvent);
+		@:privateAccess getScene().stage.addEventTarget(onEvent);
 	}
 
 	override function onRemove() {
-		hxd.Stage.getInstance().removeEventTarget(onEvent);
+		@:privateAccess getScene().stage.removeEventTarget(onEvent);
 		super.onRemove();
 	}
 

+ 0 - 1
h2d/Scene.hx

@@ -46,7 +46,6 @@ class Scene extends Layers implements h3d.IDrawable implements hxd.SceneEvents.I
 
 	function set_zoom(v:Int) {
 		var e = h3d.Engine.getCurrent();
-		var stage = hxd.Stage.getInstance();
 		var twidth = Math.ceil(stage.width / v);
 		var theight = Math.ceil(stage.height / v);
 		var totalWidth = twidth * v;

+ 8 - 6
h3d/Engine.hx

@@ -36,20 +36,23 @@ class Engine {
 	var lastTime : Float;
 	var antiAlias : Int;
 	var tmpVector = new h3d.Vector();
+	var stage : hxd.Stage;
 
 	var targetTmp : TargetTmp;
 	var targetStack : TargetTmp;
 	var currentTarget : h3d.mat.Texture;
 	var needFlushTarget : Bool;
 	var nullTexture : h3d.mat.Texture;
-
+	var textureColorCache = new Map<Int,h3d.mat.Texture>();
+	@:allow(hxd.res) var resCache = new Map<{},Dynamic>();
+	
 	@:access(hxd.Stage)
 	public function new( hardware = true, aa = 0 ) {
 		this.hardware = hardware;
 		this.antiAlias = aa;
 		this.autoResize = true;
 		fullScreen = !hxd.System.getValue(IsWindowed);
-		var stage = hxd.Stage.getInstance();
+		stage = hxd.Stage.getInstance();
 		realFps = hxd.System.getDefaultFrameRate();
 		lastTime = haxe.Timer.stamp();
 		stage.addResizeEvent(onStageResize);
@@ -194,8 +197,8 @@ class Engine {
 	}
 
 	function onCreate( disposed ) {
+		setCurrent();
 		if( autoResize ) {
-			var stage = hxd.Stage.getInstance();
 			width = stage.width;
 			height = stage.height;
 		}
@@ -223,7 +226,6 @@ class Engine {
 
 	function onStageResize() {
 		if( autoResize && !driver.isDisposed() ) {
-			var stage = hxd.Stage.getInstance();
 			var w = stage.width, h = stage.height;
 			if( w != width || h != height )
 				resize(w, h);
@@ -234,7 +236,7 @@ class Engine {
 	function set_fullScreen(v) {
 		fullScreen = v;
 		if( mem != null && hxd.System.getValue(IsWindowed) )
-			hxd.Stage.getInstance().setFullScreen(v);
+			stage.setFullScreen(v);
 		return v;
 	}
 
@@ -356,7 +358,7 @@ class Engine {
 
 	public function dispose() {
 		driver.dispose();
-		hxd.Stage.getInstance().removeResizeEvent(onStageResize);
+		stage.removeResizeEvent(onStageResize);
 	}
 
 	function get_fps() {

+ 10 - 8
h3d/impl/GlDriver.hx

@@ -133,8 +133,7 @@ class GlDriver extends Driver {
 
 	public function new(antiAlias=0) {
 		#if js
-		canvas = @:privateAccess hxd.Stage.getCanvas();
-		if( canvas == null ) throw "Canvas #webgl not found";
+		canvas = @:privateAccess hxd.Stage.getInstance().canvas;
 		gl = canvas.getContextWebGL({alpha:false,antialias:antiAlias>0});
 		if( gl == null ) throw "Could not acquire GL context";
 		// debug if webgl_debug.js is included
@@ -1075,12 +1074,15 @@ class GlDriver extends Driver {
 		#if js
 		var ready = false;
 		// wait until all assets have properly load
-		js.Browser.window.addEventListener("load", function(_) {
-			if( !ready ) {
-				ready = true;
-				onCreate(false);
-			}
-		});
+		if( js.Browser.document.readyState == 'complete' )
+			haxe.Timer.delay(onCreate.bind(false), 1);
+		else		
+			js.Browser.window.addEventListener("load", function(_) {
+				if( !ready ) {
+					ready = true;
+					onCreate(false);
+				}
+			});
 		#else
 		haxe.Timer.delay(onCreate.bind(false), 1);
 		#end

+ 3 - 3
h3d/mat/Texture.hx

@@ -344,21 +344,21 @@ class Texture {
 		return t;
 	}
 
-	static var COLOR_CACHE = new Map<Int,h3d.mat.Texture>();
 	/**
 		Creates a 1x1 texture using the RGB color passed as parameter.
 	**/
 	public static function fromColor( color : Int, ?alpha = 1., ?allocPos : h3d.impl.AllocPos ) {
+		var engine = h3d.Engine.getCurrent();
 		var aval = Std.int(alpha * 255);
 		if( aval < 0 ) aval = 0 else if( aval > 255 ) aval = 255;
 		var key = (color&0xFFFFFF) | (aval << 24);
-		var t = COLOR_CACHE.get(key);
+		var t = @:privateAccess engine.textureColorCache.get(key);
 		if( t != null )
 			return t;
 		var t = new Texture(1, 1, null, allocPos);
 		t.clear(color, alpha);
 		t.realloc = function() t.clear(color, alpha);
-		COLOR_CACHE.set(key, t);
+		@:privateAccess engine.textureColorCache.set(key, t);
 		return t;
 	}
 

+ 2 - 1
h3d/scene/Scene.hx

@@ -11,9 +11,11 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 	var events : hxd.SceneEvents;
 	var hitInteractives : Array<Interactive>;
 	var eventListeners : Array<hxd.Event -> Void>;
+	var stage : hxd.Stage;
 
 	public function new() {
 		super(null);
+		stage = hxd.Stage.getInstance();
 		eventListeners = [];
 		hitInteractives = [];
 		interactives = [];
@@ -87,7 +89,6 @@ class Scene extends Object implements h3d.IDrawable implements hxd.SceneEvents.I
 
 		if( hitInteractives.length == 0 ) {
 
-			var stage = hxd.Stage.getInstance();
 			var screenX = (event.relX / stage.width - 0.5) * 2;
 			var screenY = -(event.relY / stage.height - 0.5) * 2;
 			var p0 = camera.unproject(screenX, screenY, 0);

+ 6 - 3
hxd/SceneEvents.hx

@@ -15,6 +15,7 @@ interface Interactive {
 
 class SceneEvents {
 
+	var stage : hxd.Stage;
 	var scenes : Array<InteractiveScene>;
 
 	var currentOver : Interactive;
@@ -31,11 +32,13 @@ class SceneEvents {
 	var checkPos = new hxd.Event(ECheck);
 	var onOut = new hxd.Event(EOut);
 
-	public function new() {
+	public function new( ?stage ) {
 		scenes = [];
 		pendingEvents = [];
 		pushList = [];
-		hxd.Stage.getInstance().addEventTarget(onEvent);
+		if( stage == null ) stage = hxd.Stage.getInstance();
+		this.stage = stage;
+		stage.addEventTarget(onEvent);
 	}
 
 	function onRemove(i) {
@@ -58,7 +61,7 @@ class SceneEvents {
 	}
 
 	public function dispose() {
-		hxd.Stage.getInstance().removeEventTarget(onEvent);
+		stage.removeEventTarget(onEvent);
 	}
 
 	public function focus( i : Interactive ) {

+ 41 - 27
hxd/Stage.js.hx

@@ -15,41 +15,47 @@ class Stage {
 	var curMouseX : Float = 0.;
 	var curMouseY : Float = 0.;
 
-	@:allow(hxd)
-	static function getCanvas() {
-		var canvas : js.html.CanvasElement = cast js.Browser.document.getElementById("webgl");
-		if( canvas == null ) throw "Missing canvas#webgl";
-		return canvas;
-	}
 	var canvas : js.html.CanvasElement;
+	var element : js.html.EventTarget;
 	var canvasPos : { var width(default, never) : Float; var height(default, never) : Float; var left(default, never) : Float; var top(default, never) : Float; };
+	var timer : haxe.Timer;
 
-	function new() : Void {
+	function new( ?canvas : js.html.CanvasElement ) : Void {
 		eventTargets = new List();
 		resizeEvents = new List();
 
-		canvas = getCanvas();
+		element = canvas == null ? js.Browser.window : canvas;
+		if( canvas == null ) {
+			canvas = cast js.Browser.document.getElementById("webgl");
+			if( canvas == null ) throw "Missing canvas #webgl";
+		}
+		this.canvas = canvas;
 		canvasPos = canvas.getBoundingClientRect();
-		js.Browser.window.addEventListener("mousedown", onMouseDown);
-		js.Browser.window.addEventListener("mousemove", onMouseMove);
-		js.Browser.window.addEventListener("mouseup", onMouseUp);
-		js.Browser.window.addEventListener("mousewheel", onMouseWheel);
-		js.Browser.window.addEventListener("keydown", onKeyDown);
-		js.Browser.window.addEventListener("keyup", onKeyUp);
-		js.Browser.window.addEventListener("keypress", onKeyPress);
-		canvas.addEventListener("mousedown", function(e) {
-			onMouseDown(e);
-			e.stopPropagation();
-			e.preventDefault();
-		});
-		canvas.oncontextmenu = function(e){
-			e.stopPropagation();
-			e.preventDefault();
-			return false;
-		};
+		element.addEventListener("mousedown", onMouseDown);
+		element.addEventListener("mousemove", onMouseMove);
+		element.addEventListener("mouseup", onMouseUp);
+		element.addEventListener("mousewheel", onMouseWheel);
+		element.addEventListener("keydown", onKeyDown);
+		element.addEventListener("keyup", onKeyUp);
+		element.addEventListener("keypress", onKeyPress);
+		if( element == canvas ) {
+			canvas.setAttribute("tabindex","1"); // allow focus
+			canvas.style.outline = 'none';
+		} else {
+			canvas.addEventListener("mousedown", function(e) {
+				onMouseDown(e);
+				e.stopPropagation();
+				e.preventDefault();
+			});
+			canvas.oncontextmenu = function(e){
+				e.stopPropagation();
+				e.preventDefault();
+				return false;
+			};
+		}
 		var curW = this.width, curH = this.height;
-		var t0 = new haxe.Timer(100);
-		t0.run = function() {
+		timer = new haxe.Timer(100);
+		timer.run = function() {
 			canvasPos = canvas.getBoundingClientRect();
 			var cw = this.width, ch = this.height;
 			if( curW != cw || curH != ch ) {
@@ -60,6 +66,10 @@ class Stage {
 		};
 	}
 
+	public function dispose() {
+		timer.stop();
+	}
+
 	public dynamic function onClose() : Bool {
 		return true;
 	}
@@ -104,6 +114,10 @@ class Stage {
 	public function setFullScreen( v : Bool ) : Void {
 	}
 
+	public function setCurrent() {
+		inst = this;
+	}
+
 	static var inst : Stage = null;
 	public static function getInstance() : Stage {
 		if( inst == null ) inst = new Stage();

+ 1 - 1
hxd/System.js.hx

@@ -59,7 +59,7 @@ class System {
 	}
 
 	public static function setNativeCursor( c : Cursor ) : Void {
-		var canvas = Stage.getCanvas();
+		var canvas = @:privateAccess hxd.Stage.getInstance().canvas;
 		if( canvas != null ) {
 			canvas.style.cursor = switch( c ) {
 			case Default: "default";

+ 5 - 6
hxd/res/DefaultFont.hx

@@ -2,15 +2,14 @@ package hxd.res;
 
 class DefaultFont {
 
-	static var fnt : h2d.Font = null;
-	static var DESC = hxd.res.Embed.getResource("hxd/res/defaultFont.fnt");
-	static var BYTES = hxd.res.Embed.getResource("hxd/res/defaultFont.png");
-
 	public static function get() : h2d.Font {
+		var engine = h3d.Engine.getCurrent();
+		var fnt : h2d.Font = engine.resCache.get(DefaultFont);
 		if( fnt == null ) {
+			var BYTES = hxd.res.Embed.getResource("hxd/res/defaultFont.png");
+			var DESC = hxd.res.Embed.getResource("hxd/res/defaultFont.fnt");
 			fnt = new BitmapFont(@:privateAccess BYTES.loader, DESC.entry).toFont();
-			DESC = null;
-			BYTES = null;
+			engine.resCache.set(DefaultFont, fnt);
 		}
 		return fnt;
 	}