~alaxa27/ultimate-smash-friends/1.0

« back to all changes in this revision

Viewing changes to Debian/ultimate-smash-friends_1.0-beta-1/usr/lib/pymodules/python2.6/usf_modules/loaders.py

  • Committer: Alaxa27
  • Date: 2009-12-02 14:09:24 UTC
  • Revision ID: alaxa27@gmail.com-20091202140924-ckfr9dnurd2mql0d
Adding usf_modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
####################################################################################
 
3
# copyright 2008 Gabriel Pettier <gabriel.pettier@gmail.com>
 
4
#
 
5
# This file is part of UltimateSmashFriends
 
6
 
7
# UltimateSmashFriends is free software: you can redistribute it and/or modify
 
8
# it under the terms of the GNU General Public License as published by
 
9
# the Free Software Foundation, either version 3 of the License, or
 
10
# (at your option) any later version.
 
11
 
12
# UltimateSmashFriends is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with UltimateSmashFriends.  If not, see <http://www.gnu.org/licenses/>.
 
19
##################################################################################
 
20
 
 
21
# standards imports
 
22
import os, sys
 
23
import pygame
 
24
 
 
25
# my imports
 
26
import loaders
 
27
import music
 
28
import animations
 
29
from pygame.locals import BLEND_MAX
 
30
import config
 
31
from debug_utils import LOG
 
32
 
 
33
def memoize(function):
 
34
    cache = {}
 
35
    def decorated_function(*args, **kwargs):
 
36
        params = (args)+tuple(zip(kwargs.keys(),kwargs.values()))
 
37
        try:
 
38
            return cache[params]
 
39
        except:
 
40
            val = function(*args, **kwargs)
 
41
            cache[params] = val
 
42
            return val
 
43
    return decorated_function
 
44
 
 
45
@memoize
 
46
def image(name, *args, **kwargs):
 
47
    """
 
48
    A function to load an image, shamelessly picked from pygame
 
49
    tutorial, and grossly adjusted for native transparency support of png.
 
50
    Can scale and reverse horizontaly an image, can produce a lightened version
 
51
    of an image.
 
52
 
 
53
    keywords arguments accepted are the followings:
 
54
        zoom=None, colorkey=None, server=False, lighten=False, reversed=False,
 
55
    """
 
56
    # FIXME: should not have to load the image in server mode, we just want
 
57
    # it's size!
 
58
    if 'reversed' in kwargs and kwargs['reversed'] == True:
 
59
        kwargs['reversed'] = False
 
60
        #LOG().log("reverse "+name)
 
61
        image = pygame.transform.flip(
 
62
            loaders.image(name,*args, **kwargs)[0],
 
63
            True, #flip horizontaly
 
64
            False #not verticaly
 
65
            )
 
66
 
 
67
    elif 'lighten' in kwargs and kwargs['lighten'] == True:
 
68
        #LOG().log('lightened: '+name)
 
69
        kwargs['lighten'] = False
 
70
        image = loaders.image(name, *args, **kwargs)[0].copy()
 
71
        image.fill(
 
72
                pygame.Color('lightgrey'),
 
73
                None,
 
74
                BLEND_MAX
 
75
                )
 
76
 
 
77
    elif 'zoom' in kwargs and kwargs['zoom'] not in (None, 1):
 
78
        zoom = kwargs['zoom']
 
79
        kwargs['zoom'] = None
 
80
        #LOG().log('scaling image '+name+' :'+str(zoom))
 
81
        image = pygame.transform.scale(
 
82
                loaders.image(name, **kwargs)[0],
 
83
                (
 
84
                 int(loaders.image(name, *args, **kwargs)[1][2]*zoom*
 
85
                     config.config['SIZE'][0]/800),
 
86
                 int(loaders.image(name, *args, **kwargs)[1][3]*zoom*
 
87
                     config.config['SIZE'][1]/480)
 
88
                )
 
89
                )
 
90
    else:
 
91
        try:
 
92
            image = pygame.image.load(name)
 
93
        except pygame.error, message:
 
94
            LOG().log('Cannot load image:'+str(name), 2)
 
95
            raise# SystemExit, message
 
96
        if 'colorkey' not in kwargs or kwargs['colorkey'] is None:
 
97
            image = image.convert_alpha()
 
98
        if 'colorkey' in kwargs and kwargs['colorkey'] is not None:
 
99
            image = image.convert()
 
100
            if colorkey is -1:
 
101
                colorkey = image.get_at((0,0))
 
102
            image.set_colorkey(colorkey, RLEACCEL)
 
103
    return image, image.get_rect()
 
104
 
 
105
@memoize
 
106
def track(name):
 
107
    return pygame.mixer.Sound(name)