comparison 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
comparison
equal deleted inserted replaced
36:f46c18872796 37:a10e3f44a883
27 self.object_instances = [] 27 self.object_instances = []
28 for obj, ox, oy, oz in self.stage.object_instances: 28 for obj, ox, oy, oz in self.stage.object_instances:
29 obj_id = self.stage.objects.index(obj) 29 obj_id = self.stage.objects.index(obj)
30 30
31 obj_instance = [] 31 obj_instance = []
32 for face_vertices, face_uvs in self.objects[obj_id]: 32 for face_vertices, face_uvs, face_colors in self.objects[obj_id]:
33 obj_instance.append((tuple((x + ox, y + oy, z + oz) 33 obj_instance.append((tuple((x + ox, y + oy, z + oz)
34 for x, y, z in face_vertices), 34 for x, y, z in face_vertices),
35 face_uvs)) 35 face_uvs,
36 face_colors))
36 self.object_instances.append(obj_instance) 37 self.object_instances.append(obj_instance)
37 # Z-sorting 38 # Z-sorting
38 def keyfunc(obj): 39 def keyfunc(obj):
39 return min(z for face in obj for x, y, z in face[0]) 40 return min(z for face in obj for x, y, z in face[0])
40 self.object_instances.sort(key=keyfunc, reverse=True) 41 self.object_instances.sort(key=keyfunc, reverse=True)
41 42
42 43
43 def object_instances_to_vertices_uvs(self): 44 def object_instances_to_vertices_uvs_colors(self):
44 vertices = tuple(vertex for obj in self.object_instances 45 vertices = tuple(vertex for obj in self.object_instances
45 for face in obj for vertex in face[0]) 46 for face in obj for vertex in face[0])
46 uvs = tuple(uv for obj in self.object_instances 47 uvs = tuple(uv for obj in self.object_instances
47 for face in obj for uv in face[1]) 48 for face in obj for uv in face[1])
48 return vertices, uvs 49 colors = tuple(color for obj in self.object_instances
50 for face in obj for color in face[2])
51 return vertices, uvs, colors
49 52
50 53
51 def build_objects(self): 54 def build_objects(self):
52 self.objects = [] 55 self.objects = []
53 for i, obj in enumerate(self.stage.objects): 56 for i, obj in enumerate(self.stage.objects):
54 faces = [] 57 faces = []
55 for script_index, ox, oy, oz, width_override, height_override in obj.quads: 58 for script_index, ox, oy, oz, width_override, height_override in obj.quads:
56 #TODO: per-texture rendering 59 #TODO: per-texture rendering
57 anm, sprite = self.anm_wrapper.get_sprite(script_index) 60 anm, sprite = self.anm_wrapper.get_sprite(script_index)
58 if sprite.update(): 61 if sprite.update():
59 sprite.update_uvs_vertices(width_override, height_override) 62 sprite.update_vertices_uvs_colors(width_override, height_override)
60 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices) 63 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
61 faces.append((vertices, uvs)) 64 colors = sprite._colors
65 faces.append((vertices, uvs, colors))
62 self.objects.append(faces) 66 self.objects.append(faces)
63 67
64 68
65 def update(self, frame): 69 def update(self, frame):
66 if not self.objects_by_texture: 70 if not self.objects_by_texture:
67 vertices, uvs = self.object_instances_to_vertices_uvs() 71 vertices, uvs, colors = self.object_instances_to_vertices_uvs_colors()
68 nb_vertices = len(vertices) 72 nb_vertices = len(vertices)
69 vertices_format = 'f' * (3 * nb_vertices) 73 vertices_format = 'f' * (3 * nb_vertices)
70 uvs_format = 'f' * (2 * nb_vertices) 74 uvs_format = 'f' * (2 * nb_vertices)
75 colors_format = 'B' * (4 * nb_vertices)
71 vertices = struct.pack(vertices_format, *chain(*vertices)) 76 vertices = struct.pack(vertices_format, *chain(*vertices))
72 uvs = struct.pack(uvs_format, *chain(*uvs)) 77 uvs = struct.pack(uvs_format, *chain(*uvs))
78 colors = struct.pack(colors_format, *chain(*colors))
73 assert len(self.anm_wrapper.anm_files) == 1 #TODO 79 assert len(self.anm_wrapper.anm_files) == 1 #TODO
74 anm = self.anm_wrapper.anm_files[0] 80 anm = self.anm_wrapper.anm_files[0]
75 self.objects_by_texture = {(anm.first_name, anm.secondary_name): (nb_vertices, vertices, uvs)} 81 self.objects_by_texture = {(anm.first_name, anm.secondary_name): (nb_vertices, vertices, uvs, colors)}
76 82
77 for frame_num, message_type, args in self.stage.script: 83 for frame_num, message_type, args in self.stage.script:
78 if frame_num == frame: 84 if frame_num == frame:
79 if message_type == 0: 85 if message_type == 0:
80 self.position_interpolator.set_interpolation_start(frame_num, args) 86 self.position_interpolator.set_interpolation_start(frame_num, args)