Mercurial > touhou
comparison pytouhou/utils/interpolator.pyx @ 590:e15672733c93
Switch to Python 3.x instead of 2.7.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 30 Sep 2014 17:14:24 +0200 |
parents | d778db08190f |
children |
comparison
equal
deleted
inserted
replaced
589:0768122da817 | 590:e15672733c93 |
---|---|
20 unsigned long end_frame=0, formula=None): | 20 unsigned long end_frame=0, formula=None): |
21 self._length = len(values) | 21 self._length = len(values) |
22 self._values = <double*>malloc(self._length * sizeof(double)) | 22 self._values = <double*>malloc(self._length * sizeof(double)) |
23 self.start_values = <double*>malloc(self._length * sizeof(double)) | 23 self.start_values = <double*>malloc(self._length * sizeof(double)) |
24 self.end_values = <double*>malloc(self._length * sizeof(double)) | 24 self.end_values = <double*>malloc(self._length * sizeof(double)) |
25 for i in xrange(self._length): | 25 for i in range(self._length): |
26 self._values[i] = values[i] | 26 self._values[i] = values[i] |
27 self.start_values[i] = self._values[i] | 27 self.start_values[i] = self._values[i] |
28 if end_values is not None: | 28 if end_values is not None: |
29 for i in xrange(self._length): | 29 for i in range(self._length): |
30 self.end_values[i] = end_values[i] | 30 self.end_values[i] = end_values[i] |
31 self.start_frame = start_frame | 31 self.start_frame = start_frame |
32 self.end_frame = end_frame | 32 self.end_frame = end_frame |
33 self._frame = 0 | 33 self._frame = 0 |
34 self._formula = formula | 34 self._formula = formula |
40 free(self._values) | 40 free(self._values) |
41 | 41 |
42 | 42 |
43 property values: | 43 property values: |
44 def __get__(self): | 44 def __get__(self): |
45 return tuple([self._values[i] for i in xrange(self._length)]) | 45 return tuple([self._values[i] for i in range(self._length)]) |
46 | 46 |
47 | 47 |
48 def __nonzero__(self): | 48 def __nonzero__(self): |
49 return self._frame < self.end_frame | 49 return self._frame < self.end_frame |
50 | 50 |
51 | 51 |
52 cpdef set_interpolation_start(self, unsigned long frame, tuple values): | 52 cpdef set_interpolation_start(self, unsigned long frame, tuple values): |
53 for i in xrange(self._length): | 53 for i in range(self._length): |
54 self.start_values[i] = values[i] | 54 self.start_values[i] = values[i] |
55 self.start_frame = frame | 55 self.start_frame = frame |
56 | 56 |
57 | 57 |
58 cpdef set_interpolation_end(self, unsigned long frame, tuple values): | 58 cpdef set_interpolation_end(self, unsigned long frame, tuple values): |
59 for i in xrange(self._length): | 59 for i in range(self._length): |
60 self.end_values[i] = values[i] | 60 self.end_values[i] = values[i] |
61 self.end_frame = frame | 61 self.end_frame = frame |
62 | 62 |
63 | 63 |
64 cpdef set_interpolation_end_frame(self, unsigned long end_frame): | 64 cpdef set_interpolation_end_frame(self, unsigned long end_frame): |
65 self.end_frame = end_frame | 65 self.end_frame = end_frame |
66 | 66 |
67 | 67 |
68 cpdef set_interpolation_end_values(self, tuple values): | 68 cpdef set_interpolation_end_values(self, tuple values): |
69 for i in xrange(self._length): | 69 for i in range(self._length): |
70 self.end_values[i] = values[i] | 70 self.end_values[i] = values[i] |
71 | 71 |
72 | 72 |
73 cpdef update(self, unsigned long frame): | 73 cpdef update(self, unsigned long frame): |
74 cdef double coeff | 74 cdef double coeff |
75 | 75 |
76 self._frame = frame | 76 self._frame = frame |
77 if frame + 1 >= self.end_frame: #XXX: skip the last interpolation step | 77 if frame + 1 >= self.end_frame: #XXX: skip the last interpolation step |
78 # This bug is replicated from the original game | 78 # This bug is replicated from the original game |
79 for i in xrange(self._length): | 79 for i in range(self._length): |
80 self._values[i] = self.end_values[i] | 80 self._values[i] = self.end_values[i] |
81 self.start_values[i] = self.end_values[i] | 81 self.start_values[i] = self.end_values[i] |
82 self.start_frame = frame | 82 self.start_frame = frame |
83 else: | 83 else: |
84 coeff = float(frame - self.start_frame) / float(self.end_frame - self.start_frame) | 84 coeff = float(frame - self.start_frame) / float(self.end_frame - self.start_frame) |
85 if self._formula is not None: | 85 if self._formula is not None: |
86 coeff = self._formula(coeff) | 86 coeff = self._formula(coeff) |
87 for i in xrange(self._length): | 87 for i in range(self._length): |
88 start_value = self.start_values[i] | 88 start_value = self.start_values[i] |
89 end_value = self.end_values[i] | 89 end_value = self.end_values[i] |
90 self._values[i] = start_value + coeff * (end_value - start_value) | 90 self._values[i] = start_value + coeff * (end_value - start_value) |