Last active
August 29, 2015 14:25
-
-
Save larsiusprime/9156c0d7b5d357db9413 to your computer and use it in GitHub Desktop.
Repro scaling issues in flixel-ui with OpenFL-next
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
package; | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.Sprite; | |
import openfl.Assets; | |
import openfl.geom.Rectangle; | |
import openfl.geom.Point; | |
import openfl.geom.Matrix; | |
class Main extends Sprite { | |
public function new () { | |
super (); | |
var asset = "assets/buttonstack.png"; | |
var scale = 0.5; | |
var origW = 72; | |
var origH = 288; | |
var finalW = Std.int(origW * scale); | |
var finalH = Std.int(origH * scale); | |
var smooth = true; | |
var bitmap = new Bitmap (Assets.getBitmapData (asset)); | |
addChild (bitmap); | |
var bitmap2 = new Bitmap (scaleTileBmp(asset, scale, origW, origH, finalW, finalH, smooth)); | |
addChild(bitmap2); | |
bitmap2.x = 100; | |
asset = "assets/buttonstack2.png"; | |
origW = 256; | |
origH = 256; | |
finalW = Std.int(origW * scale); | |
finalH = Std.int(origW * scale); | |
var bitmap3 = new Bitmap (Assets.getBitmapData (asset)); | |
addChild (bitmap3); | |
bitmap3.x = 300; | |
var bitmap4 = new Bitmap (scaleTileBmp(asset, scale, origW, origH, finalW, finalH, smooth)); | |
addChild(bitmap4); | |
bitmap4.x = 600; | |
} | |
/** | |
* This scales an image that contains tiles, being super OCD about it, making sure each tile is | |
* properly scaled and put in the correct position | |
* @param orig_id asset id | |
* @param scale the scale factor | |
* @param origW original width of the tile | |
* @param origH original height of the tile | |
* @param W final width of the tile | |
* @param H final height of the tile | |
* @param smooth | |
* @return | |
*/ | |
public static function scaleTileBmp(orig_id:String, scale:Float, origW:Int, origH:Int, W:Int = -1, H:Int = -1, smooth:Bool = true):BitmapData | |
{ | |
var orig:BitmapData = Assets.getBitmapData(orig_id, false); | |
var widthInTiles:Int = Std.int(orig.width / origW); | |
var heightInTiles:Int = Std.int(orig.height / origH); | |
//if W and H are not provided, infer the correct size | |
if (W == -1) | |
{ | |
W = Std.int(origW * scale); | |
} | |
if (H == -1) | |
{ | |
H = Std.int(origH * scale); | |
scale = H / origH; | |
} | |
if (Math.abs(scale - 1.0) > 0.001) | |
{ | |
var scaled:BitmapData = new BitmapData(Std.int(W*widthInTiles), Std.int(H*heightInTiles), true, 0x00000000); | |
var rect:Rectangle = new Rectangle(); | |
var pt:Point = new Point(); | |
var matrix:Matrix = new Matrix(); | |
matrix.scale(scale, scale); | |
for (tiley in 0...heightInTiles) | |
{ | |
for (tilex in 0...widthInTiles) | |
{ | |
var tile:BitmapData = new BitmapData(origW, origH, true, 0x00000000); | |
rect.setTo(tilex * origW, tiley * origH, origW, origH); | |
pt.setTo(0, 0); | |
tile.copyPixels(orig, rect, pt); | |
var scaleTile:BitmapData = new BitmapData(W, H, true, 0x000000); | |
scaleTile.draw(tile, matrix, null, null, null, smooth); | |
pt.setTo(tilex * W, tiley * H); | |
scaled.copyPixels(scaleTile, scaleTile.rect, pt); | |
} | |
} | |
return scaled; | |
} | |
else | |
{ | |
return orig.clone(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment