comparison pytouhou/opengl/gamerunner.py @ 131:fab7ad2f0d8b

Use Cython, improve performances!
author Thibaut Girka <thib@sitedethib.com>
date Sun, 11 Sep 2011 02:02:59 +0200
parents
children 96c30ffd9b87
comparison
equal deleted inserted replaced
130:11ab06f4c4c6 131:fab7ad2f0d8b
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 import pyglet
16 from pyglet.gl import *
17
18 from pytouhou.opengl.texture import TextureManager
19 from pytouhou.opengl.sprite import get_sprite_rendering_data #TODO: cimport?
20 from pytouhou.opengl.background import get_background_rendering_data
21 from pytouhou.opengl.gamerenderer import GameRenderer
22
23
24 class GameRunner(pyglet.window.Window, GameRenderer):
25 def __init__(self, resource_loader, game=None, background=None):
26 GameRenderer.__init__(self, resource_loader, game, background)
27 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False)
28 self.keys = pyglet.window.key.KeyStateHandler()
29 self.push_handlers(self.keys)
30
31 self.fps_display = pyglet.clock.ClockDisplay()
32
33
34 def start(self, width=384, height=448):
35 self.set_size(width, height)
36
37 # Initialize OpenGL
38 glMatrixMode(GL_PROJECTION)
39 glLoadIdentity()
40 gluPerspective(30, float(width)/float(height),
41 101010101./2010101., 101010101./10101.)
42
43 glEnable(GL_BLEND)
44 glEnable(GL_TEXTURE_2D)
45 glEnable(GL_FOG)
46 glHint(GL_FOG_HINT, GL_NICEST)
47 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
48 glEnableClientState(GL_COLOR_ARRAY)
49 glEnableClientState(GL_VERTEX_ARRAY)
50 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
51
52 # Use our own loop to ensure 60 (for now, 120) fps
53 pyglet.clock.set_fps_limit(120)
54 while not self.has_exit:
55 pyglet.clock.tick()
56 self.dispatch_events()
57 self.update()
58 self.on_draw()
59 self.flip()
60
61
62 def on_resize(self, width, height):
63 glViewport(0, 0, width, height)
64
65
66 def on_key_press(self, symbol, modifiers):
67 if symbol == pyglet.window.key.ESCAPE:
68 self.has_exit = True
69 # XXX: Fullscreen will be enabled the day pyglet stops sucking
70 elif symbol == pyglet.window.key.F11:
71 self.set_fullscreen(not self.fullscreen)
72
73
74 def update(self):
75 if self.background:
76 self.background.update(self.game.game_state.frame)
77 if self.game:
78 #TODO: allow user settings
79 keystate = 0
80 if self.keys[pyglet.window.key.W]:
81 keystate |= 1
82 if self.keys[pyglet.window.key.X]:
83 keystate |= 2
84 #TODO: on some configurations, LSHIFT is Shift_L when pressed
85 # and ISO_Prev_Group when released, confusing the hell out of pyglet
86 # and leading to a always-on LSHIFT...
87 if self.keys[pyglet.window.key.LSHIFT]:
88 keystate |= 4
89 if self.keys[pyglet.window.key.UP]:
90 keystate |= 16
91 if self.keys[pyglet.window.key.DOWN]:
92 keystate |= 32
93 if self.keys[pyglet.window.key.LEFT]:
94 keystate |= 64
95 if self.keys[pyglet.window.key.RIGHT]:
96 keystate |= 128
97 if self.keys[pyglet.window.key.LCTRL]:
98 keystate |= 256
99 self.game.run_iter(keystate) #TODO: self.keys...
100
101
102 def on_draw(self):
103 GameRenderer.render(self)
104
105 #TODO
106 glMatrixMode(GL_MODELVIEW)
107 glLoadIdentity()
108 gluLookAt(192., 224., 835.979370,
109 192, 224., 0., 0., 1., 0.)
110 self.fps_display.draw()
111