~alaxa27/ultimate-smash-friends/1.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
####################################################################################
# copyright 2008 Gabriel Pettier <gabriel.pettier@gmail.com>
#
# This file is part of UltimateSmashFriends
# 
# UltimateSmashFriends 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.
# 
# UltimateSmashFriends 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 UltimateSmashFriends.  If not, see <http://www.gnu.org/licenses/>.
##################################################################################

# standards imports
import os, sys
import pygame

# my imports
import loaders
import music
import animations
from pygame.locals import BLEND_MAX
import config
from debug_utils import LOG

def memoize(function):
    cache = {}
    def decorated_function(*args, **kwargs):
        params = (args)+tuple(zip(kwargs.keys(),kwargs.values()))
        try:
            return cache[params]
        except:
            val = function(*args, **kwargs)
            cache[params] = val
            return val
    return decorated_function

@memoize
def image(name, *args, **kwargs):
    """
    A function to load an image, shamelessly picked from pygame
    tutorial, and grossly adjusted for native transparency support of png.
    Can scale and reverse horizontaly an image, can produce a lightened version
    of an image.

    keywords arguments accepted are the followings:
        zoom=None, colorkey=None, server=False, lighten=False, reversed=False,
    """
    # FIXME: should not have to load the image in server mode, we just want
    # it's size!
    if 'reversed' in kwargs and kwargs['reversed'] == True:
        kwargs['reversed'] = False
        #LOG().log("reverse "+name)
        image = pygame.transform.flip(
            loaders.image(name,*args, **kwargs)[0],
            True, #flip horizontaly
            False #not verticaly
            )

    elif 'lighten' in kwargs and kwargs['lighten'] == True:
        #LOG().log('lightened: '+name)
        kwargs['lighten'] = False
        image = loaders.image(name, *args, **kwargs)[0].copy()
        image.fill(
                pygame.Color('lightgrey'),
                None,
                BLEND_MAX
                )

    elif 'zoom' in kwargs and kwargs['zoom'] not in (None, 1):
        zoom = kwargs['zoom']
        kwargs['zoom'] = None
        #LOG().log('scaling image '+name+' :'+str(zoom))
        image = pygame.transform.scale(
                loaders.image(name, **kwargs)[0],
                (
                 int(loaders.image(name, *args, **kwargs)[1][2]*zoom*
                     config.config['SIZE'][0]/800),
                 int(loaders.image(name, *args, **kwargs)[1][3]*zoom*
                     config.config['SIZE'][1]/480)
                )
                )
    else:
        try:
            image = pygame.image.load(name)
        except pygame.error, message:
            LOG().log('Cannot load image:'+str(name), 2)
            raise# SystemExit, message
        if 'colorkey' not in kwargs or kwargs['colorkey'] is None:
            image = image.convert_alpha()
        if 'colorkey' in kwargs and kwargs['colorkey'] is not None:
            image = image.convert()
            if colorkey is -1:
                colorkey = image.get_at((0,0))
            image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

@memoize
def track(name):
    return pygame.mixer.Sound(name)