Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions src/flashcache/ImageCache.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flashcache;
import openfl.display.BitmapData;
import flixel.graphics.FlxGraphic;
import openfl.display3D.textures.RectangleTexture;
using StringTools;

/*
Expand Down Expand Up @@ -48,20 +49,29 @@ class ImageCache {
* @param makeOnlyPathName determines whether or not the image key includes assetPath and the file extension.
*/

public function cacheGraphic(path:String, ?makeOnlyPathName:Bool = false):FlxGraphic {
public function cacheGraphic(path:String, ?makeOnlyPathName:Bool = false, ?GPUCache:Bool = false):FlxGraphic {
var data:BitmapData;
var epicPath:String = assetPath + (assetPath == "" ? "" : "/") + path + '.' + extension;

if (cacheFlxGraphic.exists(makeOnlyPathName ? path : epicPath)) {
return null; // prevents duplicates
}

try (data = BitmapData.fromFile(epicPath))
catch(e) {
trace("Error loading image: " + path);
try (data = BitmapData.fromFile(epicPath))
catch(e) {
trace("Error loading image: " + path);
return null;
}

}

if (data != null && GPUCache){
var texture:RectangleTexture = FlxG.stage.context3D.createRectangleTexture(data.width, data.height, BGRA, true);
texture.uploadFromBitmapData(data);
data.image.data = null;
data.dispose();
data.disposeImage();
data = BitmapData.fromTexture(texture);

}
var graphic:FlxGraphic = FlxGraphic.fromBitmapData(data);
graphic.persist = true;
graphic.destroyOnNoUse = false;
Expand Down
14 changes: 11 additions & 3 deletions src/flashcache/SoundCache.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package flashcache;
import openfl.Assets;
import openfl.media.Sound;
import openfl.utils.AssetType;
import lime.media.vorbis.VorbisFile;
import lime.media.AudioBuffer;

/*
DO NOT USE THIS IT'S STUPID
Expand All @@ -12,9 +14,15 @@ import openfl.utils.AssetType;
*/
class SoundCache {
public function new() {}
public function cacheSound(path:String):Sound {
public function cacheSound(path:String, ?stream:Bool = false):Sound {
if (Assets.exists(path, AssetType.SOUND) || Assets.exists(path, AssetType.MUSIC))
return Assets.getSound(path, true);
var sound:Sound = null;
#if lime_vorbis
if (stream)
return sound = Sound.fromAudioBuffer(AudioBuffer.fromVorbisFile(VorbisFile.fromFile(path));
else
#end
return sound = Assets.getSound(path, true);
trace('Sound not found at ' + path);
return null;
}
Expand All @@ -23,4 +31,4 @@ class SoundCache {
cacheSound(i);
}
}
}
}
6 changes: 4 additions & 2 deletions src/flashcache/TypedCache.hx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class TypedCache<T>
{
for (p in 0...keys.length)
{
uncacheFunction(cache.get(keys[p]));
if (uncacheFunction != null)
uncacheFunction(cache.get(keys[p]));
cache.exists(keys[p]) ? cache.remove(keys[p]) : null;
}
}
Expand All @@ -100,7 +101,8 @@ class TypedCache<T>
public inline function uncacheAll(uncacheFunction:T->Void)
{
for (item in cache)
uncacheFunction(item);
if (uncacheFunction != null)
uncacheFunction(item);
cache.clear();
}

Expand Down