Jelajahi Sumber

fixes for h2d

ncannasse 12 tahun lalu
induk
melakukan
4288eb9d5d
6 mengubah file dengan 49 tambahan dan 24 penghapusan
  1. 1 1
      h2d/comp/Context.hx
  2. 42 3
      hxd/Resource.hx
  3. 0 10
      hxd/System.hx
  4. 4 8
      samples/2d/Demo.hx
  5. TEMPAT SAMPAH
      samples/2d/texture.png
  6. 2 2
      samples/comps/Comps.hx

+ 1 - 1
h2d/comp/Context.hx

@@ -36,7 +36,7 @@ class Context {
 
 	static var FONTS = new Map<String,h2d.Font>();
 	
-	public static var DEFAULT_CSS = hxd.System.getFileContent("h2d/css/default.css");
+	public static var DEFAULT_CSS = hxd.Resource.getFileContent("h2d/css/default.css");
 	
 	static var DEF = null;
 	public static function getDefaultCss() {

+ 42 - 3
hxd/Resource.hx

@@ -8,7 +8,7 @@ abstract BitmapRes(String) {
 	public function new( resourceName : String ) {
 		this = resourceName;
 	}
-	public function toTexture() {
+	public function toTexture() : h3d.mat.Texture {
 		var engine = h3d.Engine.getCurrent();
 		var res = haxe.Resource.getBytes(this);
 		if( res == null ) throw "Missing resource " + this;
@@ -30,9 +30,14 @@ abstract BitmapRes(String) {
 			tex.uploadBytes(pngBytes);
 			return tex;
 		} else {
+			// need JPG support
 			throw "TODO";
 		}
 	}
+	public function toTile() : h2d.Tile {
+		throw "TODO";
+		return null;
+	}
 }
 
 #elseif flash
@@ -41,12 +46,39 @@ abstract BitmapRes(Class<flash.display.BitmapData>) {
 	public function new( cl ) {
 		this = cl;
 	}
-	public function toTexture() {
+	
+	function makeTexture( bmp : flash.display.BitmapData) {
+		var iw = 1, ih = 1;
+		while( iw < bmp.width ) iw <<= 1;
+		while( ih < bmp.height ) ih <<= 1;
+		var tex;
+		if( iw == bmp.width && ih == bmp.height )
+			tex = h3d.mat.Texture.fromBitmap(hxd.BitmapData.fromNative(bmp));
+		else {
+			var bmp2 = new flash.display.BitmapData(iw, ih);
+			bmp2.copyPixels(bmp, bmp.rect, new flash.geom.Point());
+			tex = h3d.mat.Texture.fromBitmap(hxd.BitmapData.fromNative(bmp2));
+			bmp2.dispose();
+		}
+		return tex;
+	}
+	
+	public function toTexture() : h3d.mat.Texture {
 		var bmp = Type.createInstance(this, [0, 0]);
-		var tex = h3d.mat.Texture.fromBitmap(hxd.BitmapData.fromNative(bmp));
+		var tex = makeTexture(bmp);
 		bmp.dispose();
 		return tex;
 	}
+	public function toTile() : h2d.Tile {
+		var bmp = Type.createInstance(this, [0, 0]);
+		var w = bmp.width, h = bmp.height;
+		var tex = makeTexture(bmp);
+		bmp.dispose();
+		var t = h2d.Tile.fromTexture(tex);
+		if( t.width != w || t.height != h )
+			t = t.sub(0, 0, w, h);
+		return t;
+	}
 }
 		
 #end
@@ -87,5 +119,12 @@ class Resource {
 			return macro null;
 		}
 	}
+
+	public static macro function getFileContent( file : String ) {
+		var file = haxe.macro.Context.resolvePath(file);
+		var m = haxe.macro.Context.getLocalClass().get().module;
+		haxe.macro.Context.registerModuleDependency(m, file);
+		return macro $v{sys.io.File.getContent(file)};
+	}
 	
 }

+ 0 - 10
hxd/System.hx

@@ -9,8 +9,6 @@ enum Cursor {
 
 class System {
 	
-	#if !macro
-
 	public static var width(get,never) : Int;
 	public static var height(get,never) : Int;
 	public static var isTouch(get,never) : Bool;
@@ -164,13 +162,5 @@ class System {
 	#else
 
 	#end
-	#end
-	
-	public static macro function getFileContent( file : String ) {
-		var file = haxe.macro.Context.resolvePath(file);
-		var m = haxe.macro.Context.getLocalClass().get().module;
-		haxe.macro.Context.registerModuleDependency(m, file);
-		return macro $v{sys.io.File.getContent(file)};
-	}
 	
 }

+ 4 - 8
samples/2d/Demo.hx

@@ -19,13 +19,8 @@ class Demo {
 		tf.textColor = 0xFFFFFF;
 		tf.dropShadow = { dx : 2, dy : 2, color : 0xFF0000, alpha : 0.5 };
 		tf.text = "Hello h2d !";
-		flash.Lib.current.addEventListener(flash.events.Event.ENTER_FRAME, update);
-		
-		var b = new flash.display.BitmapData(64, 64, true, 0);
-		b.perlinNoise(64, 64, 3, 42, true, true, 7);
-		var tile = h2d.Tile.fromBitmap(b);
-		b.dispose();
 		
+		var tile = hxd.Resource.embed("texture.png").toTile();
 		spr = new h2d.Sprite(scene);
 		spr.x = engine.width >> 1;
 		spr.y = engine.height >> 1;
@@ -35,11 +30,12 @@ class Demo {
 			bmp.x = Math.cos(i * Math.PI / 8) * 100 - (tile.width>>1);
 			bmp.y = Math.sin(i * Math.PI / 8) * 100 - (tile.height>>1);
 			bmp.alpha = 0.5;
-			bmp.blendMode = Add;
 		}
+		
+		hxd.System.setLoop(update);
 	}
 	
-	function update(_) {
+	function update() {
 		spr.rotation += 0.01;
 		engine.render(scene);
 	}

TEMPAT SAMPAH
samples/2d/texture.png


+ 2 - 2
samples/comps/Comps.hx

@@ -13,9 +13,9 @@ class Comps {
 	}
 	
 	function init() {
-		h3d.System.setLoop(update);
+		hxd.System.setLoop(update);
 		scene = new h2d.Scene();
-		var document = h2d.comp.Parser.fromHtml(h3d.System.getFileContent("components.html"),{ fmt : h3d.FMath.fmt });
+		var document = h2d.comp.Parser.fromHtml(hxd.Resource.getFileContent("components.html"),{ fmt : h3d.FMath.fmt });
 		scene.addChild(document);
 		engine.onResized = function() document.setStyle(null);
 	}