Browse Source

use ctx.scene when drawing (for drawTo)

ncannasse 10 years ago
parent
commit
2722325ebd
4 changed files with 14 additions and 12 deletions
  1. 1 1
      h2d/CachedBitmap.hx
  2. 1 1
      h2d/Mask.hx
  3. 10 10
      h2d/RenderContext.hx
  4. 2 0
      h2d/Sprite.hx

+ 1 - 1
h2d/CachedBitmap.hx

@@ -64,7 +64,7 @@ class CachedBitmap extends Drawable {
 	}
 
 	override function drawRec( ctx : RenderContext ) {
-		var scene = getScene();
+		var scene = ctx.scene;
 		if( tile != null && ((width < 0 && scene.width != tile.width) || (height < 0 && scene.height != tile.height)) )
 			clean();
 		var tile = getTile();

+ 1 - 1
h2d/Mask.hx

@@ -30,7 +30,7 @@ class Mask extends Sprite {
 		var x2 = width * matA + height * matC + absX;
 		var y2 = width * matB + height * matD + absY;
 
-		var s = getScene();
+		var s = ctx.scene;
 		x1 *= ctx.engine.width / s.width;
 		x2 *= ctx.engine.width / s.width;
 		y1 *= ctx.engine.height / s.height;

+ 10 - 10
h2d/RenderContext.hx

@@ -8,6 +8,7 @@ class RenderContext extends h3d.impl.RenderContext {
 	public var buffer : hxd.FloatBuffer;
 	public var bufPos : Int;
 	public var textures : h3d.impl.TextureCache;
+	public var scene : h2d.Scene;
 
 	public var tmpBounds = new h2d.col.Bounds();
 	var texture : h3d.mat.Texture;
@@ -21,7 +22,6 @@ class RenderContext extends h3d.impl.RenderContext {
 	var baseShaderList : hxsl.ShaderList;
 	var currentObj : Drawable;
 	var stride : Int;
-	var s2d : Scene;
 	var targetsStack : Array<{ t : h3d.mat.Texture, x : Int, y : Int, w : Int, h : Int }>;
 	var hasUVPos : Bool;
 	var inFilter : Bool;
@@ -31,9 +31,9 @@ class RenderContext extends h3d.impl.RenderContext {
 	var curWidth : Int;
 	var curHeight : Int;
 
-	public function new(s2d) {
+	public function new(scene) {
 		super();
-		this.s2d = s2d;
+		this.scene = scene;
 		if( BUFFERING )
 			buffer = new hxd.FloatBuffer();
 		bufPos = 0;
@@ -61,13 +61,13 @@ class RenderContext extends h3d.impl.RenderContext {
 		curX = 0;
 		curY = 0;
 		inFilter = false;
-		curWidth = s2d.width;
-		curHeight = s2d.height;
+		curWidth = scene.width;
+		curHeight = scene.height;
 		manager.globals.set("time", time);
 		// todo : we might prefer to auto-detect this by running a test and capturing its output
 		baseShader.pixelAlign = #if flash true #else false #end;
 		baseShader.halfPixelInverse.set(0.5 / engine.width, 0.5 / engine.height);
-		baseShader.viewport.set( -s2d.width * 0.5, -s2d.height * 0.5, 2 / s2d.width, -2 / s2d.height);
+		baseShader.viewport.set( -scene.width * 0.5, -scene.height * 0.5, 2 / scene.width, -2 / scene.height);
 		baseShader.filterMatrixA.set(1, 0, 0);
 		baseShader.filterMatrixB.set(0, 1, 0);
 		baseShaderList.next = null;
@@ -100,8 +100,8 @@ class RenderContext extends h3d.impl.RenderContext {
 		flush();
 		engine.setTarget(t);
 		initShaders(baseShaderList);
-		if( width < 0 ) width = t == null ? s2d.width : t.width;
-		if( height < 0 ) height = t == null ? s2d.height : t.height;
+		if( width < 0 ) width = t == null ? scene.width : t.width;
+		if( height < 0 ) height = t == null ? scene.height : t.height;
 		baseShader.halfPixelInverse.set(0.5 / (t == null ? engine.width : t.width), 0.5 / (t == null ? engine.height : t.height));
 		baseShader.viewport.set( -width * 0.5 - startX, -height * 0.5 - startY, 2 / width, -2 / height);
 		targetsStack.push( { t : t, x : startX, y : startY, w : width, h : height } );
@@ -122,8 +122,8 @@ class RenderContext extends h3d.impl.RenderContext {
 		var t = tinf == null ? null : tinf.t;
 		var startX = tinf == null ? 0 : tinf.x;
 		var startY = tinf == null ? 0 : tinf.y;
-		var width = tinf == null ? s2d.width : tinf.w;
-		var height = tinf == null ? s2d.height : tinf.h;
+		var width = tinf == null ? scene.width : tinf.w;
+		var height = tinf == null ? scene.height : tinf.h;
 		engine.setTarget(t);
 		initShaders(baseShaderList);
 		baseShader.halfPixelInverse.set(0.5 / (t == null ? engine.width : t.width), 0.5 / (t == null ? engine.height : t.height));

+ 2 - 0
h2d/Sprite.hx

@@ -249,8 +249,10 @@ class Sprite {
 
 	public function drawTo( t : h3d.mat.Texture ) {
 		var s = getScene();
+		var needDispose = s == null;
 		if( s == null ) s = new h2d.Scene();
 		@:privateAccess s.drawImplTo(this, t);
+		if( needDispose ) s.dispose();
 	}
 
 	function draw( ctx : RenderContext ) {