From e98761f7b9310668704a657126bddf051bafd2f0 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 20 Apr 2013 22:07:36 +0200 Subject: [PATCH] Actually use kivy's cache when loading resources --- erdslangetjie/data.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) 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) -- 2.34.1