Skip to content

Commit

Permalink
Provide callback to loadFromBytes to avoid race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
grumpytoad committed Apr 23, 2012
1 parent b098b58 commit 7135fce
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions jeash/display/BitmapData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ class BitmapData implements IBitmapDrawable {
return false;
}

public static function loadFromBytes(bytes:ByteArray) {
public static function loadFromBytes(bytes:ByteArray, onload:BitmapData->Void) {
// sanity check, must be a PNG or JPG.
var type = switch (true) {
case jeashIsPNG(bytes): "image/png";
Expand All @@ -800,13 +800,14 @@ class BitmapData implements IBitmapDrawable {
var bitmapData = new BitmapData(0, 0);

var canvas = bitmapData.mTextureBuffer;
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0);

bitmapData.jeashImageDataChanged = true;
var loader = function (_) {
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0);
onload(bitmapData);
}

return bitmapData;
if (img.width != 0) loader(null); else img.addEventListener("load", loader, false);
}

public function scroll(x:Int, y:Int)
Expand Down

0 comments on commit 7135fce

Please sign in to comment.