import os
from kivy.core.image import ImageLoader
from kivy.core.audio import SoundLoader
+from kivy.cache import Cache
data_py = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
-loader = ImageLoader()
+imageloader = ImageLoader()
soundloader = SoundLoader()
+Cache.register('bane', limit=200)
def filepath(filename):
return open(filepath(filename), mode)
+def _cache_load(filename, loader):
+ '''Load an object either from disk or from the cache.
+
+ We rely on kivy's caching infrastructure'''
+ path = filepath(filename)
+ obj = Cache.get('bane', path)
+ if obj is None:
+ obj = loader.load(path)
+ Cache.append('bane', path, obj)
+ return obj
+
+
def load_image(filename):
- '''Load an image into a kivy texture. We rely on kivy's caching
- infrastructure'''
- return loader.load(filepath(filename))
+ return _cache_load(filename, imageloader)
def load_sound(filename):
- return soundloader.load(filepath(filename))
+ return _cache_load(filename, soundloader)