Nicolas Cannasse před 11 roky
rodič
revize
91ecff277c
7 změnil soubory, kde provedl 69 přidání a 83 odebrání
  1. 1 1
      h2d/Anim.hx
  2. 1 1
      h2d/CachedBitmap.hx
  3. 1 1
      h2d/Console.hx
  4. 3 50
      h2d/HtmlText.hx
  5. 17 17
      h2d/ScaleGrid.hx
  6. 38 13
      h2d/SpriteBatch.hx
  7. 8 0
      h2d/TileGroup.hx

+ 1 - 1
h2d/Anim.hx

@@ -33,7 +33,7 @@ class Anim extends Drawable {
 	
 	override function draw( ctx : RenderContext ) {
 		var t = getFrame();
-		if( t != null ) drawTile(ctx.engine,t);
+		if( t != null ) emitTile(ctx,t);
 	}
 	
 }

+ 1 - 1
h2d/CachedBitmap.hx

@@ -62,7 +62,7 @@ class CachedBitmap extends Drawable {
 	}
 
 	override function drawRec( ctx : RenderContext ) {
-		drawTile(ctx.engine, tile);
+		emitTile(ctx, tile);
 	}
 	
 	override function sync( ctx : RenderContext ) {

+ 1 - 1
h2d/Console.hx

@@ -286,7 +286,7 @@ class Console extends h2d.Sprite {
 	public function log( text : String, ?color ) {
 		if( color == null ) color = tf.textColor;
 		var oldH = logTxt.textHeight;
-		logTxt.htmlText += '<font color="#${StringTools.hex(color&0xFFFFFF,6)}">${StringTools.htmlEscape(text)}</font><br/>';
+		logTxt.text += '<font color="#${StringTools.hex(color&0xFFFFFF,6)}">${StringTools.htmlEscape(text)}</font><br/>';
 		if( logDY != 0 ) logDY += logTxt.textHeight - oldH;
 		logTxt.alpha = 1;
 		logTxt.visible = true;

+ 3 - 50
h2d/HtmlText.hx

@@ -1,39 +1,8 @@
 package h2d;
 
-class HtmlText extends Drawable {
-
-	public var font(default, null) : Font;
-	public var htmlText(default, set) : String;
-	public var textColor(default, set) : Int;
-	
-	public var textWidth(get, null) : Int;
-	public var textHeight(get, null) : Int;
-	
-	public var letterSpacing : Int;
+class HtmlText extends Text {
 	
-	var glyphs : TileColorGroup;
-	
-	public function new( font : Font, ?parent ) {
-		super(parent);
-		this.font = font;
-		glyphs = new TileColorGroup(font.tile, this);
-		htmlText = "";
-		shader = glyphs.shader;
-		textColor = 0xFFFFFF;
-	}
-	
-	override function onAlloc() {
-		super.onAlloc();
-		if( htmlText != null ) initGlyphs();
-	}
-	
-	function set_htmlText(t) {
-		this.htmlText = t == null ? "null" : t;
-		if( allocated ) initGlyphs();
-		return t;
-	}
-	
-	function initGlyphs( rebuild = true ) {
+	override function initGlyphs( text : String, rebuild = true ) {
 		if( rebuild ) glyphs.reset();
 		glyphs.setDefaultColor(textColor);
 		var x = 0, y = 0, xMax = 0;
@@ -74,25 +43,9 @@ class HtmlText extends Drawable {
 				}
 			}
 		}
-		for( e in Xml.parse(htmlText) )
+		for( e in Xml.parse(text) )
 			loop(e);
 		return { width : x > xMax ? x : xMax, height : x > 0 ? y + font.lineHeight : y };
 	}
-	
-	function get_textHeight() {
-		return initGlyphs(false).height;
-	}
-	
-	function get_textWidth() {
-		return initGlyphs(false).width;
-	}
-	
-	function set_textColor(c) {
-		if( textColor != c ) {
-			this.textColor = c;
-			if( allocated && htmlText != "" ) initGlyphs();
-		}
-		return c;
-	}
 
 }

+ 17 - 17
h2d/ScaleGrid.hx

@@ -42,10 +42,10 @@ class ScaleGrid extends h2d.TileGroup {
 			var bw = borderWidth, bh = borderHeight;
 			
 			// 4 corners
-			content.add(0, 0, tile.sub(0, 0, bw, bh));
-			content.add(width - bw, 0, tile.sub(tile.width - bw, 0, bw, bh));
-			content.add(0, height-bh, tile.sub(0, tile.height - bh, bw, bh));
-			content.add(width - bw, height - bh, tile.sub(tile.width - bw, tile.height - bh, bw, bh));
+			content.addColor(0, 0, curColor, tile.sub(0, 0, bw, bh));
+			content.addColor(width - bw, 0, curColor, tile.sub(tile.width - bw, 0, bw, bh));
+			content.addColor(0, height-bh, curColor, tile.sub(0, tile.height - bh, bw, bh));
+			content.addColor(width - bw, height - bh, curColor, tile.sub(tile.width - bw, tile.height - bh, bw, bh));
 
 			var sizeX = tile.width - bw * 2;
 			var sizeY = tile.height - bh * 2;
@@ -57,48 +57,48 @@ class ScaleGrid extends h2d.TileGroup {
 				
 				var t = tile.sub(bw, 0, sizeX, bh);
 				t.scaleToSize(w, bh);
-				content.add(bw, 0, t);
+				content.addColor(bw, 0, curColor, t);
 
 				var t = tile.sub(bw, tile.height - bh, sizeX, bh);
 				t.scaleToSize(w, bh);
-				content.add(bw, h + bh, t);
+				content.addColor(bw, h + bh, curColor, t);
 
 				var t = tile.sub(0, bh, bw, sizeY);
 				t.scaleToSize(bw, h);
-				content.add(0, bh, t);
+				content.addColor(0, bh, curColor, t);
 
 				var t = tile.sub(tile.width - bw, bh, bw, sizeY);
 				t.scaleToSize(bw, h);
-				content.add(w + bw, bh, t);
+				content.addColor(w + bw, bh, curColor, t);
 				
 			} else {
 				
 				var rw = Std.int((width - bw * 2) / sizeX);
 				for( x in 0...rw ) {
-					content.add(bw + x * sizeX, 0, tile.sub(bw, 0, sizeX, bh));
-					content.add(bw + x * sizeX, height - bh, tile.sub(bw, tile.height - bh, sizeX, bh));
+					content.addColor(bw + x * sizeX, 0, curColor, tile.sub(bw, 0, sizeX, bh));
+					content.addColor(bw + x * sizeX, height - bh, curColor, tile.sub(bw, tile.height - bh, sizeX, bh));
 				}
 				var dx = width - bw * 2 - rw * sizeX;
 				if( dx > 0 ) {
-					content.add(bw + rw * sizeX, 0, tile.sub(bw, 0, dx, bh));
-					content.add(bw + rw * sizeX, height - bh, tile.sub(bw, tile.height - bh, dx, bh));
+					content.addColor(bw + rw * sizeX, 0, curColor, tile.sub(bw, 0, dx, bh));
+					content.addColor(bw + rw * sizeX, height - bh, curColor, tile.sub(bw, tile.height - bh, dx, bh));
 				}
 
 				var rh = Std.int((height - bh * 2) / sizeY);
 				for( y in 0...rh ) {
-					content.add(0, bh + y * sizeY, tile.sub(0, bh, bw, sizeY));
-					content.add(width - bw, bh + y * sizeY, tile.sub(tile.width - bw, bh, bw, sizeY));
+					content.addColor(0, bh + y * sizeY, curColor, tile.sub(0, bh, bw, sizeY));
+					content.addColor(width - bw, bh + y * sizeY, curColor, tile.sub(tile.width - bw, bh, bw, sizeY));
 				}
 				var dy = height - bh * 2 - rh * sizeY;
 				if( dy > 0 ) {
-					content.add(0, bh + rh * sizeY, tile.sub(0, bh, bw, dy));
-					content.add(width - bw, bh + rh * sizeY, tile.sub(tile.width - bw, bh, bw, dy));
+					content.addColor(0, bh + rh * sizeY, curColor, tile.sub(0, bh, bw, dy));
+					content.addColor(width - bw, bh + rh * sizeY, curColor, tile.sub(tile.width - bw, bh, bw, dy));
 				}
 			}
 			
 			var t = tile.sub(bw, bh, sizeX, sizeY);
 			t.scaleToSize(width - bw * 2,height - bh * 2);
-			content.add(bw, bh, t);
+			content.addColor(bw, bh, curColor, t);
 		}
 		super.draw(ctx);
 	}

+ 38 - 13
h2d/SpriteBatch.hx

@@ -6,7 +6,10 @@ class BatchElement {
 	public var y : Float;
 	public var scale : Float;
 	public var rotation : Float;
-	public var alpha : Float;
+	public var r : Float;
+	public var g : Float;
+	public var b : Float;
+	public var a : Float;
 	public var t : Tile;
 	public var batch(default, null) : SpriteBatch;
 	
@@ -14,7 +17,7 @@ class BatchElement {
 	var next : BatchElement;
 	
 	function new(t) {
-		x = 0; y = 0; alpha = 1;
+		x = 0; y = 0; r = 1; g = 1; b = 1; a = 1;
 		rotation = 0; scale = 1;
 		this.t = t;
 	}
@@ -41,7 +44,6 @@ class SpriteBatch extends Drawable {
 	public function new(t,?parent) {
 		super(parent);
 		tile = t;
-		shader.hasVertexAlpha = true;
 	}
 	
 	public function add(e:BatchElement) {
@@ -86,7 +88,6 @@ class SpriteBatch extends Drawable {
 		}
 	}
 	
-	
 	override function draw( ctx : RenderContext ) {
 		if( first == null )
 			return;
@@ -104,25 +105,37 @@ class SpriteBatch extends Drawable {
 				tmp[pos++] = (py * ca - px * sa) * e.scale + e.y;
 				tmp[pos++] = t.u;
 				tmp[pos++] = t.v;
-				tmp[pos++] = e.alpha;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 				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;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 				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;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 				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;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 			} else {
 				var sx = e.x + t.dx;
 				var sy = e.y + t.dy;
@@ -130,22 +143,34 @@ class SpriteBatch extends Drawable {
 				tmp[pos++] = sy;
 				tmp[pos++] = t.u;
 				tmp[pos++] = t.v;
-				tmp[pos++] = e.alpha;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 				tmp[pos++] = sx + t.width + 0.1;
 				tmp[pos++] = sy;
 				tmp[pos++] = t.u2;
 				tmp[pos++] = t.v;
-				tmp[pos++] = e.alpha;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 				tmp[pos++] = sx;
 				tmp[pos++] = sy + t.height + 0.1;
 				tmp[pos++] = t.u;
 				tmp[pos++] = t.v2;
-				tmp[pos++] = e.alpha;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 				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;
+				tmp[pos++] = e.r;
+				tmp[pos++] = e.g;
+				tmp[pos++] = e.b;
+				tmp[pos++] = e.a;
 			}
 			e = e.next;
 		}
@@ -153,7 +178,7 @@ class SpriteBatch extends Drawable {
 		var nverts = Std.int(pos / stride);
 		var buffer = ctx.engine.mem.alloc(nverts, stride, 4);
 		buffer.uploadVector(tmpBuf, 0, nverts);
-		setupShader(ctx.engine, tile, 0);
+		ctx.beginDrawObject(this, tile.getTexture());
 		ctx.engine.renderQuadBuffer(buffer);
 		buffer.dispose();
 	}

+ 8 - 0
h2d/TileGroup.hx

@@ -13,6 +13,10 @@ private class TileLayerContent extends h3d.prim.Primitive {
 		if( buffer != null ) buffer.dispose();
 		buffer = null;
 	}
+	
+	public function isEmpty() {
+		return triCount() == 0;
+	}
 
 	override public function triCount() {
 		if( buffer == null )
@@ -26,6 +30,10 @@ private class TileLayerContent extends h3d.prim.Primitive {
 		return v >> 1;
 	}
 
+	public inline function addColor( x : Int, y : Int, color : h3d.Vector, t : Tile ) {
+		add(x, y, color.r, color.g, color.b, color.a, t);
+	}
+
 	public function add( x : Int, y : Int, r : Float, g : Float, b : Float, a : Float, t : Tile ) {
 		var sx = x + t.dx;
 		var sy = y + t.dy;