comparison pytouhou/game/sprite.py @ 108:2a03940deea3

Move everything graphical to pytouhou.opengl!
author Thibaut Girka <thib@sitedethib.com>
date Tue, 06 Sep 2011 00:26:13 +0200
parents ac2e5e1c2c3c
children 4300a832f033
comparison
equal deleted inserted replaced
107:5d9052b9a4e8 108:2a03940deea3
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from math import pi
16 15
17 from pytouhou.utils.matrix import Matrix
18 from pytouhou.utils.interpolator import Interpolator 16 from pytouhou.utils.interpolator import Interpolator
19 17
20 18
21 class Sprite(object): 19 class Sprite(object):
22 def __init__(self): 20 def __init__(self):
48 self.rotations_speed_3d = (0., 0., 0.) 46 self.rotations_speed_3d = (0., 0., 0.)
49 self.corner_relative_placement = False 47 self.corner_relative_placement = False
50 self.frame = 0 48 self.frame = 0
51 self.color = (255, 255, 255) 49 self.color = (255, 255, 255)
52 self.alpha = 255 50 self.alpha = 255
53 self._uvs = [] 51
54 self._vertices = [] 52 self._rendering_data = None
55 self._colors = []
56 53
57 54
58 def fade(self, duration, alpha, formula): 55 def fade(self, duration, alpha, formula):
59 if not self.fade_interpolator: 56 if not self.fade_interpolator:
60 self.fade_interpolator = Interpolator((self.alpha,), formula) 57 self.fade_interpolator = Interpolator((self.alpha,), formula)
72 def move_in(self, duration, x, y, z, formula): 69 def move_in(self, duration, x, y, z, formula):
73 if not self.offset_interpolator: 70 if not self.offset_interpolator:
74 self.offset_interpolator = Interpolator(self.dest_offset, formula) 71 self.offset_interpolator = Interpolator(self.dest_offset, formula)
75 self.offset_interpolator.set_interpolation_start(self.frame, self.dest_offset) 72 self.offset_interpolator.set_interpolation_start(self.frame, self.dest_offset)
76 self.offset_interpolator.set_interpolation_end(self.frame + duration - 1, (x, y, z)) 73 self.offset_interpolator.set_interpolation_end(self.frame + duration - 1, (x, y, z))
77
78
79 def update_vertices_uvs_colors(self):
80 if not self._changed:
81 return
82
83 if self.fade_interpolator:
84 self.fade_interpolator.update(self.frame)
85 self.alpha = int(self.fade_interpolator.values[0])
86
87 if self.scale_interpolator:
88 self.scale_interpolator.update(self.frame)
89 self.rescale = self.scale_interpolator.values
90
91 if self.offset_interpolator:
92 self.offset_interpolator.update(self.frame)
93 self.dest_offset = self.offset_interpolator.values
94
95 vertmat = Matrix([[-.5, .5, .5, -.5],
96 [-.5, -.5, .5, .5],
97 [ .0, .0, .0, .0],
98 [ 1., 1., 1., 1.]])
99
100 tx, ty, tw, th = self.texcoords
101 sx, sy = self.rescale
102 width = self.width_override or (tw * sx)
103 height = self.height_override or (th * sy)
104
105 vertmat.scale2d(width, height)
106 if self.mirrored:
107 vertmat.flip()
108
109 rx, ry, rz = self.rotations_3d
110 if self.automatic_orientation:
111 rz += pi/2. - self.angle
112 elif self.force_rotation:
113 rz += self.angle
114
115 if (rx, ry, rz) != (0., 0., 0.):
116 if rx:
117 vertmat.rotate_x(-rx)
118 if ry:
119 vertmat.rotate_y(ry)
120 if rz:
121 vertmat.rotate_z(-rz) #TODO: minus, really?
122 if self.corner_relative_placement: # Reposition
123 vertmat.translate(width / 2., height / 2., 0.)
124 if self.allow_dest_offset:
125 vertmat.translate(*self.dest_offset)
126
127 x_1 = 1. / self.anm.size[0]
128 y_1 = 1. / self.anm.size[1]
129 tox, toy = self.texoffsets
130 uvs = [(tx * x_1 + tox, 1. - (ty * y_1) + toy),
131 ((tx + tw) * x_1 + tox, 1. - (ty * y_1) + toy),
132 ((tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy)),
133 (tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy))]
134
135 d = vertmat.data
136 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
137 self._colors = [(self.color[0], self.color[1], self.color[2], self.alpha)] * 4
138 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2])
139 self._changed = False
140 74
141 75
142 def update(self, override_width=0, override_height=0, angle_base=0., force_rotation=False): 76 def update(self, override_width=0, override_height=0, angle_base=0., force_rotation=False):
143 if (override_width != self.width_override 77 if (override_width != self.width_override
144 or override_height != self.height_override 78 or override_height != self.height_override