Mercurial > touhou
annotate pytouhou/game/background.py @ 119:fad7b44cebf2
Switch from pygame + PyOpenGL to pyglet
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Wed, 07 Sep 2011 18:12:24 +0200 |
parents | 340fcda8e64a |
children | 4300a832f033 |
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 pytouhou.utils.interpolator import Interpolator |
69
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
17 from pytouhou.vm.anmrunner import ANMRunner |
15 | 18 from pytouhou.game.sprite import Sprite |
13 | 19 |
20 | |
21 class Background(object): | |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
22 def __init__(self, stage, anm_wrapper): |
15 | 23 self.stage = stage |
21
bf225780973f
Small refactoring, and Rumia \o/
Thibaut Girka <thib@sitedethib.com>
parents:
17
diff
changeset
|
24 self.anm_wrapper = anm_wrapper |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
25 |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
26 self.models = [] |
13 | 27 self.object_instances = [] |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
28 self.anm_runners = [] |
15 | 29 |
26
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
30 self.position_interpolator = Interpolator((0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
31 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
32 self.position2_interpolator = Interpolator((0, 0, 0)) |
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
33 |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
34 self.build_models() |
13 | 35 self.build_object_instances() |
36 | |
37 | |
38 def build_object_instances(self): | |
39 self.object_instances = [] | |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
40 for model_id, ox, oy, oz in self.stage.object_instances: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
41 self.object_instances.append((ox, oy, oz, model_id, self.models[model_id])) |
111
340fcda8e64a
Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents:
108
diff
changeset
|
42 # Z-sorting: |
340fcda8e64a
Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents:
108
diff
changeset
|
43 # TODO z-sorting may be needed at each iteration |
13 | 44 def keyfunc(obj): |
111
340fcda8e64a
Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents:
108
diff
changeset
|
45 return obj[2] + self.stage.models[obj[3]].bounding_box[2] |
13 | 46 self.object_instances.sort(key=keyfunc, reverse=True) |
47 | |
48 | |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
49 def build_models(self): |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
50 self.models = [] |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
51 for obj in self.stage.models: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
52 quads = [] |
15 | 53 for script_index, ox, oy, oz, width_override, height_override in obj.quads: |
69
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
54 sprite = Sprite() |
a142e57218a0
Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents:
52
diff
changeset
|
55 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
|
56 anm_runner.run_frame() |
90 | 57 sprite.update(width_override, height_override) |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
58 quads.append((ox, oy, oz, width_override, height_override, sprite)) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
59 self.anm_runners.append(anm_runner) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
60 self.models.append(quads) |
92
85f3b8ba3f24
Minor refactoring and optimizations. Drop stageviewer.
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
61 |
85f3b8ba3f24
Minor refactoring and optimizations. Drop stageviewer.
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
62 |
13 | 63 def update(self, frame): |
64 for frame_num, message_type, args in self.stage.script: | |
65 if frame_num == frame: | |
14 | 66 if message_type == 0: |
67 self.position_interpolator.set_interpolation_start(frame_num, args) | |
68 elif message_type == 1: | |
13 | 69 self.fog_interpolator.set_interpolation_end_values(args) |
14 | 70 elif message_type == 2: |
71 self.position2_interpolator.set_interpolation_end_values(args) | |
13 | 72 elif message_type == 3: |
73 duration, = args | |
74 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration) | |
75 elif message_type == 4: | |
76 duration, = args | |
77 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration) | |
78 if frame_num > frame and message_type == 0: | |
79 self.position_interpolator.set_interpolation_end(frame_num, args) | |
80 break | |
81 | |
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
82 for anm_runner in tuple(self.anm_runners): |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
83 if not anm_runner.run_frame(): |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
84 self.anm_runners.remove(anm_runner) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
85 |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
86 for model in self.models: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
87 for ox, oy, oz, width_override, height_override, sprite in model: |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
88 sprite.update(width_override, height_override) |
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
89 |
13 | 90 self.position2_interpolator.update(frame) |
91 self.fog_interpolator.update(frame) | |
92 self.position_interpolator.update(frame) | |
93 |