GithubHelp home page GithubHelp logo

flashcache's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.