Source

Fat x Fast / FatxFast / resource.py

# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"

import weakref
import os
import pygame
import pickle


def get_color(color):
    if isinstance(color, basestring):
        try:
            return pygame.color.THECOLORS[color]
        except KeyError:
            pass
    else:
        return color

class ResourceManager(object):
    
    def __init__(self, logger=None):
        self.resources = weakref.WeakValueDictionary()
        self.logger = logger

    def _get_loaded(self, key):
        try:
            return self.resources[key]
        except KeyError:
            pass

    def _get_loaded_set_unloaded(self, load_func, filename, *args):
        fullpath = self.get_resource_path(filename)
        key = hash((fullpath, args))
        resource = self._get_loaded(key)
        if not resource:
            resource = load_func(fullpath, *args)
            self.resources[key] = resource
        return resource

    @staticmethod
    def get_resource_path(resource_name):
        return os.path.join(os.path.dirname(__file__), "rsc", resource_name)
    
    def get_resource(self, filename):
        pass

    def _load_image(self, filename, colorkey=None):
        image = pygame.image.load(filename)
        image = image.convert()
        if colorkey:
            image.set_colorkey(colorkey, pygame.RLEACCEL)
        return image

    def load_image(self, filename, colorkey=None):
        return self._get_loaded_set_unloaded(
            self._load_image, filename, colorkey)

    def load_image_sheet(self, filename, tilewidth, tileheight, 
        colorkey=(0,0,0)): 
        image = self.load_image(filename, colorkey)
        width, height = image.get_size()
        tiles = []
        rect = pygame.Rect((0,0), (tilewidth, tileheight))
        for y in xrange(0, int(height/tileheight)):
            for x in xrange(0, int(width/tilewidth)):
                rect.topleft = (x*tilewidth, y*tileheight) 
                subsurf = image.subsurface(rect)
                tiles.append(subsurf)
        return tiles

    def load_font(self, filename, fontsize):
        return self._get_loaded_set_unloaded(
            pygame.font.Font, filename, fontsize)

    @staticmethod
    def open_supressed(filename, mode):
        try:
            return open(resource_manager.get_resource_path(filename), mode)
        except IOError:
            pass
    
    def load(self, filename): 
        #Try to load a file
        _file = self.open_supressed(filename, "rb")
        if _file:
            return pickle.load(_file)
        elif self.logger:
            self.logger.log_message(
                "Opening file {0} failed".format(filename))

    def save(self, filename, structe_to_save):
        _file = self.open_supressed(filename, "wb")
        if _file:
            pickle.dump(structe_to_save, _file)
        elif self.logger:
            self.logger.log_message(
                "Saving file {0} failed".format(filename))

pygame.color.THECOLORS["transblack-gui"] = (10,10,10,15)
pygame.color.THECOLORS["transperse"] = (35,42,55,150)
pygame.color.THECOLORS["grey29.5"] = (75,75,75,255)
pygame.color.THECOLORS["perse"] = (35,42,55,255)
resource_manager = ResourceManager()