Created
April 2, 2015 18:24
-
-
Save Beeblerox/6eb72d17ce93c2ae35c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This method takes array of tileset bitmaps and the size of tiles in them and then combine then in one big tileset. | |
* The order of bitmaps in array is important. | |
* | |
* @param bitmaps tilesets | |
* @param tileSize the size of tiles (tilesets should have tiles of the same size) | |
* @return atlas frames collection, which you can load in tilemaps or sprites: | |
* | |
* var combinedFrames = combineTiles(bitmaps, new FlxPoint(16, 16)); | |
* tilemap.loadMapFromCSV(mapData, combinedFrames); | |
*/ | |
public function combineTiles(bitmaps:Array<BitmapData>, tileSize:FlxPoint):FlxAtlasFrames | |
{ | |
// we need to calculate the size of result bitmap first | |
var totalArea:Int = 0; | |
var rows:Int = 0; | |
var cols:Int = 0; | |
for (bitmap in bitmaps) | |
{ | |
cols = Std.int(bitmap.width / tileSize.x); | |
rows = Std.int(bitmap.height / tileSize.y); | |
totalArea += Std.int(cols * tileSize.x * rows * tileSize.y); | |
} | |
var side:Float = Math.sqrt(totalArea); | |
cols = Std.int(side / tileSize.x); | |
rows = Math.ceil(totalArea / (cols * tileSize.x * tileSize.y)); | |
var width:Int = Std.int(cols * tileSize.x); | |
var height:Int = Std.int(rows * tileSize.y); | |
// now we'll create result atlas and will blit every tile on it. | |
var combined:BitmapData = new BitmapData(width, height, true, FlxColor.TRANSPARENT); | |
var graphic:FlxGraphic = FlxG.bitmap.add(combined); | |
var result:FlxAtlasFrames = new FlxAtlasFrames(graphic); | |
var frames:FlxTileFrames; | |
var point:Point = new Point(0, 0); | |
for (bitmap in bitmaps) | |
{ | |
frames = FlxTileFrames.fromRectangle(bitmap, tileSize); | |
for (frame in frames.frames) | |
{ | |
frame.paint(combined, point, true); | |
result.addAtlasFrame(new FlxRect(point.x, point.y, tileSize.x, tileSize.y), new FlxPoint(tileSize.x, tileSize.y), new FlxPoint(0, 0)); | |
point.x += tileSize.x; | |
if (point.x >= combined.width) | |
{ | |
point.x = 0; | |
point.y += tileSize.y; | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment