annotate pytouhou/opengl/gamerunner.py @ 197:e1bc8c4cbb1a

Do the right action when collecting an item.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 30 Oct 2011 11:29:08 -0700
parents 008f90ebfdc0
children df8b2ab54639
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
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
17
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from pyglet.gl import *
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 from pytouhou.opengl.gamerenderer import GameRenderer
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 class GameRunner(pyglet.window.Window, GameRenderer):
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
24 def __init__(self, resource_loader, game=None, background=None, replay=None):
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 GameRenderer.__init__(self, resource_loader, game, background)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False)
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
27 self.replay_level = None
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
28 if not replay or not replay.levels[game.stage-1]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
29 self.keys = pyglet.window.key.KeyStateHandler()
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
30 self.push_handlers(self.keys)
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
31 else:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
32 self.keys = 0
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
33 self.replay_level = replay.levels[game.stage-1]
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 self.fps_display = pyglet.clock.ClockDisplay()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 def start(self, width=384, height=448):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 self.set_size(width, height)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41 # Initialize OpenGL
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 glMatrixMode(GL_PROJECTION)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 glLoadIdentity()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 gluPerspective(30, float(width)/float(height),
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 101010101./2010101., 101010101./10101.)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 glEnable(GL_BLEND)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 glEnable(GL_TEXTURE_2D)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 glEnable(GL_FOG)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 glHint(GL_FOG_HINT, GL_NICEST)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 glEnableClientState(GL_COLOR_ARRAY)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 glEnableClientState(GL_VERTEX_ARRAY)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 # Use our own loop to ensure 60 (for now, 120) fps
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 pyglet.clock.set_fps_limit(120)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 while not self.has_exit:
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 pyglet.clock.tick()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 self.dispatch_events()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 self.update()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 self.on_draw()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 self.flip()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 def on_resize(self, width, height):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 glViewport(0, 0, width, height)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69
146
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
70 def _event_text_symbol(self, ev):
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
71 # 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
72 #TODO: fix that bug in pyglet
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
73 try:
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
74 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
75 except Exception as exc:
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
76 print('*WARNING* Pyglet error:')
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
77 traceback.print_exc(exc)
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
78 return None, None
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
79
96c30ffd9b87 Evil workaround to pyglet's X11 backend
Thibaut Girka <thib@sitedethib.com>
parents: 131
diff changeset
80
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 def on_key_press(self, symbol, modifiers):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 if symbol == pyglet.window.key.ESCAPE:
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 self.has_exit = True
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 # XXX: Fullscreen will be enabled the day pyglet stops sucking
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 elif symbol == pyglet.window.key.F11:
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 self.set_fullscreen(not self.fullscreen)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 def update(self):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 if self.background:
151
5cf927cbd9c5 Merge GameState into Game. TODO: Merge PlayerState into Player
Thibaut Girka <thib@sitedethib.com>
parents: 146
diff changeset
91 self.background.update(self.game.frame)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 if self.game:
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
93 if not self.replay_level:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
94 #TODO: allow user settings
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
95 keystate = 0
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
96 if self.keys[pyglet.window.key.W]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
97 keystate |= 1
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
98 if self.keys[pyglet.window.key.X]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
99 keystate |= 2
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
100 #TODO: on some configurations, LSHIFT is Shift_L when pressed
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
101 # 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
102 # and leading to a always-on LSHIFT...
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
103 if self.keys[pyglet.window.key.LSHIFT]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
104 keystate |= 4
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
105 if self.keys[pyglet.window.key.UP]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
106 keystate |= 16
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
107 if self.keys[pyglet.window.key.DOWN]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
108 keystate |= 32
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
109 if self.keys[pyglet.window.key.LEFT]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
110 keystate |= 64
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
111 if self.keys[pyglet.window.key.RIGHT]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
112 keystate |= 128
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
113 if self.keys[pyglet.window.key.LCTRL]:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
114 keystate |= 256
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
115 self.game.run_iter(keystate)
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
116 else:
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
117 keystate = 0
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
118 for frame, _keystate, unknown in self.replay_level.keys:
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
119 if self.game.frame < frame:
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
120 break
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
121 else:
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
122 keystate = _keystate
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 158
diff changeset
123
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
124 self.game.run_iter(keystate)
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127 def on_draw(self):
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 GameRenderer.render(self)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130 #TODO
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131 glMatrixMode(GL_MODELVIEW)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132 glLoadIdentity()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133 gluLookAt(192., 224., 835.979370,
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 192, 224., 0., 0., 1., 0.)
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135 self.fps_display.draw()
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136