Last active
November 15, 2017 14:51
-
-
Save tiagolr/52e917037930ed7487e5 to your computer and use it in GitHub Desktop.
Embeding files load with Haxe + Openfl
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
<!-- Example on how to embed files --> | |
<haxeflag name="-resource" value="assets/[email protected]" /> | |
<haxeflag name="-resource" value="assets/[email protected]" /> |
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
import haxe.io.Bytes; | |
import haxe.Resource; | |
import openfl.Assets; | |
import openfl.display.Bitmap; | |
import openfl.display.BitmapData; | |
import openfl.display.Loader; | |
import openfl.utils.ByteArray; | |
/** | |
* Copied from Haxeui lib - loads image or text files, embed or not. | |
*/ | |
class ResourceManager { | |
public static function getText(resourceId:String):String { | |
var str:String = Resource.getString(resourceId); | |
if (str == null) { | |
str = Assets.getText(resourceId); | |
} | |
return str; | |
} | |
public static function getBitmapData(resourceId:String):BitmapData { | |
if (resourceId == null || resourceId.length == 0) { | |
return null; | |
} | |
var bmp:BitmapData = null; | |
#if !(flash) | |
var bytes:haxe.io.Bytes = Resource.getBytes(resourceId); | |
if (bytes != null) { | |
var ba:ByteArray = fromHaxeBytes(bytes); | |
var loader:Loader = new Loader(); | |
loader.loadBytes(ba); | |
if (loader.content != null) { | |
bmp = cast(loader.content, Bitmap).bitmapData; | |
} | |
} else { | |
bmp = Assets.getBitmapData(resourceId, true); | |
} | |
#else | |
bmp = Assets.getBitmapData(resourceId, true); | |
#end | |
return bmp; | |
} | |
static function fromHaxeBytes(bytes:Bytes):ByteArray { | |
var ba:ByteArray = new ByteArray(); | |
for (a in 0...bytes.length) { | |
ba.writeByte(Bytes.fastGet(bytes.getData(), a)); | |
} | |
return ba; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment