Mercurial > touhou
annotate pytouhou/game/background.py @ 64:d469012368b3
Implement conditional jumps.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 24 Aug 2011 22:44:40 -0700 |
parents | ab826bc29aa2 |
children | a142e57218a0 |
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 | 16 from io import BytesIO |
17 import os | |
18 import struct | |
19 from itertools import chain | |
20 | |
21 from pytouhou.utils.interpolator import Interpolator | |
15 | 22 from pytouhou.game.sprite import Sprite |
13 | 23 |
24 | |
25 class Background(object): | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
26 def __init__(self, stage, anm_wrapper): |
15 | 27 self.stage = stage |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
28 self.anm_wrapper = anm_wrapper |
13 | 29 self.objects = [] |
30 self.object_instances = [] | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
31 self.objects_by_texture = {} |
15 | 32 |
26
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
33 self.position_interpolator = Interpolator((0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
34 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
35 self.position2_interpolator = Interpolator((0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
36 |
13 | 37 self.build_objects() |
38 self.build_object_instances() | |
39 | |
40 | |
41 def build_object_instances(self): | |
42 self.object_instances = [] | |
43 for obj, ox, oy, oz in self.stage.object_instances: | |
44 obj_id = self.stage.objects.index(obj) | |
45 | |
46 obj_instance = [] | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
47 for face_vertices, face_uvs, face_colors in self.objects[obj_id]: |
13 | 48 obj_instance.append((tuple((x + ox, y + oy, z + oz) |
49 for x, y, z in face_vertices), | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
50 face_uvs, |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
51 face_colors)) |
13 | 52 self.object_instances.append(obj_instance) |
53 # Z-sorting | |
54 def keyfunc(obj): | |
55 return min(z for face in obj for x, y, z in face[0]) | |
56 self.object_instances.sort(key=keyfunc, reverse=True) | |
57 | |
58 | |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
59 def object_instances_to_vertices_uvs_colors(self): |
13 | 60 vertices = tuple(vertex for obj in self.object_instances |
61 for face in obj for vertex in face[0]) | |
62 uvs = tuple(uv for obj in self.object_instances | |
63 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
|
64 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
|
65 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
|
66 return vertices, uvs, colors |
13 | 67 |
68 | |
69 def build_objects(self): | |
70 self.objects = [] | |
71 for i, obj in enumerate(self.stage.objects): | |
72 faces = [] | |
15 | 73 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
|
74 #TODO: per-texture rendering |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
75 anm, sprite = self.anm_wrapper.get_sprite(script_index) |
29
afa91be769ae
Don't lose time updating off-screen enemies' sprites
Thibaut Girka <thib@sitedethib.com>
parents:
26
diff
changeset
|
76 if sprite.update(): |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
77 sprite.update_vertices_uvs_colors(width_override, height_override) |
15 | 78 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
|
79 colors = sprite._colors |
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
80 faces.append((vertices, uvs, colors)) |
13 | 81 self.objects.append(faces) |
82 | |
83 | |
84 def update(self, frame): | |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
85 if not self.objects_by_texture: |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
86 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
|
87 nb_vertices = len(vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
88 vertices_format = 'f' * (3 * nb_vertices) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
89 uvs_format = 'f' * (2 * nb_vertices) |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
90 colors_format = 'B' * (4 * nb_vertices) |
16
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
91 vertices = struct.pack(vertices_format, *chain(*vertices)) |
66ce9bb440ac
Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
92 uvs = struct.pack(uvs_format, *chain(*uvs)) |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
93 colors = struct.pack(colors_format, *chain(*colors)) |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
94 assert len(self.anm_wrapper.anm_files) == 1 #TODO |
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
95 anm = self.anm_wrapper.anm_files[0] |
37
a10e3f44a883
Add alpha (anm0 instruction 3) support
Thibaut Girka <thib@sitedethib.com>
parents:
29
diff
changeset
|
96 self.objects_by_texture = {(anm.first_name, anm.secondary_name): (nb_vertices, vertices, uvs, colors)} |
13 | 97 |
98 for frame_num, message_type, args in self.stage.script: | |
99 if frame_num == frame: | |
14 | 100 if message_type == 0: |
101 self.position_interpolator.set_interpolation_start(frame_num, args) | |
102 elif message_type == 1: | |
13 | 103 self.fog_interpolator.set_interpolation_end_values(args) |
14 | 104 elif message_type == 2: |
105 self.position2_interpolator.set_interpolation_end_values(args) | |
13 | 106 elif message_type == 3: |
107 duration, = args | |
108 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration) | |
109 elif message_type == 4: | |
110 duration, = args | |
111 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration) | |
112 if frame_num > frame and message_type == 0: | |
113 self.position_interpolator.set_interpolation_end(frame_num, args) | |
114 break | |
115 | |
116 self.position2_interpolator.update(frame) | |
117 self.fog_interpolator.update(frame) | |
118 self.position_interpolator.update(frame) | |
119 |