Mercurial > touhou
annotate 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 |
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 |
30 | 27 self.instruction_pointer = 0 |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
28 self.frame = 0 |
15 | 29 self._uvs = [] |
30 self._vertices = [] | |
31 | |
32 | |
33 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
|
34 vertmat = Matrix([[-.5, .5, .5, -.5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
35 [-.5, -.5, .5, .5], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
36 [ .0, .0, .0, .0], |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
37 [ 1., 1., 1., 1.]]) |
15 | 38 |
39 tx, ty, tw, th = self.texcoords | |
40 sx, sy = self.rescale | |
41 width = override_width or (tw * sx) | |
42 height = override_height or (th * sy) | |
43 | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
44 vertmat.scale(width, height, 1.) |
15 | 45 if self.mirrored: |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
46 vertmat.flip() |
15 | 47 if self.rotations_3d != (0., 0., 0.): |
48 rx, ry, rz = self.rotations_3d | |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
49 if rx: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
50 vertmat.rotate_x(-rx) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
51 if ry: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
52 vertmat.rotate_y(ry) |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
53 if rz: |
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
54 vertmat.rotate_z(-rz) #TODO: minus, really? |
15 | 55 if self.corner_relative_placement: # Reposition |
28
f405b947624d
Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents:
27
diff
changeset
|
56 vertmat.translate(width / 2., height / 2., 0.) |
15 | 57 |
30 | 58 x_1 = 1. / self.anm.size[0] |
59 y_1 = 1. / self.anm.size[1] | |
60 uvs = [(tx * x_1, 1. - (ty * y_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))] | |
15 | 64 |
30 | 65 d = vertmat.data |
66 assert (d[3][0], d[3][1], d[3][2], d[3][3]) == (1., 1., 1., 1.) | |
67 self._uvs, self._vertices = uvs, zip(d[0], d[1], d[2]) | |
15 | 68 |
69 | |
70 | |
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
28
diff
changeset
|
71 def update(self): |
30 | 72 changed = False |
73 frame = self.frame | |
74 script = self.anm.scripts[self.script_index] | |
75 try: | |
76 while frame <= self.frame: | |
77 frame, instr_type, data = script[self.instruction_pointer] | |
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 | |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
104 self.frame += 1 |
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
105 |
30 | 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 | |
27
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
109 |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
110 if self.rotations_speed_3d != (0., 0., 0.): |
b65d6bc55793
Add rotating sprite support
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
111 return True |
15 | 112 |
30 | 113 return changed |
114 |