comparison pytouhou/game/sprite.pyx @ 526:0b2a92a25245

Store data in C arrays in Sprite, and add an interface to access them as tuples.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 18:15:45 +0100
parents pytouhou/game/sprite.py@723a3e67a223
children dacdcca59b66
comparison
equal deleted inserted replaced
525:43ecf0f98f4d 526:0b2a92a25245
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 cdef class Sprite:
16 def __init__(self, width_override=0, height_override=0):
17 self.anm = None
18 self.removed = False
19 self.changed = True
20 self.visible = True
21
22 self.width_override = width_override
23 self.height_override = height_override
24 self.angle = 0
25 self.force_rotation = False
26
27 self.scale_interpolator = None
28 self.fade_interpolator = None
29 self.offset_interpolator = None
30 self.rotation_interpolator = None
31 self.color_interpolator = None
32
33 self.automatic_orientation = False
34
35 self.blendfunc = 0 # 0 = Normal, 1 = saturate #TODO: proper constants
36
37 self._texcoords[:] = [0, 0, 0, 0] # x, y, width, height
38 self._dest_offset[:] = [0., 0., 0.]
39 self.allow_dest_offset = False
40 self._texoffsets[:] = [0., 0.]
41 self.mirrored = False
42 self._rescale[:] = [1., 1.]
43 self._scale_speed[:] = [0., 0.]
44 self._rotations_3d[:] = [0., 0., 0.]
45 self._rotations_speed_3d[:] = [0., 0., 0.]
46 self.corner_relative_placement = False
47 self.frame = 0
48
49 # Cython treats unsigned char* variables as bytes, so we can’t use
50 # slicing here.
51 for i in xrange(4):
52 self._color[i] = 255
53
54 self._rendering_data = None
55
56
57 property scale_speed:
58 def __get__(self):
59 return (self._scale_speed[0], self._scale_speed[1])
60 def __set__(self, value):
61 self._scale_speed[:] = [value[0], value[1]]
62
63 property texoffsets:
64 def __get__(self):
65 return (self._texoffsets[0], self._texoffsets[1])
66 def __set__(self, value):
67 self._texoffsets[:] = [value[0], value[1]]
68
69 property rescale:
70 def __get__(self):
71 return (self._rescale[0], self._rescale[1])
72 def __set__(self, value):
73 self._rescale[:] = [value[0], value[1]]
74
75 property dest_offset:
76 def __get__(self):
77 return (self._dest_offset[0], self._dest_offset[1], self._dest_offset[2])
78 def __set__(self, value):
79 self._dest_offset[:] = [value[0], value[1], value[2]]
80
81 property rotations_speed_3d:
82 def __get__(self):
83 return (self._rotations_speed_3d[0], self._rotations_speed_3d[1], self._rotations_speed_3d[2])
84 def __set__(self, value):
85 self._rotations_speed_3d[:] = [value[0], value[1], value[2]]
86
87 property rotations_3d:
88 def __get__(self):
89 return (self._rotations_3d[0], self._rotations_3d[1], self._rotations_3d[2])
90 def __set__(self, value):
91 self._rotations_3d[:] = [value[0], value[1], value[2]]
92
93 property color:
94 def __get__(self):
95 return (self._color[0], self._color[1], self._color[2])
96 def __set__(self, value):
97 self._color[0] = value[0]
98 self._color[1] = value[1]
99 self._color[2] = value[2]
100
101 property alpha:
102 def __get__(self):
103 return self._color[3]
104 def __set__(self, value):
105 self._color[3] = value
106
107 property texcoords:
108 def __get__(self):
109 return (self._texcoords[0], self._texcoords[1], self._texcoords[2], self._texcoords[3])
110 def __set__(self, value):
111 self._texcoords[:] = [value[0], value[1], value[2], value[3]]
112
113
114 cpdef fade(self, unsigned long duration, alpha, formula=None):
115 self.fade_interpolator = Interpolator((self._color[3],), self.frame,
116 (alpha,), self.frame + duration,
117 formula)
118
119
120 cpdef scale_in(self, unsigned long duration, sx, sy, formula=None):
121 self.scale_interpolator = Interpolator(self.rescale, self.frame,
122 (sx, sy), self.frame + duration,
123 formula)
124
125
126 cpdef move_in(self, unsigned long duration, x, y, z, formula=None):
127 self.offset_interpolator = Interpolator(self.dest_offset, self.frame,
128 (x, y, z), self.frame + duration,
129 formula)
130
131
132 cpdef rotate_in(self, unsigned long duration, rx, ry, rz, formula=None):
133 self.rotation_interpolator = Interpolator(self.rotations_3d, self.frame,
134 (rx, ry, rz), self.frame + duration,
135 formula)
136
137
138 cpdef change_color_in(self, unsigned long duration, r, g, b, formula=None):
139 self.color_interpolator = Interpolator(self.color, self.frame,
140 (r, g, b), self.frame + duration,
141 formula)
142
143
144 cpdef update_orientation(self, double angle_base=0., bint force_rotation=False):
145 if (self.angle != angle_base or self.force_rotation != force_rotation):
146 self.angle = angle_base
147 self.force_rotation = force_rotation
148 self.changed = True
149
150
151 cpdef Sprite copy(self):
152 sprite = Sprite(self.width_override, self.height_override)
153
154 sprite.blendfunc = self.blendfunc
155 sprite.frame = self.frame
156 sprite.angle = self.angle
157
158 sprite.removed = self.removed
159 sprite.changed = self.changed
160 sprite.visible = self.visible
161 sprite.force_rotation = self.force_rotation
162 sprite.automatic_orientation = self.automatic_orientation
163 sprite.allow_dest_offset = self.allow_dest_offset
164 sprite.mirrored = self.mirrored
165 sprite.corner_relative_placement = self.corner_relative_placement
166
167 sprite.scale_interpolator = self.scale_interpolator
168 sprite.fade_interpolator = self.fade_interpolator
169 sprite.offset_interpolator = self.offset_interpolator
170 sprite.rotation_interpolator = self.rotation_interpolator
171 sprite.color_interpolator = self.color_interpolator
172
173 sprite.texcoords = self.texcoords
174 sprite.dest_offset = self.dest_offset
175 sprite.texoffsets = self.texoffsets
176 sprite.rescale = self.rescale
177 sprite.scale_speed = self.scale_speed
178 sprite.rotations_3d = self.rotations_3d
179 sprite.rotations_speed_3d = self.rotations_speed_3d
180 sprite.color = self.color
181
182 sprite.alpha = self.alpha
183 sprite.anm = self.anm
184 sprite._rendering_data = self._rendering_data
185
186 return sprite
187
188
189 cpdef update(self):
190 self.frame += 1
191
192 sax, say, saz = self._rotations_speed_3d[0], self._rotations_speed_3d[1], self._rotations_speed_3d[2] #XXX
193 if sax or say or saz:
194 ax, ay, az = self._rotations_3d[0], self._rotations_3d[1], self._rotations_3d[2] #XXX
195 self._rotations_3d[:] = [ax + sax, ay + say, az + saz]
196 self.changed = True
197 elif self.rotation_interpolator:
198 self.rotation_interpolator.update(self.frame)
199 self.rotations_3d = self.rotation_interpolator.values
200 self.changed = True
201
202 rsx, rsy = self._scale_speed[0], self._scale_speed[1] #XXX
203 if rsx or rsy:
204 rx, ry = self._rescale[0], self._rescale[1] #XXX
205 self._rescale[:] = [rx + rsx, ry + rsy]
206 self.changed = True
207
208 if self.fade_interpolator:
209 self.fade_interpolator.update(self.frame)
210 self._color[3] = self.fade_interpolator.values[0]
211 self.changed = True
212
213 if self.scale_interpolator:
214 self.scale_interpolator.update(self.frame)
215 self.rescale = self.scale_interpolator.values
216 self.changed = True
217
218 if self.offset_interpolator:
219 self.offset_interpolator.update(self.frame)
220 self.dest_offset = self.offset_interpolator.values
221 self.changed = True
222
223 if self.color_interpolator:
224 self.color_interpolator.update(self.frame)
225 self.color = self.color_interpolator.values
226 self.changed = True