annotate pytouhou/game/background.py @ 91:f7525fa66bb0

Fix ECL instruction 3
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 10:19:42 +0200
parents 630e9045e851
children 85f3b8ba3f24
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
6 ## it under the terms of the GNU General Public License as published
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
7 ## by the Free Software Foundation; version 3 only.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
9 ## This program is distributed in the hope that it will be useful,
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
12 ## GNU General Public License for more details.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
14
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 37
diff changeset
15
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from io import BytesIO
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 import os
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 import struct
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 from itertools import chain
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 from pytouhou.utils.interpolator import Interpolator
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
22 from pytouhou.vm.anmrunner import ANMRunner
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
23 from pytouhou.game.sprite import Sprite
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 class Background(object):
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 17
diff changeset
27 def __init__(self, stage, anm_wrapper):
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
28 self.stage = stage
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 17
diff changeset
29 self.anm_wrapper = anm_wrapper
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 self.objects = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 self.object_instances = []
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
32 self.objects_by_texture = {}
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
33
26
f17122405121 Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
34 self.position_interpolator = Interpolator((0, 0, 0))
f17122405121 Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
35 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0))
f17122405121 Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
36 self.position2_interpolator = Interpolator((0, 0, 0))
f17122405121 Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents: 21
diff changeset
37
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 self.build_objects()
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 self.build_object_instances()
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 def build_object_instances(self):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 self.object_instances = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 for obj, ox, oy, oz in self.stage.object_instances:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 obj_id = self.stage.objects.index(obj)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 obj_instance = []
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
48 for face_vertices, face_uvs, face_colors in self.objects[obj_id]:
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 obj_instance.append((tuple((x + ox, y + oy, z + oz)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 for x, y, z in face_vertices),
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
51 face_uvs,
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
52 face_colors))
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 self.object_instances.append(obj_instance)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 # Z-sorting
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 def keyfunc(obj):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 return min(z for face in obj for x, y, z in face[0])
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 self.object_instances.sort(key=keyfunc, reverse=True)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
60 def object_instances_to_vertices_uvs_colors(self):
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 vertices = tuple(vertex for obj in self.object_instances
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 for face in obj for vertex in face[0])
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 uvs = tuple(uv for obj in self.object_instances
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 for face in obj for uv in face[1])
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
65 colors = tuple(color for obj in self.object_instances
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
66 for face in obj for color in face[2])
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
67 return vertices, uvs, colors
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 def build_objects(self):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 self.objects = []
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 for i, obj in enumerate(self.stage.objects):
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 faces = []
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
74 for script_index, ox, oy, oz, width_override, height_override in obj.quads:
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 17
diff changeset
75 #TODO: per-texture rendering
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
76 sprite = Sprite()
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
77 anm_runner = ANMRunner(self.anm_wrapper, script_index, sprite)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
78 anm_runner.run_frame()
90
630e9045e851 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
79 sprite.update(width_override, height_override)
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
80 if sprite._changed:
90
630e9045e851 Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 72
diff changeset
81 sprite.update_vertices_uvs_colors()
15
07fba4e1da65 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 14
diff changeset
82 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
83 colors = sprite._colors
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
84 faces.append((vertices, uvs, colors))
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 self.objects.append(faces)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 def update(self, frame):
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
89 if not self.objects_by_texture:
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
90 vertices, uvs, colors = self.object_instances_to_vertices_uvs_colors()
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
91 nb_vertices = len(vertices)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
92 vertices_format = 'f' * (3 * nb_vertices)
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
93 uvs_format = 'f' * (2 * nb_vertices)
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
94 colors_format = 'B' * (4 * nb_vertices)
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
95 vertices = struct.pack(vertices_format, *chain(*vertices))
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 15
diff changeset
96 uvs = struct.pack(uvs_format, *chain(*uvs))
37
a10e3f44a883 Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents: 29
diff changeset
97 colors = struct.pack(colors_format, *chain(*colors))
21
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 17
diff changeset
98 assert len(self.anm_wrapper.anm_files) == 1 #TODO
bf225780973f Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents: 17
diff changeset
99 anm = self.anm_wrapper.anm_files[0]
72
6a08f44fa01b Handle a few more ANM instructions. pytouhou.game.background needs some serious refactoring.
Thibaut Girka <thib@sitedethib.com>
parents: 69
diff changeset
100 self.objects_by_texture = {((anm.first_name, anm.secondary_name), 0): (nb_vertices, vertices, uvs, colors)} #TODO: blendfunc
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
101
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
102 for frame_num, message_type, args in self.stage.script:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
103 if frame_num == frame:
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
104 if message_type == 0:
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
105 self.position_interpolator.set_interpolation_start(frame_num, args)
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
106 elif message_type == 1:
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107 self.fog_interpolator.set_interpolation_end_values(args)
14
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
108 elif message_type == 2:
07a7f28c8aaa Minor refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 13
diff changeset
109 self.position2_interpolator.set_interpolation_end_values(args)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
110 elif message_type == 3:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
111 duration, = args
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
112 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 elif message_type == 4:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114 duration, = args
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116 if frame_num > frame and message_type == 0:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 self.position_interpolator.set_interpolation_end(frame_num, args)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 break
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120 self.position2_interpolator.update(frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
121 self.fog_interpolator.update(frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
122 self.position_interpolator.update(frame)
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123