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
13 from kivy.core.audio import SoundLoader
15 data_py = os.path.abspath(os.path.dirname(__file__))
16 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
17 loader = ImageLoader()
18 soundloader = SoundLoader()
21 def filepath(filename):
22 '''Determine the path to a file in the data directory.
24 filename = os.path.join(*filename.split('/'))
25 return os.path.join(data_dir, filename)
28 def load(filename, mode='rb'):
29 '''Open a file in the data directory.
31 "mode" is passed as the second arg to open().
33 return open(filepath(filename), mode)
36 def load_image(filename):
37 '''Load an image into a kivy texture. We rely on kivy's caching
39 return loader.load(filepath(filename))
42 def load_sound(filename):
43 return soundloader.load(filepath(filename))