comparison gamelib/data.py @ 154:ae6dff8ff88c

Cache loaded images
author Neil Muller <drnlmuller@gmail.com>
date Fri, 11 May 2012 20:32:50 +0200
parents 53277724645b
children
comparison
equal deleted inserted replaced
153:a644f6b64a6d 154:ae6dff8ff88c
10 from pygame import image 10 from pygame import image
11 11
12 12
13 data_py = os.path.abspath(os.path.dirname(__file__)) 13 data_py = os.path.abspath(os.path.dirname(__file__))
14 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data')) 14 data_dir = os.path.normpath(os.path.join(data_py, '..', 'data'))
15 IMAGE_CACHE = {}
15 16
16 17
17 def filepath(filename): 18 def filepath(filename):
18 '''Determine the path to a file in the data directory. 19 '''Determine the path to a file in the data directory.
19 ''' 20 '''
28 ''' 29 '''
29 return open(filepath(filename), mode) 30 return open(filepath(filename), mode)
30 31
31 32
32 def load_image(filename): 33 def load_image(filename):
33 return image.load(filepath(filename)) 34 if filename in IMAGE_CACHE:
35 return IMAGE_CACHE[filename]
36 else:
37 im = image.load(filepath(filename))
38 IMAGE_CACHE[filename] = im
39 return im