comparison pytouhou/game/background.py @ 94:ca571697ec83

Various minor optimisations and refactoring
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 20:04:00 +0200
parents 85f3b8ba3f24
children 2a03940deea3
comparison
equal deleted inserted replaced
93:d167280a82fc 94:ca571697ec83
25 25
26 class Background(object): 26 class Background(object):
27 def __init__(self, stage, anm_wrapper): 27 def __init__(self, stage, anm_wrapper):
28 self.stage = stage 28 self.stage = stage
29 self.anm_wrapper = anm_wrapper 29 self.anm_wrapper = anm_wrapper
30 self.objects = [] 30 self.models = []
31 self.object_instances = [] 31 self.object_instances = []
32 self.objects_by_texture = {} 32 self.objects_by_texture = {}
33 33
34 self.position_interpolator = Interpolator((0, 0, 0)) 34 self.position_interpolator = Interpolator((0, 0, 0))
35 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0)) 35 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0))
36 self.position2_interpolator = Interpolator((0, 0, 0)) 36 self.position2_interpolator = Interpolator((0, 0, 0))
37 37
38 self.build_objects() 38 self.build_models()
39 self.build_object_instances() 39 self.build_object_instances()
40 40
41 41
42 def build_object_instances(self): 42 def build_object_instances(self):
43 self.object_instances = [] 43 self.object_instances = []
44 for obj, ox, oy, oz in self.stage.object_instances: 44 for obj, ox, oy, oz in self.stage.object_instances:
45 obj_id = self.stage.objects.index(obj)
46 45
47 obj_instance = [] 46 obj_instance = []
48 for face_vertices, face_uvs, face_colors in self.objects[obj_id]: 47 for face_vertices, face_uvs, face_colors in self.models[obj]:
49 obj_instance.append((tuple((x + ox, y + oy, z + oz) 48 obj_instance.append((tuple((x + ox, y + oy, z + oz)
50 for x, y, z in face_vertices), 49 for x, y, z in face_vertices),
51 face_uvs, 50 face_uvs,
52 face_colors)) 51 face_colors))
53 self.object_instances.append(obj_instance) 52 self.object_instances.append(obj_instance)
65 colors = tuple(color for obj in self.object_instances 64 colors = tuple(color for obj in self.object_instances
66 for face in obj for color in face[2]) 65 for face in obj for color in face[2])
67 return vertices, uvs, colors 66 return vertices, uvs, colors
68 67
69 68
70 def build_objects(self): 69 def build_models(self):
71 self.objects = [] 70 self.models = []
72 for i, obj in enumerate(self.stage.objects): 71 for i, obj in enumerate(self.stage.models):
73 faces = [] 72 faces = []
74 for script_index, ox, oy, oz, width_override, height_override in obj.quads: 73 for script_index, ox, oy, oz, width_override, height_override in obj.quads:
75 #TODO: per-texture rendering 74 #TODO: per-texture rendering
76 sprite = Sprite() 75 sprite = Sprite()
77 anm_runner = ANMRunner(self.anm_wrapper, script_index, sprite) 76 anm_runner = ANMRunner(self.anm_wrapper, script_index, sprite)
80 if sprite._changed: 79 if sprite._changed:
81 sprite.update_vertices_uvs_colors() 80 sprite.update_vertices_uvs_colors()
82 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices) 81 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
83 colors = sprite._colors 82 colors = sprite._colors
84 faces.append((vertices, uvs, colors)) 83 faces.append((vertices, uvs, colors))
85 self.objects.append(faces) 84 self.models.append(faces)
86
87
88 def get_objects_by_texture(self, objects_by_texture):
89 objects_by_texture.update(self.objects_by_texture)
90 85
91 86
92 def update(self, frame): 87 def update(self, frame):
93 if not self.objects_by_texture: 88 if not self.objects_by_texture:
94 vertices, uvs, colors = self.object_instances_to_vertices_uvs_colors() 89 vertices, uvs, colors = self.object_instances_to_vertices_uvs_colors()