From AssetPaths (String) to FlxSprite to BitmapData to String... Can it be simplified?

  • So I'm converting an image inside the AssetPaths into a string for doing things in my game.
    Basicaly, I create a temporary FlxSprite to load the graphic from the AssetPaths, then I steal the BitmapData from it in order to convert it into a String. While everything is working nicely, I was wondering if there was a simplier way to do that...
    Here's my code:

    // Retrieve BitmapData from AssetPaths' String
    public static function getBitmapData(string:String):BitmapData
    	var sprite:FlxSprite = new FlxSprite();
    	return sprite.pixels;
    // Convert BitmapData to String
    public static function serializeBitmapData(bmp:BitmapData):String
    	var bmp_bytes:Bytes = Bytes.ofData(PNGEncoder.encode(bmp));
    	var serializer = new Serializer();
    	return serializer.toString();
    // Here, I convert myImage.png to String
    var bmp:BitmapData = getBitmapData(AssetPaths.myImage__png);
    var str:String = serializeBitmapData(bmp);

    I'm sure I'm doing too much here...

  • It's something like:

    #if openfl_legacy
    			var bytes = ByteArray.readFile(fullPath);
    			var bytes = File.getBytes(fullPath);

    Then there should be some method to convert to Base64, or use static method run() of Serializer class.
    I'm not sure what you want to achieve by loading it as BitmapData or encoding to PNG, since any binary data can be converted to some sort of String

  • Thanks @starry-abyss
    I'm using that in order to store user images inside a FlxSave ( but I also have to store some images that are in my AssetPaths.
    I'm not familiar AT ALL with bytes, binary data and Base64 stuff... I wrote my thing like that, it's long and redundant but it's working. I wish it was as simple as you say, but ByteArray.readFile don't exist.

  • If you're on -Dnext, then use File.getBytes. My code snippet supports legacy and next at the same time

  • @Txori My bad, the solution won't work for embedded assets. Apparently,

    var bytes = Assets.getBytes(path);

    is the way to go.

  • Thanks :)
    So I was playing around with File:getBytes, which retrieves a ByteArray, so I converted it to Bytes.

    var byteArray:ByteArray = Assets.getBytes(path);
    var bytes:Bytes = Bytes.ofData(byteArray);

    So I converted that image to three different kind of strings:

    alt text

    trace( Base64.encode(bytes) );


    960 characters

    trace( );


    965 characters

    trace( serializeBitmapData(sprite.pixels) ); (Using the code I wrote in my first post)


    371 characters

    So at first sight, the Serializer method seems more interesting for storing the multiple user images that I'm going to have on my server. Even if the method is kind of dumb... But I may be wrong.

  • Also, you could possibly just store data as bytes on the server, and not as strings. But I believe you have some reason against doing it?

  • I'll try that. At first, I went with stringify the BitmapData because the savefile can't handle those. Then I stored every parameters of the player, including his stringified image, inside a generated json file. If I can't store the bytes in the savefile or in the json, it's no use. But I'll try, for the knowlegde ;)

  • Yes, for putting inside JSON, you need a text string. PNG encoding makes sense to me now too.
    BTW, I noticed that serializer output and Base64 are almost the same, they use same letters, different special characters, and serializer prepends type and length to the string. Base64 (I mean PNG -> Base64 for small size) format is known and supported outside Haxe too, but it's up to you. :-)

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.