Hook up some basic sound support
[erdslangetjie.git] / erdslangetjie / data.py
1 '''Simple data loader module.
2
3 Loads data files from the "data" directory shipped with a game.
4
5 Enhancing this to handle caching etc. is left as an exercise for the reader.
6
7 Note that pyglet users should probably just add the data directory to the
8 pyglet.resource search path.
9 '''
10
11 import os
12 from kivy.core.image import ImageLoader
13 from kivy.core.audio import SoundLoader
14
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()
19
20
21 def filepath(filename):
22     '''Determine the path to a file in the data directory.
23     '''
24     filename = os.path.join(*filename.split('/'))
25     return os.path.join(data_dir, filename)
26
27
28 def load(filename, mode='rb'):
29     '''Open a file in the data directory.
30
31     "mode" is passed as the second arg to open().
32     '''
33     return open(filepath(filename), mode)
34
35
36 def load_image(filename):
37     '''Load an image into a kivy texture. We rely on kivy's caching
38        infrastructure'''
39     return loader.load(filepath(filename))
40
41
42 def load_sound(filename):
43     return soundloader.load(filepath(filename))