annotate pytouhou/game/sprite.py @ 15:07fba4e1da65

Refactor
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 21:21:06 +0200
parents
children d940d004b840
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 from struct import unpack
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 from pytouhou.utils.matrix import Matrix
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 class Sprite(object):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 def __init__(self, anm, script_index):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 self.anm = anm
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 self.script_index = script_index
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 self.texcoords = (0, 0, 0, 0) # x, y, width, height
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 self.mirrored = False
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 self.rescale = (1., 1.)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 self.rotations_3d = (0., 0., 0.)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 self.corner_relative_placement = False
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14 self._uvs = []
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 self._vertices = []
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 def update_uvs_vertices(self, override_width=0, override_height=0):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 vertmat = Matrix()
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 vertmat.data[0][0] = -.5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 vertmat.data[1][0] = -.5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 vertmat.data[0][1] = .5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 vertmat.data[1][1] = -.5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 vertmat.data[0][2] = .5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 vertmat.data[1][2] = .5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 vertmat.data[0][3] = -.5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 vertmat.data[1][3] = .5
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 for i in range(4):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 vertmat.data[2][i] = 0.
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 vertmat.data[3][i] = 1.
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 tx, ty, tw, th = self.texcoords
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 sx, sy = self.rescale
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 width = override_width or (tw * sx)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 height = override_height or (th * sy)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 transform = Matrix.get_scaling_matrix(width, height, 1.)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 if self.mirrored:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 if self.rotations_3d != (0., 0., 0.):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 rx, ry, rz = self.rotations_3d
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really?
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 if self.corner_relative_placement: # Reposition
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 vertmat = transform.mult(vertmat)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 uvs = [(tx / self.anm.size[0], 1. - (ty / self.anm.size[1])),
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 ((tx + tw) / self.anm.size[0], 1. - (ty / self.anm.size[1])),
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 ((tx + tw) / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1])),
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 (tx / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1]))]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 vertices = []
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 for i in xrange(4):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 w = vertmat.data[3][i]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 vertices.append((vertmat.data[0][i] / w,
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 vertmat.data[1][i] / w,
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 vertmat.data[2][i] / w))
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 self._uvs, self._vertices = uvs, vertices
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 def update(self, frame, override_width=0, override_height=0):
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 properties = {}
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 for time, instr_type, data in self.anm.scripts[self.script_index]:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 if time == frame:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 if instr_type == 15: #Return
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 break
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 else:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 properties[instr_type] = data
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 if properties:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 if 1 in properties:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 self.texcoords = self.anm.sprites[unpack('<I', properties[1])[0]]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 del properties[1]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 if 2 in properties:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 self.rescale = unpack('<ff', properties[2])
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 del properties[2]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 if 7 in properties:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 self.mirrored = True #TODO
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 del properties[7]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 if 9 in properties:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 self.rotations_3d = unpack('<fff', properties[9])
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 del properties[9]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 if 23 in properties:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 self.corner_relative_placement = True #TODO
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 del properties[23]
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 if properties:
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 print('Leftover properties: %r' % properties) #TODO
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 self.update_uvs_vertices(override_width, override_height)
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 return True
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 return False
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98