comparison pytouhou/game/background.py @ 13:58bc264aba38

Refactor
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 13:23:33 +0200
parents
children 07a7f28c8aaa
comparison
equal deleted inserted replaced
12:776453783743 13:58bc264aba38
1 from io import BytesIO
2 import os
3 import struct
4 from itertools import chain
5
6 from pytouhou.utils.matrix import Matrix
7 from pytouhou.utils.interpolator import Interpolator
8
9 from pytouhou.formats.std import Stage
10 from pytouhou.formats.anm0 import Animations
11
12
13
14 class Background(object):
15 def __init__(self, archive, stage_num):
16 self.stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)))
17 self.anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num)))
18 texture_components = [None, None]
19 for i, component_name in ((0, self.anim.first_name), (1, self.anim.secondary_name)):
20 if component_name:
21 texture_components[i] = BytesIO(archive.extract(os.path.basename(component_name)))
22 self.texture_components = texture_components
23 self.objects = []
24 self.object_instances = []
25 self._uvs = b''
26 self._vertices = b''
27 self.build_objects()
28 self.build_object_instances()
29
30
31 def build_object_instances(self):
32 self.object_instances = []
33 for obj, ox, oy, oz in self.stage.object_instances:
34 obj_id = self.stage.objects.index(obj)
35
36 obj_instance = []
37 for face_vertices, face_uvs in self.objects[obj_id]:
38 obj_instance.append((tuple((x + ox, y + oy, z + oz)
39 for x, y, z in face_vertices),
40 face_uvs))
41 self.object_instances.append(obj_instance)
42 # Z-sorting
43 def keyfunc(obj):
44 return min(z for face in obj for x, y, z in face[0])
45 self.object_instances.sort(key=keyfunc, reverse=True)
46
47
48 def object_instances_to_vertices_uvs(self):
49 vertices = tuple(vertex for obj in self.object_instances
50 for face in obj for vertex in face[0])
51 uvs = tuple(uv for obj in self.object_instances
52 for face in obj for uv in face[1])
53 return vertices, uvs
54
55
56 def build_objects(self):
57 self.objects = []
58 for i, obj in enumerate(self.stage.objects):
59 faces = []
60 for script_index, x, y, z, width_override, height_override in obj.quads:
61 #TODO: refactor
62 vertices = []
63 uvs = []
64 vertmat = Matrix()
65 vertmat.data[0][0] = -.5
66 vertmat.data[1][0] = -.5
67
68 vertmat.data[0][1] = .5
69 vertmat.data[1][1] = -.5
70
71 vertmat.data[0][2] = .5
72 vertmat.data[1][2] = .5
73
74 vertmat.data[0][3] = -.5
75 vertmat.data[1][3] = .5
76
77 for i in range(4):
78 vertmat.data[2][i] = 0.
79 vertmat.data[3][i] = 1.
80
81 properties = {}
82 for time, instr_type, data in self.anim.scripts[script_index]:
83 if instr_type == 15:
84 properties[15] = b''
85 break
86 elif time == 0: #TODO
87 properties[instr_type] = data
88 #if 15 not in properties: #TODO: Skip properties
89 # continue
90
91 #TODO: properties 3 and 4
92 if 1 in properties:
93 tx, ty, tw, th = self.anim.sprites[struct.unpack('<I', properties[1])[0]]
94 width, height = 1., 1.
95 if 2 in properties:
96 width, height = struct.unpack('<ff', properties[2])
97 width = width_override or width * tw
98 height = height_override or height * th
99 transform = Matrix.get_scaling_matrix(width, height, 1.)
100 if 7 in properties:
101 transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform)
102 if 9 in properties:
103 rx, ry, rz = struct.unpack('<fff', properties[9])
104 transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform)
105 transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform)
106 transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really?
107 if 23 in properties: # Reposition
108 transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform)
109 vertmat = transform.mult(vertmat)
110
111 uvs = [(tx / self.anim.size[0], 1. - (ty / self.anim.size[1])),
112 ((tx + tw) / self.anim.size[0], 1. - (ty / self.anim.size[1])),
113 ((tx + tw) / self.anim.size[0], 1. - ((ty + th) / self.anim.size[1])),
114 (tx / self.anim.size[0], 1. - ((ty + th) / self.anim.size[1]))]
115
116 for i in xrange(4):
117 w = vertmat.data[3][i]
118 vertices.append((vertmat.data[0][i] / w + x,
119 vertmat.data[1][i] / w + y,
120 vertmat.data[2][i] / w + z))
121 faces.append((vertices, uvs))
122 self.objects.append(faces)
123
124
125 def update(self, frame):
126 if not self._uvs or not self._vertices:
127 vertices, uvs = self.object_instances_to_vertices_uvs()
128 self.nb_vertices = len(vertices)
129 vertices_format = 'f' * (3 * self.nb_vertices)
130 uvs_format = 'f' * (2 * self.nb_vertices)
131 self._vertices = struct.pack(vertices_format, *chain(*vertices))
132 self._uvs = struct.pack(uvs_format, *chain(*uvs))
133
134 self.position_interpolator = Interpolator((0, 0, 0))
135 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0))
136 self.position2_interpolator = Interpolator((0, 0, 0))
137
138 for frame_num, message_type, args in self.stage.script:
139 if frame_num == frame:
140 if message_type == 1:
141 self.fog_interpolator.set_interpolation_end_values(args)
142 elif message_type == 3:
143 duration, = args
144 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration)
145 elif message_type == 4:
146 duration, = args
147 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration)
148 elif message_type == 2:
149 self.position2_interpolator.set_interpolation_end_values(args)
150 if frame_num <= frame and message_type == 0:
151 self.position_interpolator.set_interpolation_start(frame_num, args)
152 if frame_num > frame and message_type == 0:
153 self.position_interpolator.set_interpolation_end(frame_num, args)
154 break
155
156 self.position2_interpolator.update(frame)
157 self.fog_interpolator.update(frame)
158 self.position_interpolator.update(frame)
159