Mercurial > touhou
annotate pytouhou/game/background.py @ 37:a10e3f44a883
Add alpha (anm0 instruction 3) support
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 14 Aug 2011 18:00:06 +0200 |
parents | afa91be769ae |
children | ab826bc29aa2 |
rev | line source |
---|---|
13 | 1 from io import BytesIO |
2 import os | |
3 import struct | |
4 from itertools import chain | |
5 | |
6 from pytouhou.utils.interpolator import Interpolator | |
15 | 7 from pytouhou.game.sprite import Sprite |
13 | 8 |
9 | |
10 class Background(object): | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
11 def __init__(self, stage, anm_wrapper): |
15 | 12 self.stage = stage |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
13 self.anm_wrapper = anm_wrapper |
13 | 14 self.objects = [] |
15 self.object_instances = [] | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
16 self.objects_by_texture = {} |
15 | 17 |
26
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
18 self.position_interpolator = Interpolator((0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
19 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
20 self.position2_interpolator = Interpolator((0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
21 |
13 | 22 self.build_objects() |
23 self.build_object_instances() | |
24 | |
25 | |
26 def build_object_instances(self): | |
27 self.object_instances = [] | |
28 for obj, ox, oy, oz in self.stage.object_instances: | |
29 obj_id = self.stage.objects.index(obj) | |
30 | |
31 obj_instance = [] | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
32 for face_vertices, face_uvs, face_colors in self.objects[obj_id]: |
13 | 33 obj_instance.append((tuple((x + ox, y + oy, z + oz) |
34 for x, y, z in face_vertices), | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
35 face_uvs, |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
36 face_colors)) |
13 | 37 self.object_instances.append(obj_instance) |
38 # Z-sorting | |
39 def keyfunc(obj): | |
40 return min(z for face in obj for x, y, z in face[0]) | |
41 self.object_instances.sort(key=keyfunc, reverse=True) | |
42 | |
43 | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
44 def object_instances_to_vertices_uvs_colors(self): |
13 | 45 vertices = tuple(vertex for obj in self.object_instances |
46 for face in obj for vertex in face[0]) | |
47 uvs = tuple(uv for obj in self.object_instances | |
48 for face in obj for uv in face[1]) | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
49 colors = tuple(color for obj in self.object_instances |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
50 for face in obj for color in face[2]) |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
51 return vertices, uvs, colors |
13 | 52 |
53 | |
54 def build_objects(self): | |
55 self.objects = [] | |
56 for i, obj in enumerate(self.stage.objects): | |
57 faces = [] | |
15 | 58 for script_index, ox, oy, oz, width_override, height_override in obj.quads: |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
59 #TODO: per-texture rendering |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
60 anm, sprite = self.anm_wrapper.get_sprite(script_index) |
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
61 if sprite.update(): |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
62 sprite.update_vertices_uvs_colors(width_override, height_override) |
15 | 63 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices) |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
64 colors = sprite._colors |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
65 faces.append((vertices, uvs, colors)) |
13 | 66 self.objects.append(faces) |
67 | |
68 | |
69 def update(self, frame): | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
70 if not self.objects_by_texture: |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
71 vertices, uvs, colors = self.object_instances_to_vertices_uvs_colors() |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
72 nb_vertices = len(vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
73 vertices_format = 'f' * (3 * nb_vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
74 uvs_format = 'f' * (2 * nb_vertices) |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
75 colors_format = 'B' * (4 * nb_vertices) |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
76 vertices = struct.pack(vertices_format, *chain(*vertices)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
77 uvs = struct.pack(uvs_format, *chain(*uvs)) |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
78 colors = struct.pack(colors_format, *chain(*colors)) |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
79 assert len(self.anm_wrapper.anm_files) == 1 #TODO |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
80 anm = self.anm_wrapper.anm_files[0] |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
81 self.objects_by_texture = {(anm.first_name, anm.secondary_name): (nb_vertices, vertices, uvs, colors)} |
13 | 82 |
83 for frame_num, message_type, args in self.stage.script: | |
84 if frame_num == frame: | |
14 | 85 if message_type == 0: |
86 self.position_interpolator.set_interpolation_start(frame_num, args) | |
87 elif message_type == 1: | |
13 | 88 self.fog_interpolator.set_interpolation_end_values(args) |
14 | 89 elif message_type == 2: |
90 self.position2_interpolator.set_interpolation_end_values(args) | |
13 | 91 elif message_type == 3: |
92 duration, = args | |
93 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration) | |
94 elif message_type == 4: | |
95 duration, = args | |
96 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration) | |
97 if frame_num > frame and message_type == 0: | |
98 self.position_interpolator.set_interpolation_end(frame_num, args) | |
99 break | |
100 | |
101 self.position2_interpolator.update(frame) | |
102 self.fog_interpolator.update(frame) | |
103 self.position_interpolator.update(frame) | |
104 |