Mercurial > touhou
annotate pytouhou/game/background.py @ 792:11bc22bad1bf
python: Replace the image crate with png
We weren’t using any of its features anyway, so the png crate is exactly what
we need, without the many heavy dependencies of image.
https://github.com/image-rs/image-png/pull/670 will eventually make it even
faster to build.
| author | Link Mauve <linkmauve@linkmauve.fr> |
|---|---|
| date | Sat, 17 Jan 2026 22:22:25 +0100 |
| parents | d1f0bb0b7a17 |
| children |
| 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 |
|
547
e35bef07290d
Always import runners from pytouhou.vm, to allow their replacement.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
433
diff
changeset
|
17 from pytouhou.vm import ANMRunner |
| 15 | 18 from pytouhou.game.sprite import Sprite |
| 13 | 19 |
| 20 | |
|
615
d1f0bb0b7a17
Don’t inherit explicitely from object, we are not on Python 2.7 anymore. :)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
547
diff
changeset
|
21 class Background: |
|
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
332
diff
changeset
|
22 def __init__(self, stage, anm): |
| 15 | 23 self.stage = stage |
|
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
332
diff
changeset
|
24 self.anm = anm |
| 332 | 25 self.last_frame = -1 |
|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
26 |
|
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
27 self.models = [] |
| 13 | 28 self.object_instances = [] |
|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
29 self.anm_runners = [] |
| 15 | 30 |
|
26
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
31 self.position_interpolator = Interpolator((0, 0, 0)) |
|
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
32 self.fog_interpolator = Interpolator((0, 0, 0, 0, 0)) |
|
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
33 self.position2_interpolator = Interpolator((0, 0, 0)) |
|
f17122405121
Basic sprite animation support
Thibaut Girka <thib@sitedethib.com>
parents:
21
diff
changeset
|
34 |
|
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
35 self.build_models() |
| 13 | 36 self.build_object_instances() |
| 37 | |
| 38 | |
| 39 def build_object_instances(self): | |
| 40 self.object_instances = [] | |
|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
41 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
|
42 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
|
43 # Z-sorting: |
|
340fcda8e64a
Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents:
108
diff
changeset
|
44 # TODO z-sorting may be needed at each iteration |
| 13 | 45 def keyfunc(obj): |
|
111
340fcda8e64a
Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents:
108
diff
changeset
|
46 return obj[2] + self.stage.models[obj[3]].bounding_box[2] |
| 13 | 47 self.object_instances.sort(key=keyfunc, reverse=True) |
| 48 | |
| 49 | |
|
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
50 def build_models(self): |
|
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
92
diff
changeset
|
51 self.models = [] |
|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
52 for obj in self.stage.models: |
|
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
53 quads = [] |
| 15 | 54 for script_index, ox, oy, oz, width_override, height_override in obj.quads: |
|
120
4300a832f033
Small refactoring and massive performance improvement
Thibaut Girka <thib@sitedethib.com>
parents:
111
diff
changeset
|
55 sprite = Sprite(width_override, height_override) |
|
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
332
diff
changeset
|
56 anm_runner = ANMRunner(self.anm, script_index, sprite) |
|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
57 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
|
58 self.anm_runners.append(anm_runner) |
|
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
59 self.models.append(quads) |
|
92
85f3b8ba3f24
Minor refactoring and optimizations. Drop stageviewer.
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
60 |
|
85f3b8ba3f24
Minor refactoring and optimizations. Drop stageviewer.
Thibaut Girka <thib@sitedethib.com>
parents:
90
diff
changeset
|
61 |
| 13 | 62 def update(self, frame): |
| 63 for frame_num, message_type, args in self.stage.script: | |
| 332 | 64 if self.last_frame < frame_num <= frame: |
| 14 | 65 if message_type == 0: |
| 66 self.position_interpolator.set_interpolation_start(frame_num, args) | |
| 67 elif message_type == 1: | |
| 13 | 68 self.fog_interpolator.set_interpolation_end_values(args) |
| 14 | 69 elif message_type == 2: |
| 70 self.position2_interpolator.set_interpolation_end_values(args) | |
| 13 | 71 elif message_type == 3: |
| 72 duration, = args | |
| 73 self.position2_interpolator.set_interpolation_end_frame(frame_num + duration) | |
| 74 elif message_type == 4: | |
| 75 duration, = args | |
| 76 self.fog_interpolator.set_interpolation_end_frame(frame_num + duration) | |
| 77 if frame_num > frame and message_type == 0: | |
| 78 self.position_interpolator.set_interpolation_end(frame_num, args) | |
| 79 break | |
| 80 | |
| 332 | 81 for i in range(frame - self.last_frame): |
| 82 for anm_runner in tuple(self.anm_runners): | |
| 83 if not anm_runner.run_frame(): | |
| 84 self.anm_runners.remove(anm_runner) | |
|
108
2a03940deea3
Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
94
diff
changeset
|
85 |
| 13 | 86 self.position2_interpolator.update(frame) |
| 87 self.fog_interpolator.update(frame) | |
| 88 self.position_interpolator.update(frame) | |
| 89 | |
| 332 | 90 self.last_frame = frame |
| 91 |
