comparison pytouhou/ui/gamerunner.py @ 370:74471afbac37

Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 27 Jul 2012 18:43:48 +0200
parents 451bee1d2cec
children 6deab6ad8be8
comparison
equal deleted inserted replaced
369:f305cdd6f6c5 370:74471afbac37
16 import traceback 16 import traceback
17 from itertools import chain 17 from itertools import chain
18 18
19 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, glDisable, 19 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, glDisable,
20 glHint, glEnableClientState, glViewport, glScissor, 20 glHint, glEnableClientState, glViewport, glScissor,
21 gluPerspective, gluOrtho2D, 21 glLoadMatrixf,
22 GL_MODELVIEW, GL_PROJECTION, 22 GL_MODELVIEW, GL_PROJECTION,
23 GL_TEXTURE_2D, GL_BLEND, GL_FOG, 23 GL_TEXTURE_2D, GL_BLEND, GL_FOG,
24 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, 24 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST,
25 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) 26 GL_SCISSOR_TEST)
27 27
28 from pytouhou.utils.helpers import get_logger 28 from pytouhou.utils.helpers import get_logger
29 from pytouhou.utils.matrix import Matrix
29 30
30 from .gamerenderer import GameRenderer 31 from .gamerenderer import GameRenderer
31 from .music import MusicPlayer, SFXPlayer 32 from .music import MusicPlayer, SFXPlayer
33 from .shaders.eosd import GameShader, BackgroundShader
32 34
33 35
34 logger = get_logger(__name__) 36 logger = get_logger(__name__)
35 37
36 38
37 class GameRunner(pyglet.window.Window, GameRenderer): 39 class GameRunner(pyglet.window.Window, GameRenderer):
38 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60): 40 def __init__(self, resource_loader, game=None, background=None, replay=None, double_buffer=True, fps_limit=60, fixed_pipeline=False):
39 GameRenderer.__init__(self, resource_loader, game, background) 41 GameRenderer.__init__(self, resource_loader, game, background)
40 42
41 config = pyglet.gl.Config(double_buffer=double_buffer) 43 config = pyglet.gl.Config(double_buffer=double_buffer)
42 width, height = (game.interface.width, game.interface.height) if game else (None, None) 44 width, height = (game.interface.width, game.interface.height) if game else (None, None)
43 pyglet.window.Window.__init__(self, width=width, height=height, 45 pyglet.window.Window.__init__(self, width=width, height=height,
44 caption='PyTouhou', resizable=False, 46 caption='PyTouhou', resizable=False,
45 config=config) 47 config=config)
46 48
47 self.fps_limit = fps_limit 49 self.fps_limit = fps_limit
50 self.use_fixed_pipeline = fixed_pipeline
48 self.replay_level = None 51 self.replay_level = None
52
53 if not self.use_fixed_pipeline:
54 self.game_shader = GameShader()
55 self.background_shader = BackgroundShader()
56 self.interface_shader = self.game_shader
49 57
50 if game: 58 if game:
51 self.load_game(game, background, replay) 59 self.load_game(game, background, replay)
52 60
53 self.clock = pyglet.clock.get_default() 61 self.clock = pyglet.clock.get_default()
81 self.set_size(width, height) 89 self.set_size(width, height)
82 90
83 # Initialize OpenGL 91 # Initialize OpenGL
84 glEnable(GL_BLEND) 92 glEnable(GL_BLEND)
85 glEnable(GL_TEXTURE_2D) 93 glEnable(GL_TEXTURE_2D)
86 glEnable(GL_FOG)
87 glHint(GL_FOG_HINT, GL_NICEST) 94 glHint(GL_FOG_HINT, GL_NICEST)
88 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) 95 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
89 glEnableClientState(GL_COLOR_ARRAY) 96 glEnableClientState(GL_COLOR_ARRAY)
90 glEnableClientState(GL_VERTEX_ARRAY) 97 glEnableClientState(GL_VERTEX_ARRAY)
91 glEnableClientState(GL_TEXTURE_COORD_ARRAY) 98 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
99
100 self.proj = self.perspective(30, float(self.game.width) / float(self.game.height),
101 101010101./2010101., 101010101./10101.)
102 game_view = self.setup_camera(0, 0, 1)
103 self.game_mvp = game_view * self.proj
104 self.interface_mvp = self.ortho_2d(0., float(self.width), float(self.height), 0.)
92 105
93 if self.fps_limit > 0: 106 if self.fps_limit > 0:
94 pyglet.clock.set_fps_limit(self.fps_limit) 107 pyglet.clock.set_fps_limit(self.fps_limit)
95 while not self.has_exit: 108 while not self.has_exit:
96 pyglet.clock.tick() 109 pyglet.clock.tick()
161 #TODO: move that to GameRenderer? 174 #TODO: move that to GameRenderer?
162 x, y = self.game.interface.game_pos 175 x, y = self.game.interface.game_pos
163 glViewport(x, y, self.game.width, self.game.height) 176 glViewport(x, y, self.game.width, self.game.height)
164 glScissor(x, y, self.game.width, self.game.height) 177 glScissor(x, y, self.game.width, self.game.height)
165 glEnable(GL_SCISSOR_TEST) 178 glEnable(GL_SCISSOR_TEST)
166 glMatrixMode(GL_PROJECTION)
167 glLoadIdentity()
168 gluPerspective(30, float(self.game.width) / float(self.game.height),
169 101010101./2010101., 101010101./10101.)
170 179
171 GameRenderer.render(self) 180 GameRenderer.render(self)
172 181
173 glDisable(GL_SCISSOR_TEST) 182 glDisable(GL_SCISSOR_TEST)
174 183
175 184
176 def render_interface(self): 185 def render_interface(self):
177 # Interface
178 interface = self.game.interface 186 interface = self.game.interface
179 interface.labels['framerate'].set_text('%.2ffps' % self.clock.get_fps()) 187 interface.labels['framerate'].set_text('%.2ffps' % self.clock.get_fps())
180 188
181 glMatrixMode(GL_PROJECTION) 189 if self.use_fixed_pipeline:
182 glLoadIdentity() 190 glMatrixMode(GL_MODELVIEW)
183 glMatrixMode(GL_MODELVIEW) 191 glLoadMatrixf(self.interface_mvp.get_c_data())
184 glLoadIdentity() 192 glDisable(GL_FOG)
185 gluOrtho2D(0., float(self.width), float(self.height), 0.) 193 else:
194 self.interface_shader.bind()
195 self.interface_shader.uniform_matrixf('mvp', self.interface_mvp.get_c_data())
186 glViewport(0, 0, self.width, self.height) 196 glViewport(0, 0, self.width, self.height)
187 197
188 items = [item for item in interface.items if item.anmrunner and item.anmrunner.running] 198 items = [item for item in interface.items if item.anmrunner and item.anmrunner.running]
189 labels = interface.labels.values() 199 labels = interface.labels.values()
190 200