annotate pytouhou/ui/gamerunner.py @ 374:6a63fd3deb76

Use an iterator to get the keystates from a replay.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 05 Aug 2012 18:19:42 +0200
parents 6deab6ad8be8
children 11d895b6c0dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 import pyglet
146
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
16 import traceback
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
17 from itertools import chain
146
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
18
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
19 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, glDisable,
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
20 glHint, glEnableClientState, glViewport, glScissor,
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
21 glLoadMatrixf,
203
df8b2ab54639 Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
22 GL_MODELVIEW, GL_PROJECTION,
df8b2ab54639 Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
23 GL_TEXTURE_2D, GL_BLEND, GL_FOG,
df8b2ab54639 Make pylint slightly happier (and code analysis easier)
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
24 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
25 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
26 GL_SCISSOR_TEST)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
224
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
28 from pytouhou.utils.helpers import get_logger
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
29 from pytouhou.utils.matrix import Matrix
224
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
30
205
ee6dfd14a785 Rename pytouhou.opengl to pytouhou.ui, makes much more sense that way.
Thibaut Girka <thib@sitedethib.com>
parents: 203
diff changeset
31 from .gamerenderer import GameRenderer
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 341
diff changeset
32 from .music import MusicPlayer, SFXPlayer
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
33 from .shaders.eosd import GameShader, BackgroundShader
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35
224
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
36 logger = get_logger(__name__)
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
37
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
38
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 class GameRunner(pyglet.window.Window, GameRenderer):
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
40 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60, fixed_pipeline=False):
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 GameRenderer.__init__(self, resource_loader, game, background)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
42
334
4eca6130f118 Add options to set FPS limit and disable double buffering
Thibaut Girka <thib@sitedethib.com>
parents: 331
diff changeset
43 config = pyglet.gl.Config(double_buffer=double_buffer)
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
44 width, height = (game.interface.width, game.interface.height) if game else (None, None)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
45 pyglet.window.Window.__init__(self, width=width, height=height,
334
4eca6130f118 Add options to set FPS limit and disable double buffering
Thibaut Girka <thib@sitedethib.com>
parents: 331
diff changeset
46 caption='PyTouhou', resizable=False,
4eca6130f118 Add options to set FPS limit and disable double buffering
Thibaut Girka <thib@sitedethib.com>
parents: 331
diff changeset
47 config=config)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
48
334
4eca6130f118 Add options to set FPS limit and disable double buffering
Thibaut Girka <thib@sitedethib.com>
parents: 331
diff changeset
49 self.fps_limit = fps_limit
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
50 self.use_fixed_pipeline = fixed_pipeline
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
51 self.replay_level = None
331
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
52
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
53 if not self.use_fixed_pipeline:
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
54 self.game_shader = GameShader()
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
55 self.background_shader = BackgroundShader()
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
56 self.interface_shader = self.game_shader
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
57
331
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
58 if game:
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
59 self.load_game(game, background, replay)
330
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
60
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
61 self.clock = pyglet.clock.get_default()
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
62
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
63
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
64 def load_game(self, game=None, background=None, bgms=None, replay=None, save_keystates=None):
330
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
65 GameRenderer.load_game(self, game, background)
16ed1ab1e14b Add a GameRunner.load_game method to allow level changes.
Thibaut Girka <thib@sitedethib.com>
parents: 323
diff changeset
66 self.replay_level = None
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
67 if not replay or not replay.levels[game.stage-1]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
68 self.keys = pyglet.window.key.KeyStateHandler()
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
69 self.push_handlers(self.keys)
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
70 else:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
71 self.replay_level = replay.levels[game.stage-1]
374
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
72 self.keys = self.replay_level.iter_keystates()
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
73 game.players[0].state.lives = self.replay_level.lives
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
74 game.players[0].state.power = self.replay_level.power
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
75 game.players[0].state.bombs = self.replay_level.bombs
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
76 game.difficulty = self.replay_level.difficulty
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
77
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
78 self.save_keystates = save_keystates
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
79
341
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
80 game.music = MusicPlayer(game.resource_loader, bgms)
61caded6b4f5 Clean music playback API a little.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 334
diff changeset
81 game.music.play(0)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82
343
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 341
diff changeset
83 game.player_sfx = SFXPlayer(game.resource_loader)
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 341
diff changeset
84 game.enemy_sfx = SFXPlayer(game.resource_loader)
94fdb6c782c1 Implement sfx for player and enemies.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 341
diff changeset
85
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
87 def start(self, width=None, height=None):
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
88 width = width or (self.game.interface.width if self.game else 640)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
89 height = height or (self.game.interface.height if self.game else 480)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
90 if (width, height) != (self.width, self.height):
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
91 self.set_size(width, height)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 # Initialize OpenGL
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 glEnable(GL_BLEND)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 glEnable(GL_TEXTURE_2D)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 glHint(GL_FOG_HINT, GL_NICEST)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98 glEnableClientState(GL_COLOR_ARRAY)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
99 glEnableClientState(GL_VERTEX_ARRAY)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
100 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
101
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
102 self.proj = self.perspective(30, float(self.game.width) / float(self.game.height),
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
103 101010101./2010101., 101010101./10101.)
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
104 game_view = self.setup_camera(0, 0, 1)
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
105 self.game_mvp = game_view * self.proj
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
106 self.interface_mvp = self.ortho_2d(0., float(self.width), float(self.height), 0.)
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
107
334
4eca6130f118 Add options to set FPS limit and disable double buffering
Thibaut Girka <thib@sitedethib.com>
parents: 331
diff changeset
108 if self.fps_limit > 0:
4eca6130f118 Add options to set FPS limit and disable double buffering
Thibaut Girka <thib@sitedethib.com>
parents: 331
diff changeset
109 pyglet.clock.set_fps_limit(self.fps_limit)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
110 while not self.has_exit:
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
111 pyglet.clock.tick()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
112 self.dispatch_events()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 self.update()
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
114 self.render_game()
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
115 self.render_interface()
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116 self.flip()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118
146
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
119 def _event_text_symbol(self, ev):
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
120 # XXX: Ugly workaround to a pyglet bug on X11
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
121 #TODO: fix that bug in pyglet
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
122 try:
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
123 return pyglet.window.Window._event_text_symbol(self, ev)
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
124 except Exception as exc:
224
9bb26dbb8438 Remove useless and occasionally problematic (think about non-utf8 terminals) print calls.
Thibaut Girka <thib@sitedethib.com>
parents: 205
diff changeset
125 logger.warn('Pyglet error: %s', traceback.format_exc(exc))
146
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
126 return None, None
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
127
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
128
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 def on_key_press(self, symbol, modifiers):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130 if symbol == pyglet.window.key.ESCAPE:
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131 self.has_exit = True
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132 # XXX: Fullscreen will be enabled the day pyglet stops sucking
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133 elif symbol == pyglet.window.key.F11:
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 self.set_fullscreen(not self.fullscreen)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
137 def update(self):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
138 if self.background:
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 146
diff changeset
139 self.background.update(self.game.frame)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
140 if self.game:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
141 if not self.replay_level:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
142 #TODO: allow user settings
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
143 keystate = 0
353
451bee1d2cec Allow Z as well as W to shoot.
Thibaut Girka <thib@sitedethib.com>
parents: 345
diff changeset
144 if self.keys[pyglet.window.key.W] or self.keys[pyglet.window.key.Z]:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
145 keystate |= 1
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
146 if self.keys[pyglet.window.key.X]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
147 keystate |= 2
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
148 #TODO: on some configurations, LSHIFT is Shift_L when pressed
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
149 # and ISO_Prev_Group when released, confusing the hell out of pyglet
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
150 # and leading to a always-on LSHIFT...
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
151 if self.keys[pyglet.window.key.LSHIFT]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
152 keystate |= 4
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
153 if self.keys[pyglet.window.key.UP]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
154 keystate |= 16
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
155 if self.keys[pyglet.window.key.DOWN]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
156 keystate |= 32
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
157 if self.keys[pyglet.window.key.LEFT]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
158 keystate |= 64
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
159 if self.keys[pyglet.window.key.RIGHT]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
160 keystate |= 128
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
161 if self.keys[pyglet.window.key.LCTRL]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
162 keystate |= 256
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
163 else:
374
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
164 try:
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
165 keystate = self.keys.next()
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
166 except StopIteration:
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
167 keystate = 0
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
168
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
169 if self.save_keystates is not None:
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
170 self.save_keystates.append(keystate)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
171
331
1b4f04b08729 Add the story mode.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
172 self.game.run_iter(keystate)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
173
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
174
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
175 def render_game(self):
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
176 # Switch to game projection
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
177 #TODO: move that to GameRenderer?
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
178 x, y = self.game.interface.game_pos
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
179 glViewport(x, y, self.game.width, self.game.height)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
180 glScissor(x, y, self.game.width, self.game.height)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
181 glEnable(GL_SCISSOR_TEST)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
182
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
183 GameRenderer.render(self)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
184
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
185 glDisable(GL_SCISSOR_TEST)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
186
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
187
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
188 def render_interface(self):
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
189 interface = self.game.interface
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
190 interface.labels['framerate'].set_text('%.2ffps' % self.clock.get_fps())
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
191
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
192 if self.use_fixed_pipeline:
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
193 glMatrixMode(GL_MODELVIEW)
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
194 glLoadMatrixf(self.interface_mvp.get_c_data())
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
195 glDisable(GL_FOG)
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
196 else:
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
197 self.interface_shader.bind()
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
198 self.interface_shader.uniform_matrixf('mvp', self.interface_mvp.get_c_data())
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
199 glViewport(0, 0, self.width, self.height)
225
2d35565b5608 Move game size in the game's definition, and don't keep changing the window's size.
Thibaut Girka <thib@sitedethib.com>
parents: 224
diff changeset
200
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
201 items = [item for item in interface.items if item.anmrunner and item.anmrunner.running]
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
202 labels = interface.labels.values()
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
203
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
204 if items:
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
205 # Redraw all the interface
300
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
206 self.render_elements(items)
da53bc29b94a Add the game interface.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 233
diff changeset
207 else:
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
208 # Redraw only changed labels
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
209 labels = [label for label in labels if label.changed]
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
210
345
2c4589370cc6 Display a boss remaining lives and timeout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 343
diff changeset
211 if self.game.boss:
2c4589370cc6 Display a boss remaining lives and timeout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 343
diff changeset
212 self.render_elements(interface.boss_items)
2c4589370cc6 Display a boss remaining lives and timeout.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 343
diff changeset
213
323
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
214 self.render_elements(labels)
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
215 self.render_elements(chain(*(label.objects() for label in labels)))
2fcdb8966957 Display lives and bombs.
Thibaut Girka <thib@sitedethib.com>
parents: 321
diff changeset
216 for label in labels:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 300
diff changeset
217 label.changed = False
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
218