Mercurial > touhou
annotate pytouhou/game/background.py @ 23:444ac7bca7bc
Refacto ECL stuff, add support for a few instructions, and add some culling
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Fri, 12 Aug 2011 19:13:43 +0200 |
parents | bf225780973f |
children | f17122405121 |
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 |
13 | 18 self.build_objects() |
19 self.build_object_instances() | |
20 | |
21 | |
22 def build_object_instances(self): | |
23 self.object_instances = [] | |
24 for obj, ox, oy, oz in self.stage.object_instances: | |
25 obj_id = self.stage.objects.index(obj) | |
26 | |
27 obj_instance = [] | |
28 for face_vertices, face_uvs in self.objects[obj_id]: | |
29 obj_instance.append((tuple((x + ox, y + oy, z + oz) | |
30 for x, y, z in face_vertices), | |
31 face_uvs)) | |
32 self.object_instances.append(obj_instance) | |
33 # Z-sorting | |
34 def keyfunc(obj): | |
35 return min(z for face in obj for x, y, z in face[0]) | |
36 self.object_instances.sort(key=keyfunc, reverse=True) | |
37 | |
38 | |
39 def object_instances_to_vertices_uvs(self): | |
40 vertices = tuple(vertex for obj in self.object_instances | |
41 for face in obj for vertex in face[0]) | |
42 uvs = tuple(uv for obj in self.object_instances | |
43 for face in obj for uv in face[1]) | |
44 return vertices, uvs | |
45 | |
46 | |
47 def build_objects(self): | |
48 self.objects = [] | |
49 for i, obj in enumerate(self.stage.objects): | |
50 faces = [] | |
15 | 51 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
|
52 #TODO: per-texture rendering |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
53 anm, sprite = self.anm_wrapper.get_sprite(script_index) |
17
d940d004b840
Make game.sprite.Sprite use its own frame counter.
Thibaut Girka <thib@sitedethib.com>
parents:
16
diff
changeset
|
54 sprite.update(width_override, height_override) |
15 | 55 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices) |
13 | 56 faces.append((vertices, uvs)) |
57 self.objects.append(faces) | |
58 | |
59 | |
60 def update(self, frame): | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
61 if not self.objects_by_texture: |
13 | 62 vertices, uvs = self.object_instances_to_vertices_uvs() |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
63 nb_vertices = len(vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
64 vertices_format = 'f' * (3 * nb_vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
65 uvs_format = 'f' * (2 * nb_vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
66 vertices = struct.pack(vertices_format, *chain(*vertices)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
67 uvs = struct.pack(uvs_format, *chain(*uvs)) |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
68 assert len(self.anm_wrapper.anm_files) == 1 #TODO |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
69 anm = self.anm_wrapper.anm_files[0] |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
70 self.objects_by_texture = {(anm.first_name, anm.secondary_name): (nb_vertices, vertices, uvs)} |
13 | 71 |
72 self.position_interpolator = Interpolator((0, 0, 0)) | |
73 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0)) | |
74 self.position2_interpolator = Interpolator((0, 0, 0)) | |
75 | |
76 for frame_num, message_type, args in self.stage.script: | |
77 if frame_num == frame: | |
14 | 78 if message_type == 0: |
79 self.position_interpolator.set_interpolation_start(frame_num, args) | |
80 elif message_type == 1: | |
13 | 81 self.fog_interpolator.set_interpolation_end_values(args) |
14 | 82 elif message_type == 2: |
83 self.position2_interpolator.set_interpolation_end_values(args) | |
13 | 84 elif message_type == 3: |
85 duration, = args | |
86 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration) | |
87 elif message_type == 4: | |
88 duration, = args | |
89 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration) | |
90 if frame_num > frame and message_type == 0: | |
91 self.position_interpolator.set_interpolation_end(frame_num, args) | |
92 break | |
93 | |
94 self.position2_interpolator.update(frame) | |
95 self.fog_interpolator.update(frame) | |
96 self.position_interpolator.update(frame) | |
97 |