FlashCache

Image cacher for HaxeFlixel. Makes games speedy fast.
https://github.com/sayofthelor/FlashCache/

To install, run:

haxelib install FlashCache 2.0.0 

See using Haxelib in Haxelib documentation for more information.

README.md

FlashCache

Work-in-progress general-purpose cacher for HaxeFlixel. Makes games speedy fast.

Note: This uses a hell of a lot of memory if you cache a lot of stuff, so it's a good idea to make this optional if you are.

ImageCache adapted from Kade Engine

Currently functional: ImageCache, SoundCache (partially)

KEEP IN MIND, FUNCTIONS MAY CHANGE! WE AREN'T RESPONSIBLE IF THIS BREAKS YOUR CODE.

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.

Functions (ImageCache)

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.

Another Note: Don't clear memory when this is active or else there is literally no point.

Example State

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());
    }
}

Example of Loading a Cached Graphic

function getImage(path:String):FlxGraphic {
    var foo:FlxGraphic = CachingScreen.imageCache.getGraphic(path);
    if (foo != null) return foo;
    else {
        return null;
    }
}

Example of Caching a Graphic

function cacheBackgrounds() {
    CachingScreen.imageCache.cacheGraphic('bg/menuBackground', true);
    CachingScreen.imageCache.cacheGraphic('bg/optionsBackground', true);
    CachingScreen.imageCache.cacheGraphic('bg/gameBackground', true);
}

Example of Getting a Cached Graphic

function cachePlayerGraphic() {
    if (foo.skin == "bar") {
        CachingScreen.imageCache.getGraphic('skins/bar');
    }
}

Example of Uncaching a Graphic

function uncacheTiles() {
    for (p in foo) {
        CachingScreen.imageCache.uncacheGraphic(p);
    }
}

Example of Uncaching All Graphics

function lowGraphicsCacheManagement() {
    if (storeMemVarOrSomething > memLimit) {
        CachingScreen.imageCache.uncacheAllGraphics();
    }
}

Example of Uncaching a Graphic Group

inline function uncacheMenuSprites() {
    CachingScreen.imageCache.uncacheGraphicGroup(menuSpritePaths);
}
Contributors
sayofthelor
vidyagirl
Version
2.0.0
Published
3 years ago
Dependencies
License
GPL

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub