/
pygame_loaders.py
208 lines (185 loc) · 7.14 KB
/
pygame_loaders.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# copyright 2008 Gabriel Pettier <gabriel.pettier@gmail.com>
#
# This file is part of pygame_loaders
#
# pygame_loaders 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.
#
# pygame_loaders 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 pygame_loaders If not, see <http://www.gnu.org/licenses/>.
###############################################################################
# standards imports
import os, sys, copy
import pygame
import logging
import math
try:
from pygame.locals import BLEND_RGBA_MAX
from pygame.locals import BLEND_RGBA_ADD
from pygame.locals import BLEND_RGBA_SUB
from pygame.locals import BLEND_RGBA_MULT
from pygame.locals import BLEND_RGBA_MIN
except:
Log().log("old version of pygame no BLEND_RGBA_MAX")
BLEND_RGBA_MAX = None
def memoize(function):
"""
Any function decorated with memoize will cache it's results and send them
directly when called with same parameters as before, without calling the
actual code, please only use with functions which result depend only of
parameters (not time, state of the game or such).
"""
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
decorated_function.__name__ = function.__name__
decorated_function.__doc__ = function.__doc__
decorated_function.__dict__.update(function.__dict__)
return decorated_function
@memoize #the memoize is critical for performances!
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 'nodisplay' in kwargs and kwargs['nodisplay']:
return None, pygame.Rect((0, 0), pygame.image.load(name).get_size())
if 'reversed' in kwargs and kwargs['reversed']:
kwargs['reversed'] = False
#logging.debug("reverse "+name)
img = pygame.transform.flip(
image(name,*args, **kwargs)[0],
True, #flip horizontaly
False #not verticaly
)
elif 'lighten' in kwargs and kwargs['lighten']:
#logging.debug('lightened: '+name)
kwargs['lighten'] = False
img = image(name, *args, **kwargs)[0].copy()
if BLEND_RGBA_MAX is not None:
img.fill(
pygame.Color('lightgrey'),
None,
BLEND_RGBA_MAX
)
else:
# this mean this version of pygame is to old to use the effect
# above, an equivalent method would be a good thing
logging.warning('pygame version < 1.9 no alpha blend.')
pass #nothing for the moment
elif 'alpha' in kwargs and kwargs['alpha'] is not None:
alpha = kwargs['alpha']
if not 0 <= alpha <= 1:
logging.warning('bad alpha value: %s' % alpha)
alpha = min(1, max(0, alpha))
kwargs['alpha'] = None
img = image(name, *args, **kwargs)[0].copy()
img.fill(
pygame.Color(255, 255, 255, int(alpha*255)),
image(name, *args, **kwargs)[1],
BLEND_RGBA_MULT
)
elif 'scale' in kwargs and kwargs['scale'] is not None:
if len(kwargs['scale']) is not 2:
raise Exception.ValueError(
"scale parameter should be a tuple of two integers"
)
scale = kwargs['scale']
kwargs['scale'] = None
try:
img = pygame.transform.smoothscale(
image(name, *args, **kwargs)[0],
scale
)
except:
img = pygame.transform.scale(
image(name, *args, **kwargs)[0],
scale
)
elif 'zoom' in kwargs and kwargs['zoom'] not in (None, 1):
zoom = kwargs['zoom']
kwargs['zoom'] = None
#logging.debug('scaling image '+name+' :'+str(zoom))
try:
img = pygame.transform.smoothscale(
image(name, **kwargs)[0],
(
int(image(name, *args, **kwargs)[1][2]*zoom),
int(image(name, *args, **kwargs)[1][3]*zoom)
)
)
except:
img = pygame.transform.scale(
image(name, **kwargs)[0],
(
int(image(name, *args, **kwargs)[1][2]*zoom),
int(image(name, *args, **kwargs)[1][3]*zoom)
)
)
elif 'rotate' in kwargs and kwargs['rotate'] not in (None, 0):
angle = kwargs['rotate']
kwargs['rotate'] = None
img = pygame.transform.rotate(image(name, **kwargs)[0], angle *180/math.pi)
else:
try:
img = pygame.image.load(name)
except pygame.error, message:
logging.debug('Cannot load image:'+str(name), 2)
raise# SystemExit, message
img = img.convert_alpha()
return img, img.get_rect()
@memoize
def image_layer(first, second, pos=(0,0)):
surface = copy.copy(first)
surface.blit(second, pos)
return surface
@memoize
def text(text_send, font, r=240, g=240, b=240, a=250):
return font.render(text_send.decode('utf-8'),
True,
pygame.color.Color(r, g, b, a))
@memoize
def paragraph(text_send, font):
max_len = text("", font)
for texte in text_send.split('\n'):
if text(texte, font).get_width() > max_len.get_width():
max_len = text(texte, font)
text_re = pygame.surface.Surface((max_len.get_width(), len(text_send.split('\n'))*text("", font).get_height()))
i = -1
for texte in text_send.split('\n'):
i += 1
surf = text(texte, font)
text_re.blit(surf, (text_re.get_width()/2 - surf.get_width()/2, i*surf.get_height()))
return text_re
@memoize
def track(name):
try:
FREQ, BITSIZE, CHANNELS, BUFFER = (44100, -16, 2, 1024)
pygame.mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
return pygame.mixer.Sound(name)
except (pygame.error):
# no sound
logging.info("Unable to initialize audio.")
return None