[as3] Fastest way for Flipping tiles horiz, vert, sprite sheets
The tilesheets I use only have top/left facing sets and I assume that you have to implement your own flipping functions or at least I think they are flipped, not rotated.
This is my current drawing function that does NOT flip. It gets information from a tile array, then copies a 16x16 tile from a spriteSheet onto the main bitmapdata. Tiles in the spriteSheet are numbered linearly from 0 and this number can be turned into row/col coordinates of the sheet with simple division.
Code:
private var rect:Rectangle = new Rectangle(0, 0, tileSize, tileSize);
private var p:Point = new Point();
override public function redraw() {
var t:TimeStuff = new TimeStuff();
var data:Array = currentMap.getData();
var sheets:Array = currentMap.getSheets()
var w:int = currentMap.getMapW();
var h:int = currentMap.getMapH();
//for (var z:int = 0; z < 20;z++){
for (var y:int = 0; y < h; y++) {
for (var x:int = 0; x < w; x++) {
var tile:Tile = data[0][y][x];
var sheet:int = tile.sheet;
rect.x = sheets[sheet].getTileCol(tile.sheetNumber) * tileSize;
rect.y = sheets[sheet].getTileRow(tile.sheetNumber) * tileSize;
p.x = x * tileSize;
p.y = y * tileSize;
layers[0].bitmapData.copyPixels(sheets[sheet].getBMD(), rect, p);
}
}
//}
//layers[0].bitmapData.unlock();
trace("t:" + t.timeElapsed());
}
Right now flipping is pretty expensive time wise. For example, doing the commented 20x loop above takes the same time it takes to flip every tile on the screen individually only once, mainly because I have to:
1. get original tile from sprite sheet
2. flip it onto a separate bitmapData with draw and edit: transformation matrix
3. return the flipped bitmapData
4. copyPixel the flipped bitmapData onto the screen
code for above... similar except rect does not change and getTile instead of getBMD is used:
Code:
public function redraw2() {
var t:TimeStuff = new TimeStuff();
var data:Array = currentMap.getData();
var sheets:Array = currentMap.getSheets()
var w:int = currentMap.getMapW();
var h:int = currentMap.getMapH();
//for (var z:int = 0; z < 50;z++){
for (var y:int = 0; y < h; y++) {
for (var x:int = 0; x < w; x++) {
var tile:Tile = data[0][y][x];
var sheet:int = tile.sheet;
p.x = x * tileSize;
p.y = y * tileSize;
layers[0].bitmapData.copyPixels(sheets[sheet].getTile(tile.sheetNumber, 0), rect, p);
}
}
//}
trace("t:" + t.timeElapsed());
}
flipping or no flipping code below
Code:
public function getTile(number:int, flip:int):BitmapData{
var row:int=number/width;
var col:int=number % width;
rect.y = row * tileSize;
rect.x = col * tileSize;
bmdt.copyPixels(bmd, rect, p);
if (flip == 0) {
bmdt2.draw(bmdt, flipVerticalMatrix);
return bmdt2;//flipped
}
return bmdt;//nonflipped
}
I don't think I can optimize the flipping much more without doing something completely different... such as copying the sprite sheet then flipping each tile individually(in code). Then it would just use a different sheet to copy if it needs to be flipped. The tradeoff is the memory of the spritesheet around 20-50 kb and the additional loading time for the map, which I guess shouldn't be too bad?
Anyone have any ideas? Cropping/creating right/bot facing tiles from the sets in photoshop is the last thing I want to do.