|
@@ -1,104 +1,102 @@
|
|
package h2d;
|
|
package h2d;
|
|
|
|
|
|
-class Tiles {
|
|
|
|
|
|
+@:allow(h2d)
|
|
|
|
+class Tile {
|
|
|
|
+
|
|
|
|
+ static inline var EPSILON_PIXEL = 0.00001;
|
|
|
|
|
|
- var bmp : flash.display.BitmapData;
|
|
|
|
var tex : h3d.mat.Texture;
|
|
var tex : h3d.mat.Texture;
|
|
- public var width(get, null) : Int;
|
|
|
|
- public var height(get, null) : Int;
|
|
|
|
- public var elements : Array<Array<TilePos>>;
|
|
|
|
|
|
|
|
- public function new() {
|
|
|
|
- elements = [];
|
|
|
|
- }
|
|
|
|
|
|
+ var u : Float;
|
|
|
|
+ var v : Float;
|
|
|
|
+ var u2 : Float;
|
|
|
|
+ var v2 : Float;
|
|
|
|
|
|
- public function create( x, y, w, h, dx = 0, dy = 0 ) {
|
|
|
|
- return new TilePos(this, x, y, w, h, dx, dy);
|
|
|
|
- }
|
|
|
|
|
|
+ public var dx : Int;
|
|
|
|
+ public var dy : Int;
|
|
|
|
+ public var x(default,null) : Int;
|
|
|
|
+ public var y(default,null) : Int;
|
|
|
|
+ public var width(default,null) : Int;
|
|
|
|
+ public var height(default,null) : Int;
|
|
|
|
|
|
- function get_width() {
|
|
|
|
- return tex == null ? bmp.width : tex.width;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- function get_height() {
|
|
|
|
- return tex == null ? bmp.height : tex.height;
|
|
|
|
|
|
+ function new(tex, x, y, w, h, dx=0, dy=0) {
|
|
|
|
+ this.tex = tex;
|
|
|
|
+ this.x = x;
|
|
|
|
+ this.y = y;
|
|
|
|
+ this.width = w;
|
|
|
|
+ this.height = h;
|
|
|
|
+ this.dx = dx;
|
|
|
|
+ this.dy = dy;
|
|
|
|
+ if( tex != null ) setTexture(tex);
|
|
}
|
|
}
|
|
|
|
|
|
- public function get( id : Int ) {
|
|
|
|
- var e = elements[0][id];
|
|
|
|
- if( e == null )
|
|
|
|
- throw "Invalid tile #" + id;
|
|
|
|
- return e;
|
|
|
|
|
|
+ public function setTexture(tex) {
|
|
|
|
+ this.tex = tex;
|
|
|
|
+ this.u = x / tex.width;
|
|
|
|
+ this.v = y / tex.height;
|
|
|
|
+ this.u2 = (x + width - EPSILON_PIXEL) / tex.width;
|
|
|
|
+ this.v2 = (y + height - EPSILON_PIXEL) / tex.height;
|
|
}
|
|
}
|
|
|
|
|
|
- public function getBitmap() {
|
|
|
|
- return bmp;
|
|
|
|
|
|
+ public function sub( x, y, w, h, dx = 0, dy = 0 ) {
|
|
|
|
+ return new Tile(tex, this.x + x, this.y + y, w, h, dx, dy);
|
|
}
|
|
}
|
|
|
|
|
|
- public function dispose() {
|
|
|
|
- if( bmp != null ) {
|
|
|
|
- bmp.dispose();
|
|
|
|
- bmp = null;
|
|
|
|
- }
|
|
|
|
- if( tex != null ) {
|
|
|
|
- tex.dispose();
|
|
|
|
- tex = null;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public function getTexture( engine : h3d.Engine ) {
|
|
|
|
- if( tex == null ) {
|
|
|
|
- tex = engine.mem.allocTexture(bmp.width, bmp.height);
|
|
|
|
- tex.upload(bmp);
|
|
|
|
- }
|
|
|
|
- return tex;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static function fromBitmap( bmp : flash.display.BitmapData ) {
|
|
|
|
- var tl = new Tiles();
|
|
|
|
|
|
+ public static function fromBitmap( bmp : flash.display.BitmapData, freeBitmap = true ) {
|
|
var w = 1, h = 1;
|
|
var w = 1, h = 1;
|
|
while( w < bmp.width )
|
|
while( w < bmp.width )
|
|
w <<= 1;
|
|
w <<= 1;
|
|
while( h < bmp.height )
|
|
while( h < bmp.height )
|
|
h <<= 1;
|
|
h <<= 1;
|
|
|
|
+ var tex = h3d.Engine.getCurrent().mem.allocTexture(w, h);
|
|
if( w != bmp.width || h != bmp.height ) {
|
|
if( w != bmp.width || h != bmp.height ) {
|
|
var bmp2 = new flash.display.BitmapData(w, h, true, 0);
|
|
var bmp2 = new flash.display.BitmapData(w, h, true, 0);
|
|
var p0 = new flash.geom.Point(0, 0);
|
|
var p0 = new flash.geom.Point(0, 0);
|
|
bmp2.copyPixels(bmp, bmp.rect, p0, bmp, p0, true);
|
|
bmp2.copyPixels(bmp, bmp.rect, p0, bmp, p0, true);
|
|
- tl.bmp = bmp2;
|
|
|
|
- bmp.dispose();
|
|
|
|
|
|
+ tex.upload(bmp2);
|
|
|
|
+ bmp2.dispose();
|
|
} else
|
|
} else
|
|
- tl.bmp = bmp;
|
|
|
|
- tl.elements.push([new TilePos(tl, 0, 0, tl.width, tl.height)]);
|
|
|
|
- return tl;
|
|
|
|
|
|
+ tex.upload(bmp);
|
|
|
|
+ var t = new Tile(tex, 0, 0, bmp.width, bmp.height);
|
|
|
|
+ if( freeBitmap )
|
|
|
|
+ bmp.dispose();
|
|
|
|
+ return t;
|
|
}
|
|
}
|
|
|
|
|
|
- public static function autoCut( bmp : flash.display.BitmapData, size : Int ) {
|
|
|
|
|
|
+ public static function autoCut( bmp : flash.display.BitmapData, size : Int, freeBitmap = true ) {
|
|
var colorBG = bmp.getPixel32(bmp.width - 1, bmp.height - 1);
|
|
var colorBG = bmp.getPixel32(bmp.width - 1, bmp.height - 1);
|
|
- var tl = new Tiles();
|
|
|
|
- tl.bmp = bmp;
|
|
|
|
|
|
+ var tl = new Array();
|
|
|
|
+ var w = 1, h = 1;
|
|
|
|
+ while( w < bmp.width )
|
|
|
|
+ w <<= 1;
|
|
|
|
+ while( h < bmp.height )
|
|
|
|
+ h <<= 1;
|
|
|
|
+ var tex = h3d.Engine.getCurrent().mem.allocTexture(w, h);
|
|
for( y in 0...Std.int(bmp.height / size) ) {
|
|
for( y in 0...Std.int(bmp.height / size) ) {
|
|
var a = [];
|
|
var a = [];
|
|
- tl.elements[y] = a;
|
|
|
|
|
|
+ tl[y] = a;
|
|
for( x in 0...Std.int(bmp.width / size) ) {
|
|
for( x in 0...Std.int(bmp.width / size) ) {
|
|
var sz = isEmpty(bmp, x * size, y * size, size, colorBG);
|
|
var sz = isEmpty(bmp, x * size, y * size, size, colorBG);
|
|
if( sz == null )
|
|
if( sz == null )
|
|
break;
|
|
break;
|
|
- a.push(new TilePos(tl, x*size+sz.dx, y*size+sz.dy, sz.w, sz.h, sz.dx, sz.dy));
|
|
|
|
|
|
+ a.push(new Tile(tex,x*size+sz.dx, y*size+sz.dy, sz.w, sz.h, sz.dx, sz.dy));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return tl;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static function fromTexture( tex : h3d.mat.Texture ) {
|
|
|
|
- var t = new Tiles();
|
|
|
|
- t.tex = tex;
|
|
|
|
- return t;
|
|
|
|
|
|
+ if( w != bmp.width || h != bmp.height ) {
|
|
|
|
+ var bmp2 = new flash.display.BitmapData(w, h, true, 0);
|
|
|
|
+ var p0 = new flash.geom.Point(0, 0);
|
|
|
|
+ bmp2.copyPixels(bmp, bmp.rect, p0, bmp, p0, true);
|
|
|
|
+ tex.upload(bmp2);
|
|
|
|
+ bmp2.dispose();
|
|
|
|
+ } else
|
|
|
|
+ tex.upload(bmp);
|
|
|
|
+ var main = new Tile(tex, 0, 0, bmp.width, bmp.height);
|
|
|
|
+ if( freeBitmap )
|
|
|
|
+ bmp.dispose();
|
|
|
|
+ return { main : main, tiles : tl };
|
|
}
|
|
}
|
|
|
|
|
|
public static function fromSprites( sprites : Array<flash.display.Sprite> ) {
|
|
public static function fromSprites( sprites : Array<flash.display.Sprite> ) {
|
|
- var tl = new Tiles();
|
|
|
|
- tl.elements[0] = [];
|
|
|
|
var tmp = [];
|
|
var tmp = [];
|
|
var width = 0;
|
|
var width = 0;
|
|
var height = 0;
|
|
var height = 0;
|
|
@@ -118,15 +116,17 @@ class Tiles {
|
|
while( rh < height )
|
|
while( rh < height )
|
|
rh <<= 1;
|
|
rh <<= 1;
|
|
var bmp = new flash.display.BitmapData(rw, rh, true, 0);
|
|
var bmp = new flash.display.BitmapData(rw, rh, true, 0);
|
|
- tl.bmp = bmp;
|
|
|
|
var m = new flash.geom.Matrix();
|
|
var m = new flash.geom.Matrix();
|
|
for( t in tmp ) {
|
|
for( t in tmp ) {
|
|
m.tx = t.x-t.dx;
|
|
m.tx = t.x-t.dx;
|
|
m.ty = -t.dy;
|
|
m.ty = -t.dy;
|
|
bmp.draw(t.s, m);
|
|
bmp.draw(t.s, m);
|
|
- tl.elements[0].push(new TilePos(tl, t.x, 0, t.w, t.h, t.dx, t.dy));
|
|
|
|
}
|
|
}
|
|
- return tl;
|
|
|
|
|
|
+ var main = fromBitmap(bmp);
|
|
|
|
+ var tiles = [];
|
|
|
|
+ for( t in tmp )
|
|
|
|
+ tiles.push(main.sub(t.x, 0, t.w, t.h, t.dx, t.dy));
|
|
|
|
+ return tiles;
|
|
}
|
|
}
|
|
|
|
|
|
static function isEmpty( b : flash.display.BitmapData, px, py, size, bg : UInt ) {
|
|
static function isEmpty( b : flash.display.BitmapData, px, py, size, bg : UInt ) {
|