Changeset 17:b0644173d0aa


Ignore:
Timestamp:
Sep 1, 2013, 12:36:18 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Message:

Image loading

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/data.py

    r8 r17  
    22
    33Loads 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.
    94'''
    105
    116import os
     7
     8import pygame
    129
    1310data_py = os.path.abspath(os.path.dirname(__file__))
     
    2926    '''
    3027    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 TracChangeset for help on using the changeset viewer.