comparison pytouhou/game/background.py @ 15:07fba4e1da65

Refactor
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 21:21:06 +0200
parents 07a7f28c8aaa
children 66ce9bb440ac
comparison
equal deleted inserted replaced
14:07a7f28c8aaa 15:07fba4e1da65
1 from io import BytesIO 1 from io import BytesIO
2 import os 2 import os
3 import struct 3 import struct
4 from itertools import chain 4 from itertools import chain
5 5
6 from pytouhou.utils.matrix import Matrix
7 from pytouhou.utils.interpolator import Interpolator 6 from pytouhou.utils.interpolator import Interpolator
8 7 from pytouhou.game.sprite import Sprite
9 from pytouhou.formats.std import Stage
10 from pytouhou.formats.anm0 import Animations
11 8
12 9
13 class Background(object): 10 class Background(object):
14 def __init__(self, archive, stage_num): 11 def __init__(self, stage, anim):
15 self.stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num))) 12 self.stage = stage
16 self.anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num))) 13 self.anim = anim
17 self.objects = [] 14 self.objects = []
18 self.object_instances = [] 15 self.object_instances = []
19 self._uvs = b'' 16 self._uvs = b''
20 self._vertices = b'' 17 self._vertices = b''
18 self.nb_vertices = 0
19
21 self.build_objects() 20 self.build_objects()
22 self.build_object_instances() 21 self.build_object_instances()
23 22
24 23
25 def build_object_instances(self): 24 def build_object_instances(self):
49 48
50 def build_objects(self): 49 def build_objects(self):
51 self.objects = [] 50 self.objects = []
52 for i, obj in enumerate(self.stage.objects): 51 for i, obj in enumerate(self.stage.objects):
53 faces = [] 52 faces = []
54 for script_index, x, y, z, width_override, height_override in obj.quads: 53 for script_index, ox, oy, oz, width_override, height_override in obj.quads:
55 #TODO: refactor 54 sprite = Sprite(self.anim, script_index)
56 vertices = [] 55 sprite.update(0, width_override, height_override)
57 uvs = [] 56 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
58 vertmat = Matrix()
59 vertmat.data[0][0] = -.5
60 vertmat.data[1][0] = -.5
61
62 vertmat.data[0][1] = .5
63 vertmat.data[1][1] = -.5
64
65 vertmat.data[0][2] = .5
66 vertmat.data[1][2] = .5
67
68 vertmat.data[0][3] = -.5
69 vertmat.data[1][3] = .5
70
71 for i in range(4):
72 vertmat.data[2][i] = 0.
73 vertmat.data[3][i] = 1.
74
75 properties = {}
76 for time, instr_type, data in self.anim.scripts[script_index]:
77 if instr_type == 15:
78 properties[15] = b''
79 break
80 elif time == 0: #TODO
81 properties[instr_type] = data
82 #if 15 not in properties: #TODO: Skip properties
83 # continue
84
85 #TODO: properties 3 and 4
86 if 1 in properties:
87 tx, ty, tw, th = self.anim.sprites[struct.unpack('<I', properties[1])[0]]
88 width, height = 1., 1.
89 if 2 in properties:
90 width, height = struct.unpack('<ff', properties[2])
91 width = width_override or width * tw
92 height = height_override or height * th
93 transform = Matrix.get_scaling_matrix(width, height, 1.)
94 if 7 in properties:
95 transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform)
96 if 9 in properties:
97 rx, ry, rz = struct.unpack('<fff', properties[9])
98 transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform)
99 transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform)
100 transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really?
101 if 23 in properties: # Reposition
102 transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform)
103 vertmat = transform.mult(vertmat)
104
105 uvs = [(tx / self.anim.size[0], 1. - (ty / self.anim.size[1])),
106 ((tx + tw) / self.anim.size[0], 1. - (ty / self.anim.size[1])),
107 ((tx + tw) / self.anim.size[0], 1. - ((ty + th) / self.anim.size[1])),
108 (tx / self.anim.size[0], 1. - ((ty + th) / self.anim.size[1]))]
109
110 for i in xrange(4):
111 w = vertmat.data[3][i]
112 vertices.append((vertmat.data[0][i] / w + x,
113 vertmat.data[1][i] / w + y,
114 vertmat.data[2][i] / w + z))
115 faces.append((vertices, uvs)) 57 faces.append((vertices, uvs))
116 self.objects.append(faces) 58 self.objects.append(faces)
117 59
118 60
119 def update(self, frame): 61 def update(self, frame):