I am loading my sprite images using SVG images so that they smoothly scale to match device resolutions. Currently I am naively rendering the SVG data for each sprite, but I would like to reduce memory overhead and improve performance by sharing the rendered image across multiple sprite instances.
How can this be achieved using OpenFL / Haxe?
For example:
The tile implementation below is wasteful since SVG image is rendered for each tile upon creation.
// Each of the following tile sprites contain copies of the same image.
var xyz1:Tile = new Tile("xyz");
var xyz2:Tile = new Tile("xyz");
var xyz3:Tile = new Tile("xyz");
Tile Implementation
package;
import flash.display.Shape;
import format.SVG;
import openfl.Assets;
class Tile extends Shape {
// Static cache of SVG data to avoid loading asset each time.
private static var tileImageMap:Map<String, SVG> = new Map<String, SVG>();
private static function lookupSVG(tile:String):SVG {
var svg:SVG = tileImageMap.get(tile);
if (svg == null) {
svg = new SVG(Assets.getText("img/" + tile + ".svg"));
tileImageMap.set(tile, svg);
}
return svg;
}
public var tile(get,set):String;
private var _tile:String;
private function get_tile():String {
return _tile;
}
private function set_tile(value:String):String {
if (value != _tile) {
_tile = value;
// Render tile SVG to tile sprite.
// How can this be cached and reused by multiple tile instances?
graphics.clear();
lookupSVG(value).render(graphics, 0, 0, 56, 56);
}
return _tile;
}
public function new(tile:String) {
super();
cacheAsBitmap = true;
this.tile = tile;
}
}