pygame is
Python
Simple DirectMedia Layer
 
 
pygame.org is
Site Swing
Wiki

LazyImageLoading

      
Search:  
 
 
import pygame
import weakref
 
class ResourceController(object):
    def __init__(self, loader):
        self.__dict__.update(dict(
            names = {},
            cache = weakref.WeakValueDictionary(),
            loader = loader
        ))
        
    def __setattr__(self, name, value):
        self.names[name] = value
        
    def __getattr__(self, name):
        try:
            img = self.cache[name]
        except KeyError:
            img = self.loader(self.names[name])
            self.cache[name] = img
        return img
        
    
class ImageController(ResourceController):
    def __init__(self):
        ResourceController.__init__(self, pygame.image.load)

Use the image controller to prepare images for loading, without actually loading them.

img_collection = ImageController()
img_collection.background = 'background.png'

When you first reference an item in an ImageController instance, it is loaded and returned. While a reference is kept to the image, it remains available in the ImageController. When the image no longer has any active references, it is removed from memory, and will be reloaded next time it is referenced.

# the controller loads and returns the image surface
image = img_collection.background
 
# the same reference is returned
another_image = img_collection.background
 
# when image and another_image go out of scope (garbage collected)
# the surface is unloaded. 

You could also subclass ResourceController to provide automatic resource mangement for other resources, such as sounds.

spotlight

 
our projects
pygame.org welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
 
recent releases
Sep 9, 2014

Sep 8, 2014

Sep 7, 2014


Sep 5, 2014

Aug 26, 2014

Aug 22, 2014

Aug 21, 2014


Aug 18, 2014

Aug 2, 2014


... more!
 
for pygame related questions, comments, and suggestions, please see help (lists, irc)