Skip to content

Commit

Permalink
Make BitmapData.fromBytes synchronous where possible (resolve #1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgranick committed May 8, 2017
1 parent 2054292 commit fbaed24
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
6 changes: 3 additions & 3 deletions openfl/_internal/macros/AssetsMacro.hx
Expand Up @@ -60,7 +60,7 @@ class AssetsMacro {

} else {

__fromBase64 (haxe.Resource.getString (resourceName), resourceType, function (b) {
__loadFromBase64 (haxe.Resource.getString (resourceName), resourceType).onComplete (function (b) {

if (preload == null) {

Expand Down Expand Up @@ -315,12 +315,12 @@ class AssetsMacro {
throw "not implemented";
#else
var byteArray = openfl.utils.ByteArray.fromBytes (haxe.Resource.getBytes (resourceName));
loadCompressedDataFromByteArray (byteArray, byteArray.length, forcePlayAsMusic);
loadCompressedDataFromByteArray (byteArray, byteArray.length);
#end

};

var args = [ { name: "stream", opt: true, type: macro :openfl.net.URLRequest, value: null }, { name: "context", opt: true, type: macro :openfl.media.SoundLoaderContext, value: null }, { name: "forcePlayAsMusic", opt: true, type: macro :Bool, value: macro false } ];
var args = [ { name: "stream", opt: true, type: macro :openfl.net.URLRequest, value: null }, { name: "context", opt: true, type: macro :openfl.media.SoundLoaderContext, value: null } ];
fields.push ({ name: "new", access: [ APublic ], kind: FFun({ args: args, expr: constructor, params: [], ret: null }), pos: Context.currentPos() });

#end
Expand Down
72 changes: 53 additions & 19 deletions openfl/display/BitmapData.hx
Expand Up @@ -1441,39 +1441,30 @@ class BitmapData implements IBitmapDrawable {

private inline function __fromBase64 (base64:String, type:String):Void {

Image.loadFromBase64 (base64, type).onComplete (function (image) {

__fromImage (image);

});
var image = Image.fromBase64 (base64, type);
__fromImage (image);

}


private inline function __fromBytes (bytes:ByteArray, rawAlpha:ByteArray = null):Void {

Image.loadFromBytes (bytes).onComplete (function (image) {

__fromImage (image);
var image = Image.fromBytes (bytes);
__fromImage (image);

if (rawAlpha != null) {

if (rawAlpha != null) {

__applyAlpha (rawAlpha);

}
__applyAlpha (rawAlpha);

});
}

}


private function __fromFile (path:String):Void {

Image.loadFromFile (path).onComplete (function (image) {

__fromImage (image);

});
var image = Image.fromFile (path);
__fromImage (image);

}

Expand Down Expand Up @@ -1520,6 +1511,49 @@ class BitmapData implements IBitmapDrawable {
}


private inline function __loadFromBase64 (base64:String, type:String):Future<BitmapData> {

return Image.loadFromBase64 (base64, type).then (function (image) {

__fromImage (image);
return Future.withValue (this);

});

}


private inline function __loadFromBytes (bytes:ByteArray, rawAlpha:ByteArray = null):Future<BitmapData> {

return Image.loadFromBytes (bytes).then (function (image) {

__fromImage (image);

if (rawAlpha != null) {

__applyAlpha (rawAlpha);

}

return Future.withValue (this);

});

}


private function __loadFromFile (path:String):Future<BitmapData> {

return Image.loadFromFile (path).then (function (image) {

__fromImage (image);
return Future.withValue (this);

});

}


private function __renderCairo (renderSession:RenderSession):Void {

#if lime_cairo
Expand Down

0 comments on commit fbaed24

Please sign in to comment.