Mercurial > touhou
annotate pytouhou/game/background.py @ 16:66ce9bb440ac
Refactor in order to support multiple textures
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sat, 06 Aug 2011 12:36:25 +0200 |
parents | 07fba4e1da65 |
children | d940d004b840 |
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): | |
15 | 11 def __init__(self, stage, anim): |
12 self.stage = stage | |
13 self.anim = anim | |
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: |
52 sprite = Sprite(self.anim, script_index) | |
53 sprite.update(0, width_override, height_override) | |
54 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices) | |
13 | 55 faces.append((vertices, uvs)) |
56 self.objects.append(faces) | |
57 | |
58 | |
59 def update(self, frame): | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
60 if not self.objects_by_texture: |
13 | 61 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
|
62 nb_vertices = len(vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
63 vertices_format = 'f' * (3 * nb_vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
64 uvs_format = 'f' * (2 * nb_vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
65 vertices = struct.pack(vertices_format, *chain(*vertices)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
66 uvs = struct.pack(uvs_format, *chain(*uvs)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
67 self.objects_by_texture = {(self.anim.first_name, self.anim.secondary_name): (nb_vertices, vertices, uvs)} |
13 | 68 |
69 self.position_interpolator = Interpolator((0, 0, 0)) | |
70 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0)) | |
71 self.position2_interpolator = Interpolator((0, 0, 0)) | |
72 | |
73 for frame_num, message_type, args in self.stage.script: | |
74 if frame_num == frame: | |
14 | 75 if message_type == 0: |
76 self.position_interpolator.set_interpolation_start(frame_num, args) | |
77 elif message_type == 1: | |
13 | 78 self.fog_interpolator.set_interpolation_end_values(args) |
14 | 79 elif message_type == 2: |
80 self.position2_interpolator.set_interpolation_end_values(args) | |
13 | 81 elif message_type == 3: |
82 duration, = args | |
83 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration) | |
84 elif message_type == 4: | |
85 duration, = args | |
86 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration) | |
87 if frame_num > frame and message_type == 0: | |
88 self.position_interpolator.set_interpolation_end(frame_num, args) | |
89 break | |
90 | |
91 self.position2_interpolator.update(frame) | |
92 self.fog_interpolator.update(frame) | |
93 self.position_interpolator.update(frame) | |
94 |