Last active
August 29, 2015 14:08
-
-
Save danielbenmergui/35f466dbb346c2b98e6b to your computer and use it in GitHub Desktop.
Code I used to load images into bitmaps very quickly
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
private function load_frames(id:String, scalex:Float, scaley:Float = -1, fx:String = "default"):Array<Frame> | |
{ | |
if (scaley == -1) scaley = scalex; | |
scalex = Math.floor(scalex * 1000.0) / 1000.0; | |
scaley = Math.floor(scaley * 1000.0) / 1000.0; | |
// scan local cache first | |
var file_name = 'img_${id}_${scalex}_${scaley}_v1.anim'; | |
var file = cache_dir.resolvePath(file_name); | |
if (file.exists) | |
{ | |
var load_stream = new FileStream(); | |
load_stream.open(file, FileMode.READ); | |
var data = new ByteArray(); | |
load_stream.readBytes(data); | |
//data.uncompress(); | |
load_stream.close(); | |
// parse the cache | |
var frame_count = data.readInt(); | |
var ret = new Array<Frame>(); | |
for (i in 0...frame_count) | |
{ | |
var fr = new Frame(); | |
fr.offx = data.readInt(); | |
fr.offy = data.readInt(); | |
fr.originalw = data.readInt(); | |
fr.originalh = data.readInt(); | |
var bmpw = data.readInt(); | |
var bmph = data.readInt(); | |
fr.bmp = new BitmapData(bmpw, bmph, true, 0); | |
fr.bmp.setPixels(fr.bmp.rect, data); | |
ret.push(fr); | |
} | |
return ret; | |
} | |
else | |
{ | |
var mc = get_swf(id); | |
var ret = Utils.swf_to_frames(mc, true, scalex, scaley, fx); | |
// store in the cache | |
var data = new ByteArray(); | |
data.writeInt(ret.length); | |
for (i in 0...ret.length) | |
{ | |
var fr = ret[i]; | |
data.writeInt(Std.int(fr.offx)); | |
data.writeInt(Std.int(fr.offy)); | |
data.writeInt(Std.int(fr.originalw)); | |
data.writeInt(Std.int(fr.originalh)); | |
data.writeInt(fr.bmp.width); | |
data.writeInt(fr.bmp.height); | |
data.writeBytes(fr.bmp.getPixels(fr.bmp.rect)); | |
} | |
//data.compress(); | |
var save_stream = new FileStream(); | |
save_stream.open(file, FileMode.WRITE); | |
save_stream.writeBytes(data); | |
save_stream.close(); | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment