comparison pytouhou/ui/gamerunner.py @ 300:da53bc29b94a

Add the game interface.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 10 Mar 2012 17:47:03 +0100
parents 067f6d9c562b
children f3099ebf4f61
comparison
equal deleted inserted replaced
299:e04e402e6380 300:da53bc29b94a
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 import pyglet 15 import pyglet
16 import traceback 16 import traceback
17 from itertools import chain
17 18
18 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, 19 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, glDisable,
19 glHint, glEnableClientState, glViewport, 20 glHint, glEnableClientState, glViewport, glScissor,
20 gluPerspective, gluOrtho2D, 21 gluPerspective, gluOrtho2D,
21 GL_MODELVIEW, GL_PROJECTION, 22 GL_MODELVIEW, GL_PROJECTION,
22 GL_TEXTURE_2D, GL_BLEND, GL_FOG, 23 GL_TEXTURE_2D, GL_BLEND, GL_FOG,
23 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, 24 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY) 25 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
26 GL_SCISSOR_TEST)
25 27
26 from pytouhou.utils.helpers import get_logger 28 from pytouhou.utils.helpers import get_logger
27 29
28 from .gamerenderer import GameRenderer 30 from .gamerenderer import GameRenderer
29 31
33 35
34 class GameRunner(pyglet.window.Window, GameRenderer): 36 class GameRunner(pyglet.window.Window, GameRenderer):
35 def __init__(self, resource_loader, game=None, background=None, replay=None): 37 def __init__(self, resource_loader, game=None, background=None, replay=None):
36 GameRenderer.__init__(self, resource_loader, game, background) 38 GameRenderer.__init__(self, resource_loader, game, background)
37 39
38 width, height = (game.width, game.height) if game else (None, None) 40 width, height = (game.interface.width, game.interface.height) if game else (None, None)
39 pyglet.window.Window.__init__(self, width=width, height=height, 41 pyglet.window.Window.__init__(self, width=width, height=height,
40 caption='PyTouhou', resizable=False) 42 caption='PyTouhou', resizable=False)
41 43
42 self.replay_level = None 44 self.replay_level = None
43 if not replay or not replay.levels[game.stage-1]: 45 if not replay or not replay.levels[game.stage-1]:
49 self.game.players[0].state.lives = self.replay_level.lives 51 self.game.players[0].state.lives = self.replay_level.lives
50 self.game.players[0].state.power = self.replay_level.power 52 self.game.players[0].state.power = self.replay_level.power
51 self.game.players[0].state.bombs = self.replay_level.bombs 53 self.game.players[0].state.bombs = self.replay_level.bombs
52 self.game.difficulty = self.replay_level.difficulty 54 self.game.difficulty = self.replay_level.difficulty
53 55
54 self.fps_display = pyglet.clock.ClockDisplay() 56 self.clock = pyglet.clock.get_default()
55 57
56 58
57 def start(self, width=None, height=None): 59 def start(self, width=None, height=None):
58 width = width or (self.game.width if self.game else 640) 60 width = width or (self.game.interface.width if self.game else 640)
59 height = height or (self.game.height if self.game else 480) 61 height = height or (self.game.interface.height if self.game else 480)
60
61 if (width, height) != (self.width, self.height): 62 if (width, height) != (self.width, self.height):
62 self.set_size(width, height) 63 self.set_size(width, height)
63 64
64 # Initialize OpenGL 65 # Initialize OpenGL
65 glEnable(GL_BLEND) 66 glEnable(GL_BLEND)
75 pyglet.clock.set_fps_limit(120) 76 pyglet.clock.set_fps_limit(120)
76 while not self.has_exit: 77 while not self.has_exit:
77 pyglet.clock.tick() 78 pyglet.clock.tick()
78 self.dispatch_events() 79 self.dispatch_events()
79 self.update() 80 self.update()
80 self.on_draw() 81 self.render_game()
82 self.render_interface()
81 self.flip() 83 self.flip()
82
83
84 def on_resize(self, width, height):
85 glViewport(0, 0, width, height)
86 84
87 85
88 def _event_text_symbol(self, ev): 86 def _event_text_symbol(self, ev):
89 # XXX: Ugly workaround to a pyglet bug on X11 87 # XXX: Ugly workaround to a pyglet bug on X11
90 #TODO: fix that bug in pyglet 88 #TODO: fix that bug in pyglet
139 keystate = _keystate 137 keystate = _keystate
140 138
141 self.game.run_iter(keystate) 139 self.game.run_iter(keystate)
142 140
143 141
144 def on_draw(self): 142 def render_game(self):
145 # Switch to game projection 143 # Switch to game projection
146 #TODO: move that to GameRenderer? 144 #TODO: move that to GameRenderer?
145 x, y = self.game.interface.game_pos
146 glViewport(x, y, self.game.width, self.game.height)
147 glScissor(x, y, self.game.width, self.game.height)
148 glEnable(GL_SCISSOR_TEST)
147 glMatrixMode(GL_PROJECTION) 149 glMatrixMode(GL_PROJECTION)
148 glLoadIdentity() 150 glLoadIdentity()
149 gluPerspective(30, float(self.width) / float(self.height), 151 gluPerspective(30, float(self.game.width) / float(self.game.height),
150 101010101./2010101., 101010101./10101.) 152 101010101./2010101., 101010101./10101.)
151 153
152 GameRenderer.render(self) 154 GameRenderer.render(self)
153 155
154 # Get back to standard orthographic projection 156 glDisable(GL_SCISSOR_TEST)
157
158
159 def render_interface(self):
160 # Interface
161 interface = self.game.interface
162 interface.labels['framerate'].set_text('%.2ffps' % self.clock.get_fps())
163
155 glMatrixMode(GL_PROJECTION) 164 glMatrixMode(GL_PROJECTION)
156 glLoadIdentity() 165 glLoadIdentity()
157 glMatrixMode(GL_MODELVIEW) 166 glMatrixMode(GL_MODELVIEW)
158 glLoadIdentity() 167 glLoadIdentity()
168 gluOrtho2D(0., float(self.width), float(self.height), 0.)
169 glViewport(0, 0, self.width, self.height)
159 170
160 #TODO: draw interface 171 items = [item for item in interface.items if item._anmrunner and item._anmrunner._running]
161 gluOrtho2D(0., float(self.game.width), 0., float(self.game.height)) 172 labels = interface.labels
162 self.fps_display.draw() 173 if items:
174 # Force rendering of labels
175 self.render_elements(items)
176 self.render_elements(chain(*(label.objects()
177 for label in labels.itervalues())))
178 else:
179 self.render_elements(chain(*(label.objects()
180 for label in labels.itervalues()
181 if label._changed)))
182 for label in interface.labels.itervalues():
183 label._changed = False
163 184