Mercurial > touhou
annotate pytouhou/ui/gamerunner.pyx @ 748:63f155923ea8
Cargo.toml: Remove useless panic=abort
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 25 Feb 2020 20:36:09 +0100 |
parents | 80687f258001 |
children |
rev | line source |
---|---|
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
2 ## |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
4 ## |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
8 ## |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
13 ## |
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
14 |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
15 cimport cython |
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
16 |
635
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
17 from pytouhou.lib.gui cimport EXIT, PAUSE, SCREENSHOT, RESIZE, FULLSCREEN |
146
96c30ffd9b87
Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents:
131
diff
changeset
|
18 |
463
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
459
diff
changeset
|
19 from .window cimport Window, Runner |
528
7c3c90468996
Inherit music players from a base class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
511
diff
changeset
|
20 from .music import BGMPlayer, SFXPlayer |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
21 from pytouhou.game.game cimport Game |
528
7c3c90468996
Inherit music players from a base class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
511
diff
changeset
|
22 from pytouhou.game.music cimport MusicPlayer |
224
9bb26dbb8438
Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents:
205
diff
changeset
|
23 |
9bb26dbb8438
Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents:
205
diff
changeset
|
24 |
463
11708a1d0a1a
Make GameRunner inherit from a Runner base class, to bypass Python calls in Window.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
459
diff
changeset
|
25 cdef class GameRunner(Runner): |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
26 cdef object background, con, resource_loader, keys, replay_level, common |
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
27 cdef Game game |
458
1b56d62250ab
Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
456
diff
changeset
|
28 cdef Window window |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
29 cdef list save_keystates |
464
36bc577b2392
Make the window resizable, and scale its content correctly.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
463
diff
changeset
|
30 cdef bint skip |
458
1b56d62250ab
Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
456
diff
changeset
|
31 |
511
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
32 # Since we want to support multiple renderers, don’t specify its type. |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
33 #TODO: find a way to still specify its interface. |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
34 cdef object renderer |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
35 |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
36 def __init__(self, Window window, renderer, common, resource_loader, |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
37 bint skip=False, con=None): |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
38 self.renderer = renderer |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
39 self.common = common |
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
40 self.resource_loader = resource_loader |
419
1c92721f8e49
Re-implement frame control
Thibaut Girka <thib@sitedethib.com>
parents:
418
diff
changeset
|
41 |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
42 self.window = window |
187
46793ccfedca
Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
43 self.replay_level = None |
378
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
44 self.skip = skip |
486 | 45 self.con = con |
331
1b4f04b08729
Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
330
diff
changeset
|
46 |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
47 self.width = common.interface.width |
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
48 self.height = common.interface.height |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
49 |
330
16ed1ab1e14b
Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents:
323
diff
changeset
|
50 |
511
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
51 def load_game(self, Game game, background=None, bgms=None, replay=None, |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
52 save_keystates=None): |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
53 self.game = game |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
54 self.background = background |
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
55 |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
56 if self.renderer is not None: |
511
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
57 self.renderer.load_textures(self.resource_loader.instanced_anms) |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
58 self.renderer.load_background(background) |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
59 |
378
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
60 self.set_input(replay) |
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
61 if replay and replay.levels[game.stage - 1]: |
494
6be9c99a3a24
Merge PlayerState into Player, fix player respawn position.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
487
diff
changeset
|
62 game.players[0].lives = self.replay_level.lives |
6be9c99a3a24
Merge PlayerState into Player, fix player respawn position.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
487
diff
changeset
|
63 game.players[0].power = self.replay_level.power |
6be9c99a3a24
Merge PlayerState into Player, fix player respawn position.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
487
diff
changeset
|
64 game.players[0].bombs = self.replay_level.bombs |
341
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
334
diff
changeset
|
65 game.difficulty = self.replay_level.difficulty |
61caded6b4f5
Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
334
diff
changeset
|
66 |
373
6deab6ad8be8
Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
370
diff
changeset
|
67 self.save_keystates = save_keystates |
6deab6ad8be8
Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
370
diff
changeset
|
68 |
528
7c3c90468996
Inherit music players from a base class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
511
diff
changeset
|
69 null_player = MusicPlayer() |
511
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
70 if bgms is not None: |
528
7c3c90468996
Inherit music players from a base class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
511
diff
changeset
|
71 game.music = BGMPlayer(self.resource_loader, bgms) |
428
f41a26971a19
Remove all Loader uses from outside pytouhou.games, and add a --no-music option to disable bgm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
427
diff
changeset
|
72 game.music.play(0) |
f41a26971a19
Remove all Loader uses from outside pytouhou.games, and add a --no-music option to disable bgm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
427
diff
changeset
|
73 else: |
f41a26971a19
Remove all Loader uses from outside pytouhou.games, and add a --no-music option to disable bgm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
427
diff
changeset
|
74 game.music = null_player |
f41a26971a19
Remove all Loader uses from outside pytouhou.games, and add a --no-music option to disable bgm.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
427
diff
changeset
|
75 |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
76 game.sfx_player = SFXPlayer(self.resource_loader) if not self.skip else null_player |
343
94fdb6c782c1
Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
341
diff
changeset
|
77 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
78 |
617
a6af3ff86612
Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
606
diff
changeset
|
79 cdef bint set_input(self, replay=None) except True: |
378
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
80 if not replay or not replay.levels[self.game.stage-1]: |
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
81 self.replay_level = None |
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
82 else: |
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
83 self.replay_level = replay.levels[self.game.stage-1] |
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
84 self.keys = self.replay_level.iter_keystates() |
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
85 |
11d895b6c0dc
Add the debug feature to start a game at the end of a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
374
diff
changeset
|
86 |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
87 @cython.cdivision(True) |
617
a6af3ff86612
Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
606
diff
changeset
|
88 cdef bint set_renderer_size(self, long width, long height) except True: |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
89 if self.renderer is not None: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
90 runner_width = float(self.width) |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
91 runner_height = float(self.height) |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
92 |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
93 scale = min(width / runner_width, |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
94 height / runner_height) |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
95 |
511
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
96 new_width = <long>(runner_width * scale) |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
97 new_height = <long>(runner_height * scale) |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
98 |
511
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
99 x = (width - new_width) // 2 |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
100 y = (height - new_height) // 2 |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
101 |
2e8ceaa85d5c
Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
508
diff
changeset
|
102 self.renderer.size = x, y, new_width, new_height |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
103 |
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
104 |
617
a6af3ff86612
Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
606
diff
changeset
|
105 cdef bint start(self) except True: |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
106 if self.renderer is not None: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
107 self.set_renderer_size(self.width, self.height) |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
108 self.renderer.start(self.common) |
370
74471afbac37
Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
353
diff
changeset
|
109 |
131
fab7ad2f0d8b
Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff
changeset
|
110 |
617
a6af3ff86612
Change all “void except *” function into “bint except True”, to prevent PyErr_Occurred() from being called at each call.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
606
diff
changeset
|
111 cdef bint capture(self) except True: |
592
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
112 if self.renderer is not None: |
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
113 filename = 'screenshot/frame%06d.ppm' % self.game.frame |
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
114 self.renderer.capture(filename, self.width, self.height) |
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
115 |
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
116 |
606
3c2f96f1d715
Fix compilation under Cython 0.22, by making the pyx and the pxd declarations’ except clause similar.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
594
diff
changeset
|
117 cpdef bint update(self, bint render) except -1: |
458
1b56d62250ab
Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
456
diff
changeset
|
118 cdef long keystate |
592
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
119 capture = False |
458
1b56d62250ab
Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
456
diff
changeset
|
120 |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
121 if self.background is not None: |
151
5cf927cbd9c5
Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents:
146
diff
changeset
|
122 self.background.update(self.game.frame) |
635
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
123 for event, args in self.window.get_events(): |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
124 if event == EXIT: |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
125 return False |
635
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
126 elif event == PAUSE: |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
127 return False # TODO: implement the pause. |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
128 elif event == FULLSCREEN: |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
129 self.window.toggle_fullscreen() |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
130 elif event == SCREENSHOT: |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
131 capture = True |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
132 elif event == RESIZE: |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
133 width, height = args |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
134 self.set_renderer_size(width, height) |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
135 if self.window is not None: |
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
136 self.window.set_size(width, height) |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
137 if self.replay_level is None: |
635
80687f258001
Make sdl.Window inherit from gui.Window, so we can swap implementations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
617
diff
changeset
|
138 keystate = self.window.get_keystate() |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
139 else: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
140 try: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
141 keystate = self.keys.next() |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
142 except StopIteration: |
187
46793ccfedca
Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
143 keystate = 0 |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
144 if self.skip: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
145 self.set_input() |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
146 self.skip = False |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
147 self.game.sfx_player = SFXPlayer(self.resource_loader) |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
148 |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
149 if self.save_keystates is not None: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
150 self.save_keystates.append(keystate) |
187
46793ccfedca
Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
158
diff
changeset
|
151 |
508
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
152 if self.con is not None: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
153 self.con.run_iter(self.game, keystate) |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
154 else: |
1bc014f9d572
Make GameRunner entirely independent of Window or GameRenderer, so we can run a game without display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
504
diff
changeset
|
155 self.game.run_iter([keystate]) |
373
6deab6ad8be8
Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
370
diff
changeset
|
156 |
549
56bca8ce4b68
Add a very simple sample interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
538
diff
changeset
|
157 labels = self.game.interface.labels |
56bca8ce4b68
Add a very simple sample interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
538
diff
changeset
|
158 if self.window is not None and 'framerate' in labels: |
56bca8ce4b68
Add a very simple sample interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
538
diff
changeset
|
159 labels['framerate'].set_text('%.2ffps' % self.window.get_fps()) |
56bca8ce4b68
Add a very simple sample interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
538
diff
changeset
|
160 |
594
12756994a92c
Make frameskip actually skip the rendering part, not just the buffer swap.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
592
diff
changeset
|
161 if render and not self.skip and self.renderer is not None: |
503
c622eaf64428
Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
494
diff
changeset
|
162 self.renderer.render(self.game) |
549
56bca8ce4b68
Add a very simple sample interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
538
diff
changeset
|
163 |
592
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
164 if capture: |
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
165 self.capture() |
19d930f9e3f0
Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
578
diff
changeset
|
166 |
422
52829ebe2561
Refactor window management in its own class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
421
diff
changeset
|
167 return True |