소스 검색

added scale + rotation and sync (optional)

ncannasse 12 년 전
부모
커밋
1fc0185c62
1개의 변경된 파일80개의 추가작업 그리고 26개의 파일을 삭제
  1. 80 26
      h2d/SpriteBatch.hx

+ 80 - 26
h2d/SpriteBatch.hx

@@ -4,6 +4,8 @@ package h2d;
 class BatchElement {
 	public var x : Float;
 	public var y : Float;
+	public var scale : Float;
+	public var rotation : Float;
 	public var alpha : Float;
 	public var t : Tile;
 	public var batch(default, null) : SpriteBatch;
@@ -11,12 +13,16 @@ class BatchElement {
 	var prev : BatchElement;
 	var next : BatchElement;
 	
-	function new(b,t) {
+	function new(t) {
 		x = 0; y = 0; alpha = 1;
-		this.batch = b;
+		rotation = 0; scale = 1;
 		this.t = t;
 	}
 	
+	function update(et:Float) {
+		return true;
+	}
+	
 	public inline function remove() {
 		batch.delete(this);
 	}
@@ -26,6 +32,8 @@ class BatchElement {
 class SpriteBatch extends Drawable {
 
 	public var tile : Tile;
+	public var hasRotationScale : Bool;
+	public var hasUpdate : Bool;
 	var first : BatchElement;
 	var last : BatchElement;
 	var tmpBuf : hxd.FloatBuffer;
@@ -36,8 +44,8 @@ class SpriteBatch extends Drawable {
 		shader.hasVertexAlpha = true;
 	}
 	
-	public function alloc(t) {
-		var e = new BatchElement(this, t);
+	public function add(e:BatchElement) {
+		e.batch = this;
 		if( first == null )
 			first = last = e;
 		else {
@@ -48,6 +56,10 @@ class SpriteBatch extends Drawable {
 		return e;
 	}
 	
+	public function alloc(t) {
+		return add(new BatchElement(t));
+	}
+	
 	@:allow(h2d.BatchElement)
 	function delete(e : BatchElement) {
 		if( e.prev == null ) {
@@ -62,6 +74,19 @@ class SpriteBatch extends Drawable {
 			e.next.prev = e.prev;
 	}
 	
+	override function sync(ctx) {
+		super.sync(ctx);
+		if( hasUpdate ) {
+			var e = first;
+			while( e != null ) {
+				if( !e.update(ctx.elapsedTime) )
+					e.remove();
+				e = e.next;
+			}
+		}
+	}
+	
+	
 	override function draw( ctx : RenderContext ) {
 		if( first == null )
 			return;
@@ -71,28 +96,57 @@ class SpriteBatch extends Drawable {
 		var tmp = tmpBuf;
 		while( e != null ) {
 			var t = e.t;
-			var sx = e.x + t.dx;
-			var sy = e.y + t.dy;
-			tmp[pos++] = sx;
-			tmp[pos++] = sy;
-			tmp[pos++] = t.u;
-			tmp[pos++] = t.v;
-			tmp[pos++] = e.alpha;
-			tmp[pos++] = sx + t.width + 0.1;
-			tmp[pos++] = sy;
-			tmp[pos++] = t.u2;
-			tmp[pos++] = t.v;
-			tmp[pos++] = e.alpha;
-			tmp[pos++] = sx;
-			tmp[pos++] = sy + t.height + 0.1;
-			tmp[pos++] = t.u;
-			tmp[pos++] = t.v2;
-			tmp[pos++] = e.alpha;
-			tmp[pos++] = sx + t.width + 0.1;
-			tmp[pos++] = sy + t.height + 0.1;
-			tmp[pos++] = t.u2;
-			tmp[pos++] = t.v2;
-			tmp[pos++] = e.alpha;
+			if( hasRotationScale ) {
+				var ca = Math.cos(e.rotation), sa = Math.sin(e.rotation);
+				var hx = t.width, hy = t.height;
+				var px = t.dx, py = t.dy;
+				tmp[pos++] = (px * ca + py * sa) * e.scale + e.x;
+				tmp[pos++] = (py * ca - px * sa) * e.scale + e.y;
+				tmp[pos++] = t.u;
+				tmp[pos++] = t.v;
+				tmp[pos++] = e.alpha;
+				var px = t.dx + hx, py = t.dy;
+				tmp[pos++] = (px * ca + py * sa) * e.scale + e.x;
+				tmp[pos++] = (py * ca - px * sa) * e.scale + e.y;
+				tmp[pos++] = t.u2;
+				tmp[pos++] = t.v;
+				tmp[pos++] = e.alpha;
+				var px = t.dx, py = t.dy + hy;
+				tmp[pos++] = (px * ca + py * sa) * e.scale + e.x;
+				tmp[pos++] = (py * ca - px * sa) * e.scale + e.y;
+				tmp[pos++] = t.u;
+				tmp[pos++] = t.v2;
+				tmp[pos++] = e.alpha;
+				var px = t.dx + hx, py = t.dy + hy;
+				tmp[pos++] = (px * ca + py * sa) * e.scale + e.x;
+				tmp[pos++] = (py * ca - px * sa) * e.scale + e.y;
+				tmp[pos++] = t.u2;
+				tmp[pos++] = t.v2;
+				tmp[pos++] = e.alpha;
+			} else {
+				var sx = e.x + t.dx;
+				var sy = e.y + t.dy;
+				tmp[pos++] = sx;
+				tmp[pos++] = sy;
+				tmp[pos++] = t.u;
+				tmp[pos++] = t.v;
+				tmp[pos++] = e.alpha;
+				tmp[pos++] = sx + t.width + 0.1;
+				tmp[pos++] = sy;
+				tmp[pos++] = t.u2;
+				tmp[pos++] = t.v;
+				tmp[pos++] = e.alpha;
+				tmp[pos++] = sx;
+				tmp[pos++] = sy + t.height + 0.1;
+				tmp[pos++] = t.u;
+				tmp[pos++] = t.v2;
+				tmp[pos++] = e.alpha;
+				tmp[pos++] = sx + t.width + 0.1;
+				tmp[pos++] = sy + t.height + 0.1;
+				tmp[pos++] = t.u2;
+				tmp[pos++] = t.v2;
+				tmp[pos++] = e.alpha;
+			}
 			e = e.next;
 		}
 		var stride = 5;