source: nagslang/data.py@ 17:b0644173d0aa

Last change on this file since 17:b0644173d0aa was 17:b0644173d0aa, checked in by Stefano Rivera <stefano@…>, 8 years ago

Image loading

File size: 1.1 KB
Line 
1'''Simple data loader module.
2
3Loads data files from the "data" directory shipped with a game.
4'''
5
6import os
7
8import pygame
9
10data_py = os.path.abspath(os.path.dirname(__file__))
11data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
12
13
14def filepath(filename):
15 '''Determine the path to a file in the data directory.
16 '''
17 # Allow using / as separator in filenames
18 filename = os.path.join(*filename.split('/'))
19 return os.path.join(data_dir, filename)
20
21
22def load(filename, mode='rb'):
23 '''Open a file in the data directory.
24
25 "mode" is passed as the second arg to open().
26 '''
27 return open(os.path.join(data_dir, filename), mode)
28
29
30IMAGES = {}
31MUTATED_IMAGES = {}
32
33
34def load_image(filename, mutators=()):
35 if filename not in IMAGES:
36 image = pygame.image.load(filepath(filename))
37 image = image.convert_alpha(pygame.display.get_surface())
38 IMAGES[filename] = image
39
40 key = (filename, mutators)
41 if key not in MUTATED_IMAGES:
42 image = IMAGES[filename]
43 for mutator in mutators:
44 image = mutator(image)
45 MUTATED_IMAGES[key] = image
46
47 return MUTATED_IMAGES[key]
Note: See TracBrowser for help on using the repository browser.