add basic player sprites
[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
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()
17
18
19 def filepath(filename):
20     '''Determine the path to a file in the data directory.
21     '''
22     filename = os.path.join(*filename.split('/'))
23     return os.path.join(data_dir, filename)
24
25
26 def load(filename, mode='rb'):
27     '''Open a file in the data directory.
28
29     "mode" is passed as the second arg to open().
30     '''
31     return open(filepath(filename), mode)
32
33
34 def load_image(filename):
35     '''Load an image into a kivy texture. We rely on kivy's caching
36        infrastructure'''
37     return loader.load(filepath(filename))