From: Neil Date: Sat, 20 Apr 2013 20:07:36 +0000 (+0200) Subject: Actually use kivy's cache when loading resources X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=e98761f7b9310668704a657126bddf051bafd2f0;p=erdslangetjie.git Actually use kivy's cache when loading resources --- diff --git a/erdslangetjie/data.py b/erdslangetjie/data.py index ad42ff3..2ee8691 100644 --- a/erdslangetjie/data.py +++ b/erdslangetjie/data.py @@ -11,11 +11,13 @@ pyglet.resource search path. 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): @@ -33,11 +35,21 @@ def load(filename, mode='rb'): 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)