1 '''Simple data loader module.
3 Loads data files from the "data" directory shipped with a game.
5 Enhancing this to handle caching etc. is left as an exercise for the reader.
7 Note that pyglet users should probably just add the data directory to the
8 pyglet.resource search path.
12 from kivy.core.image import ImageLoader
14 data_py = os.path.abspath(os.path.dirname(__file__))
15 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
16 loader = ImageLoader()
19 def filepath(filename):
20 '''Determine the path to a file in the data directory.
22 filename = os.path.join(*filename.split('/'))
23 return os.path.join(data_dir, filename)
26 def load(filename, mode='rb'):
27 '''Open a file in the data directory.
29 "mode" is passed as the second arg to open().
31 return open(filepath(filename), mode)
34 def load_image(filename):
35 '''Load an image into a kivy texture. We rely on kivy's caching
37 return loader.load(filepath(filename))