Work-in-progress general-purpose cacher for HaxeFlixel. Makes games speedy fast at the expense of memory usage.
ImageCache adapted from Kade Engine
ANOTHER NOTE: Psych Engine and any derivative mods of it contain a lot of existing things relating to image loading that are required to be changed in order for this to not be woefully inefficient.
new()
initializes ImageCache. Should always be attached to a public static variable, ideally either in Main.hx
or your own custom caching state.
cacheGraphic(path:String, extension:String = "png", ?starter:String = "")
caches a single image, from starter/path.extension
, with the key path
. You MUST NOT include the file extension, and you MUST include assets/
(or your asset folder name) in either path
or starter
. Feel free to include more of the path, depending on what you want to type into getGraphic
to get your graphic.
getGraphic(path:String)
either returns your cached FlxGraphic if it exists or null
if it doesn't. To retrieve the graphic, type what you typed into path
when you cached the graphic.
uncacheAllGraphics()
clears the image cache.
uncacheGraphic(tag:String)
uncaches the image at tag
if it is cached.
uncacheGraphicGroup(tag:Array<String>)
uncaches a group of images at the path specified in tag
if they are cached.
import flashcache.ImageCache;
import flashcache.SoundCache;
import flixel.text.FlxText;
import flixel.FlxState;
import flixel.FlxG;
import flixel.util.FlxColor;
import sys.FileSystem;
/*
Example caching state for FlashCache.
Will be updated over time.
*/
class CachingScreen extends FlxState {
// Both ImageCache and SoundCache have constructors that *must* be used before doing anything with them.
public static var imageCache:ImageCache = new ImageCache("assets/shared/images", "png");
public static var soundCache:SoundCache = new SoundCache();
public override function create() {
add(new FlxText(20, 20, 0, "Caching..." 32).setFormat('assets/fonts/segoe.ttf', FlxColor.WHITE, LEFT));
initCache();
super.create();
}
public static function initCache() {
for (i in FileSystem.readFileSystem.readDirectory(FileSystem.absolutePath("assets/shared/images"))
imageCache.cacheGraphic(i, true);
var sounds:String = FileSystem.readFileSystem.readDirectory(FileSystem.absolutePath("assets/shared/sounds");
soundCache.cacheSoundGroup(sounds);
FlxG.switchState(new TitleScreen());
}
}
var foo:FlxGraphic = CachingScreen.imageCache.getGraphic(path);
function cacheBackgrounds() {
CachingScreen.imageCache.cacheGraphic('bg/menuBackground', true);
CachingScreen.imageCache.cacheGraphic('bg/optionsBackground', true);
CachingScreen.imageCache.cacheGraphic('bg/gameBackground', true);
}
function cachePlayerGraphic() {
if (foo.skin == "bar") {
CachingScreen.imageCache.getGraphic('skins/bar');
}
}
function uncacheTiles() {
for (p in foo) {
CachingScreen.imageCache.uncacheGraphic(p);
}
}
function lowGraphicsCacheManagement() {
if (storeMemVarOrSomething > memLimit) {
CachingScreen.imageCache.uncacheAllGraphics();
}
}
inline function uncacheMenuSprites() {
CachingScreen.imageCache.uncacheGraphicGroup(menuSpritePaths);
}