Mercurial > touhou
annotate pytouhou/game/sprite.py @ 28:f405b947624d
Massive sprite updating/matrix handling optimizations
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Fri, 12 Aug 2011 21:40:26 +0200 |
parents | b65d6bc55793 |
children | afa91be769ae |
rev | line source |
---|---|
15 | 1 from struct import unpack |
2 | |
3 from pytouhou.utils.matrix import Matrix | |
4 | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
5 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
6 class AnmWrapper(object): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
7 def __init__(self, anm_files): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
8 self.anm_files = list(anm_files) |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
9 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
10 def get_sprite(self, script_index): |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
11 for anm in self.anm_files: |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
12 if script_index in anm.scripts: |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
13 return anm, Sprite(anm, script_index) |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
14 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
15 |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
16 |
15 | 17 class Sprite(object): |
18 def __init__(self, anm, script_index): | |
19 self.anm = anm | |
20 self.script_index = script_index | |
21 self.texcoords = (0, 0, 0, 0) # x, y, width, height | |
22 self.mirrored = False | |
23 self.rescale = (1., 1.) | |
24 self.rotations_3d = (0., 0., 0.) | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
25 self.rotations_speed_3d = (0., 0., 0.) |
15 | 26 self.corner_relative_placement = False |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
27 self.frame = 0 |
15 | 28 self._uvs = [] |
29 self._vertices = [] | |
30 | |
31 | |
32 def update_uvs_vertices(self, override_width=0, override_height=0): | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
33 vertmat = Matrix([[-.5, .5, .5, -.5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
34 [-.5, -.5, .5, .5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
35 [ .0, .0, .0, .0], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
36 [ 1., 1., 1., 1.]]) |
15 | 37 |
38 tx, ty, tw, th = self.texcoords | |
39 sx, sy = self.rescale | |
40 width = override_width or (tw * sx) | |
41 height = override_height or (th * sy) | |
42 | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
43 vertmat.scale(width, height, 1.) |
15 | 44 if self.mirrored: |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
45 vertmat.flip() |
15 | 46 if self.rotations_3d != (0., 0., 0.): |
47 rx, ry, rz = self.rotations_3d | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
48 if rx: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
49 vertmat.rotate_x(-rx) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
50 if ry: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
51 vertmat.rotate_y(ry) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
52 if rz: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
53 vertmat.rotate_z(-rz) #TODO: minus, really? |
15 | 54 if self.corner_relative_placement: # Reposition |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
55 vertmat.translate(width / 2., height / 2., 0.) |
15 | 56 |
57 uvs = [(tx / self.anm.size[0], 1. - (ty / self.anm.size[1])), | |
58 ((tx + tw) / self.anm.size[0], 1. - (ty / self.anm.size[1])), | |
59 ((tx + tw) / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1])), | |
60 (tx / self.anm.size[0], 1. - ((ty + th) / self.anm.size[1]))] | |
61 | |
62 vertices = [] | |
63 for i in xrange(4): | |
64 w = vertmat.data[3][i] | |
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 | |
71 | |
72 | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
73 def update(self, override_width=0, override_height=0): |
15 | 74 properties = {} |
75 for time, instr_type, data in self.anm.scripts[self.script_index]: | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
76 if time == self.frame: |
15 | 77 if instr_type == 15: #Return |
78 break | |
79 else: | |
80 properties[instr_type] = data | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
81 self.frame += 1 |
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
82 |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
83 self.rotations_3d = tuple(x + y for x, y in zip(self.rotations_3d, self.rotations_speed_3d)) |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
84 |
15 | 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] | |
26
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
92 if 5 in properties: |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
93 self.frame, = unpack('<I', properties[5]) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
94 del properties[5] |
15 | 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] | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
101 if 10 in properties: |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
102 self.rotations_speed_3d = unpack('<fff', properties[10]) |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
103 del properties[10] |
15 | 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 self.update_uvs_vertices(override_width, override_height) | |
110 return True | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
111 if self.rotations_speed_3d != (0., 0., 0.): |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
112 self.update_uvs_vertices(override_width, override_height) |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
113 return True |
15 | 114 return False |
115 |