Mercurial > nagslang
changeset 17:b0644173d0aa
Image loading
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 01 Sep 2013 14:36:18 +0200 |
parents | fe1426d09074 |
children | 9ecb1d222ee0 |
files | nagslang/data.py |
diffstat | 1 files changed, 22 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/nagslang/data.py Sun Sep 01 14:21:13 2013 +0200 +++ b/nagslang/data.py Sun Sep 01 14:36:18 2013 +0200 @@ -1,15 +1,12 @@ '''Simple data loader module. Loads data files from the "data" directory shipped with a game. - -Enhancing this to handle caching etc. is left as an exercise for the reader. - -Note that pyglet users should probably just add the data directory to the -pyglet.resource search path. ''' import os +import pygame + data_py = os.path.abspath(os.path.dirname(__file__)) data_dir = os.path.normpath(os.path.join(data_py, '..', 'data')) @@ -28,3 +25,23 @@ "mode" is passed as the second arg to open(). ''' return open(os.path.join(data_dir, filename), mode) + + +IMAGES = {} +MUTATED_IMAGES = {} + + +def load_image(filename, mutators=()): + if filename not in IMAGES: + image = pygame.image.load(filepath(filename)) + image = image.convert_alpha(pygame.display.get_surface()) + IMAGES[filename] = image + + key = (filename, mutators) + if key not in MUTATED_IMAGES: + image = IMAGES[filename] + for mutator in mutators: + image = mutator(image) + MUTATED_IMAGES[key] = image + + return MUTATED_IMAGES[key]