annotate pytouhou/opengl/gamerunner.py @ 187:46793ccfedca

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