comparison pytouhou/formats/anm0.py @ 69:a142e57218a0

Refactor. Move VMs to pytouhou.vm.
author Thibaut Girka <thib@sitedethib.com>
date Sat, 27 Aug 2011 10:58:54 +0200
parents 694f25881d0f
children a03d7a94b997
comparison
equal deleted inserted replaced
68:a2459defd4b6 69:a142e57218a0
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from struct import pack, unpack 15 from struct import pack, unpack
16 from pytouhou.utils.helpers import read_string 16 from pytouhou.utils.helpers import read_string, get_logger
17
18
19 logger = get_logger(__name__)
17 20
18 #TODO: refactor/clean up 21 #TODO: refactor/clean up
19 22
20 23
21 class Animations(object): 24 class Animations(object):
25 _instructions = {0: ('', 'delete'),
26 1: ('I', 'set_sprite'),
27 2: ('ff', 'set_scale'),
28 3: ('I', 'set_alpha'),
29 4: ('BBBx', 'set_color'),
30 5: ('I', 'jump'),
31 7: ('', 'toggle_mirrored'),
32 9: ('fff', 'set_3d_rotations'),
33 10: ('fff', 'set_3d_rotations_speed'),
34 11: ('ff', 'set_scale_speed'),
35 12: ('ii', 'fade'),
36 15: ('', 'keep_still'),
37 16: ('i', 'set_random_sprite'),
38 23: ('', 'set_corner_relative_placement'),
39 27: ('f', 'shift_texture_x'),
40 28: ('f', 'shift_texture_y'),
41 30: ('ffi', 'scale_in')}
42
43
22 def __init__(self): 44 def __init__(self):
23 self.size = (0, 0) 45 self.size = (0, 0)
24 self.first_name = None 46 self.first_name = None
25 self.secondary_name = None 47 self.secondary_name = None
26 self.sprites = {} 48 self.sprites = {}
69 instruction_offsets = [] 91 instruction_offsets = []
70 file.seek(offset) 92 file.seek(offset)
71 while True: 93 while True:
72 #TODO 94 #TODO
73 instruction_offsets.append(file.tell() - offset) 95 instruction_offsets.append(file.tell() - offset)
74 time, instr_type, length = unpack('<HBB', file.read(4)) 96 time, opcode, length = unpack('<HBB', file.read(4))
75 data = file.read(length) 97 data = file.read(length)
76 if instr_type == 1: # set_sprite 98 if opcode in cls._instructions:
77 args = unpack('<I', data) 99 args = unpack('<%s' % cls._instructions[opcode][0], data)
78 elif instr_type == 2: # set_scale
79 args = unpack('<ff', data)
80 elif instr_type == 3: # set_alpha
81 args = unpack('<I', data)
82 elif instr_type == 4: # set_color
83 args = unpack('<BBBx', data)
84 elif instr_type == 5: # jump
85 # Translate offset to instruction index
86 args = (instruction_offsets.index(unpack('<I', data)[0]),)
87 elif instr_type == 9: # set_3d_rotation
88 args = unpack('<fff', data)
89 elif instr_type == 10: # set_3d_rotation_speed
90 args = unpack('<fff', data)
91 elif instr_type == 11: # set_scale_speed
92 args = unpack('<ff', data)
93 elif instr_type == 12: # fade
94 args = unpack('<ii', data)
95 elif instr_type == 16: # set_random_sprite
96 args = unpack('<ii', data)
97 elif instr_type == 27: # shift_texture_x
98 args = unpack('<f', data)
99 elif instr_type == 28: # shift_texture_y
100 args = unpack('<f', data)
101 elif instr_type == 30: # scale_in
102 args = unpack('<ffi', data)
103 else: 100 else:
104 args = (data,) 101 args = (data,)
105 anm.scripts[i].append((time, instr_type, args)) 102 logger.warn('unknown opcode %d', opcode)
106 if instr_type == 0: 103
104 anm.scripts[i].append((time, opcode, args))
105 if opcode == 0:
107 break 106 break
107
108 # Translate offsets to instruction pointers
109 for instr_offset, (j, instr) in zip(instruction_offsets, enumerate(anm.scripts[i])):
110 time, opcode, args = instr
111 if opcode == 5:
112 args = (instruction_offsets.index(args[0]),)
113 anm.scripts[i][j] = time, opcode, args
108 #TODO 114 #TODO
109 115
110 return anm 116 return anm
111 117