comparison pytouhou/game/sprite.py @ 30:e3ba2fa966f6

Various optimizations
author Thibaut Girka <thib@sitedethib.com>
date Fri, 12 Aug 2011 23:47:34 +0200
parents afa91be769ae
children 55973a3f1222
comparison
equal deleted inserted replaced
29:afa91be769ae 30:e3ba2fa966f6
22 self.mirrored = False 22 self.mirrored = False
23 self.rescale = (1., 1.) 23 self.rescale = (1., 1.)
24 self.rotations_3d = (0., 0., 0.) 24 self.rotations_3d = (0., 0., 0.)
25 self.rotations_speed_3d = (0., 0., 0.) 25 self.rotations_speed_3d = (0., 0., 0.)
26 self.corner_relative_placement = False 26 self.corner_relative_placement = False
27 self.instruction_pointer = 0
27 self.frame = 0 28 self.frame = 0
28 self._uvs = [] 29 self._uvs = []
29 self._vertices = [] 30 self._vertices = []
30 31
31 32
52 if rz: 53 if rz:
53 vertmat.rotate_z(-rz) #TODO: minus, really? 54 vertmat.rotate_z(-rz) #TODO: minus, really?
54 if self.corner_relative_placement: # Reposition 55 if self.corner_relative_placement: # Reposition
55 vertmat.translate(width / 2., height / 2., 0.) 56 vertmat.translate(width / 2., height / 2., 0.)
56 57
57 uvs = [(tx / self.anm.size[0], 1. - (ty / self.anm.size[1])), 58 x_1 = 1. / self.anm.size[0]
58 ((tx + tw) / self.anm.size[0], 1. - (ty / self.anm.size[1])), 59 y_1 = 1. / self.anm.size[1]
59 ((tx + tw) / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1])), 60 uvs = [(tx * x_1, 1. - (ty * y_1)),
60 (tx / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1]))] 61 ((tx + tw) * x_1, 1. - (ty * y_1)),
62 ((tx + tw) * x_1, 1. - ((ty + th) * y_1)),
63 (tx * x_1, 1. - ((ty + th) * y_1))]
61 64
62 vertices = [] 65 d = vertmat.data
63 for i in xrange(4): 66 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.)
64 w = vertmat.data[3][i] 67 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2])
65 vertices.append((vertmat.data[0][i] / w,
66 vertmat.data[1][i] / w,
67 vertmat.data[2][i] / w))
68
69 self._uvs, self._vertices = uvs, vertices
70 68
71 69
72 70
73 def update(self): 71 def update(self):
74 properties = {} 72 changed = False
75 for time, instr_type, data in self.anm.scripts[self.script_index]: 73 frame = self.frame
76 if time == self.frame: 74 script = self.anm.scripts[self.script_index]
77 if instr_type == 15: #Return 75 try:
78 break 76 while frame <= self.frame:
79 else: 77 frame, instr_type, data = script[self.instruction_pointer]
80 properties[instr_type] = data 78 if frame == self.frame:
79 changed = True
80 if instr_type == 1:
81 self.texcoords = self.anm.sprites[unpack('<I', data)[0]]
82 elif instr_type == 2:
83 self.rescale = unpack('<ff', data)
84 elif instr_type == 5:
85 self.frame, = unpack('<I', data)
86 self.instruction_pointer = 0
87 elif instr_type == 7:
88 self.mirrored = True
89 elif instr_type == 9:
90 self.rotations_3d = unpack('<fff', data)
91 elif instr_type == 10:
92 self.rotations_speed_3d = unpack('<fff', data)
93 elif instr_type == 23:
94 self.corner_relative_placement = True #TODO
95 elif instr_type == 15:
96 self.instruction_pointer += 1
97 break
98 else:
99 print('unhandled opcode: %d, %r' % (instr_type, data)) #TODO
100 if frame <= self.frame:
101 self.instruction_pointer += 1
102 except IndexError:
103 pass
81 self.frame += 1 104 self.frame += 1
82 105
83 self.rotations_3d = tuple(x + y for x, y in zip(self.rotations_3d, self.rotations_speed_3d)) 106 ax, ay, az = self.rotations_3d
107 sax, say, saz = self.rotations_speed_3d
108 self.rotations_3d = ax + sax, ay + say, az + saz
84 109
85 if properties:
86 if 1 in properties:
87 self.texcoords = self.anm.sprites[unpack('<I', properties[1])[0]]
88 del properties[1]
89 if 2 in properties:
90 self.rescale = unpack('<ff', properties[2])
91 del properties[2]
92 if 5 in properties:
93 self.frame, = unpack('<I', properties[5])
94 del properties[5]
95 if 7 in properties:
96 self.mirrored = True #TODO
97 del properties[7]
98 if 9 in properties:
99 self.rotations_3d = unpack('<fff', properties[9])
100 del properties[9]
101 if 10 in properties:
102 self.rotations_speed_3d = unpack('<fff', properties[10])
103 del properties[10]
104 if 23 in properties:
105 self.corner_relative_placement = True #TODO
106 del properties[23]
107 if properties:
108 print('Leftover properties: %r' % properties) #TODO
109 return True
110 if self.rotations_speed_3d != (0., 0., 0.): 110 if self.rotations_speed_3d != (0., 0., 0.):
111 return True 111 return True
112 return False
113 112
113 return changed
114