Mercurial > touhou
comparison pytouhou/ui/gamerunner.py @ 205:ee6dfd14a785
Rename pytouhou.opengl to pytouhou.ui, makes much more sense that way.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Tue, 01 Nov 2011 13:50:33 +0100 |
parents | pytouhou/opengl/gamerunner.py@df8b2ab54639 |
children | 9bb26dbb8438 |
comparison
equal
deleted
inserted
replaced
204:88361534c77e | 205:ee6dfd14a785 |
---|---|
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 import traceback | |
17 | |
18 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, | |
19 glHint, glEnableClientState, glViewport, | |
20 gluPerspective, gluLookAt, | |
21 GL_MODELVIEW, GL_PROJECTION, | |
22 GL_TEXTURE_2D, GL_BLEND, GL_FOG, | |
23 GL_PERSPECTIVE_CORRECTION_HINT, GL_FOG_HINT, GL_NICEST, | |
24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY) | |
25 | |
26 from .gamerenderer import GameRenderer | |
27 | |
28 | |
29 class GameRunner(pyglet.window.Window, GameRenderer): | |
30 def __init__(self, resource_loader, game=None, background=None, replay=None): | |
31 GameRenderer.__init__(self, resource_loader, game, background) | |
32 pyglet.window.Window.__init__(self, caption='PyTouhou', resizable=False) | |
33 self.replay_level = None | |
34 if not replay or not replay.levels[game.stage-1]: | |
35 self.keys = pyglet.window.key.KeyStateHandler() | |
36 self.push_handlers(self.keys) | |
37 else: | |
38 self.keys = 0 | |
39 self.replay_level = replay.levels[game.stage-1] | |
40 | |
41 self.fps_display = pyglet.clock.ClockDisplay() | |
42 | |
43 | |
44 def start(self, width=384, height=448): | |
45 self.set_size(width, height) | |
46 | |
47 # Initialize OpenGL | |
48 glMatrixMode(GL_PROJECTION) | |
49 glLoadIdentity() | |
50 gluPerspective(30, float(width)/float(height), | |
51 101010101./2010101., 101010101./10101.) | |
52 | |
53 glEnable(GL_BLEND) | |
54 glEnable(GL_TEXTURE_2D) | |
55 glEnable(GL_FOG) | |
56 glHint(GL_FOG_HINT, GL_NICEST) | |
57 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) | |
58 glEnableClientState(GL_COLOR_ARRAY) | |
59 glEnableClientState(GL_VERTEX_ARRAY) | |
60 glEnableClientState(GL_TEXTURE_COORD_ARRAY) | |
61 | |
62 # Use our own loop to ensure 60 (for now, 120) fps | |
63 pyglet.clock.set_fps_limit(120) | |
64 while not self.has_exit: | |
65 pyglet.clock.tick() | |
66 self.dispatch_events() | |
67 self.update() | |
68 self.on_draw() | |
69 self.flip() | |
70 | |
71 | |
72 def on_resize(self, width, height): | |
73 glViewport(0, 0, width, height) | |
74 | |
75 | |
76 def _event_text_symbol(self, ev): | |
77 # XXX: Ugly workaround to a pyglet bug on X11 | |
78 #TODO: fix that bug in pyglet | |
79 try: | |
80 return pyglet.window.Window._event_text_symbol(self, ev) | |
81 except Exception as exc: | |
82 print('*WARNING* Pyglet error:') | |
83 traceback.print_exc(exc) | |
84 return None, None | |
85 | |
86 | |
87 def on_key_press(self, symbol, modifiers): | |
88 if symbol == pyglet.window.key.ESCAPE: | |
89 self.has_exit = True | |
90 # XXX: Fullscreen will be enabled the day pyglet stops sucking | |
91 elif symbol == pyglet.window.key.F11: | |
92 self.set_fullscreen(not self.fullscreen) | |
93 | |
94 | |
95 def update(self): | |
96 if self.background: | |
97 self.background.update(self.game.frame) | |
98 if self.game: | |
99 if not self.replay_level: | |
100 #TODO: allow user settings | |
101 keystate = 0 | |
102 if self.keys[pyglet.window.key.W]: | |
103 keystate |= 1 | |
104 if self.keys[pyglet.window.key.X]: | |
105 keystate |= 2 | |
106 #TODO: on some configurations, LSHIFT is Shift_L when pressed | |
107 # and ISO_Prev_Group when released, confusing the hell out of pyglet | |
108 # and leading to a always-on LSHIFT... | |
109 if self.keys[pyglet.window.key.LSHIFT]: | |
110 keystate |= 4 | |
111 if self.keys[pyglet.window.key.UP]: | |
112 keystate |= 16 | |
113 if self.keys[pyglet.window.key.DOWN]: | |
114 keystate |= 32 | |
115 if self.keys[pyglet.window.key.LEFT]: | |
116 keystate |= 64 | |
117 if self.keys[pyglet.window.key.RIGHT]: | |
118 keystate |= 128 | |
119 if self.keys[pyglet.window.key.LCTRL]: | |
120 keystate |= 256 | |
121 self.game.run_iter(keystate) | |
122 else: | |
123 keystate = 0 | |
124 for frame, _keystate, unknown in self.replay_level.keys: | |
125 if self.game.frame < frame: | |
126 break | |
127 else: | |
128 keystate = _keystate | |
129 | |
130 self.game.run_iter(keystate) | |
131 | |
132 | |
133 def on_draw(self): | |
134 GameRenderer.render(self) | |
135 | |
136 #TODO | |
137 glMatrixMode(GL_MODELVIEW) | |
138 glLoadIdentity() | |
139 gluLookAt(192., 224., 835.979370, | |
140 192, 224., 0., 0., 1., 0.) | |
141 self.fps_display.draw() | |
142 |