comparison pytouhou/ui/anmrenderer.py @ 239:901489c21d19

Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
author Thibaut Girka <thib@sitedethib.com>
date Sun, 01 Jan 2012 21:12:50 +0100
parents 0e1762b1ab9f
children dd2bd7283bec
comparison
equal deleted inserted replaced
238:0e1762b1ab9f 239:901489c21d19
20 glHint, glEnableClientState, glViewport, 20 glHint, glEnableClientState, glViewport,
21 gluPerspective, GL_PROJECTION, 21 gluPerspective, GL_PROJECTION,
22 GL_TEXTURE_2D, GL_BLEND, 22 GL_TEXTURE_2D, GL_BLEND,
23 GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST, 23 GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST,
24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, 24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
25 glClear, GL_COLOR_BUFFER_BIT) 25 glClearColor, glClear, GL_COLOR_BUFFER_BIT)
26 26
27 from pytouhou.game.sprite import Sprite 27 from pytouhou.game.sprite import Sprite
28 from pytouhou.vm.anmrunner import ANMRunner 28 from pytouhou.vm.anmrunner import ANMRunner
29 29
30 from pytouhou.utils.helpers import get_logger 30 from pytouhou.utils.helpers import get_logger
44 caption='PyTouhou', resizable=False) 44 caption='PyTouhou', resizable=False)
45 45
46 self._anm_wrapper = anm_wrapper 46 self._anm_wrapper = anm_wrapper
47 self.anm = anm_wrapper.anm_files[0] 47 self.anm = anm_wrapper.anm_files[0]
48 self.sprites = sprites 48 self.sprites = sprites
49 self.clear_color = (0., 0., 0., 1.)
49 if sprites: 50 if sprites:
50 self.items = self.anm.sprites 51 self.items = self.anm.sprites
51 else: 52 else:
52 self.items = self.anm.scripts 53 self.items = self.anm.scripts
53 self.load(index) 54 self.load(index)
100 101
101 102
102 def on_key_press(self, symbol, modifiers): 103 def on_key_press(self, symbol, modifiers):
103 if symbol == pyglet.window.key.ESCAPE: 104 if symbol == pyglet.window.key.ESCAPE:
104 self.has_exit = True 105 self.has_exit = True
105 # XXX: Fullscreen will be enabled the day pyglet stops sucking
106 elif symbol == pyglet.window.key.F11:
107 self.set_fullscreen(not self.fullscreen)
108 elif symbol == pyglet.window.key.W: 106 elif symbol == pyglet.window.key.W:
109 self.load() 107 self.load()
110 elif symbol == pyglet.window.key.X: 108 elif symbol == pyglet.window.key.X:
111 self.x, self.y = (192, 224) if self.x == 0 else (0, 0) 109 self.x, self.y = (192, 224) if self.x == 0 else (0, 0)
112 elif symbol == pyglet.window.key.LEFT: 110 elif symbol == pyglet.window.key.LEFT:
113 self.change(-1) 111 self.change(-1)
114 elif symbol == pyglet.window.key.RIGHT: 112 elif symbol == pyglet.window.key.RIGHT:
115 self.change(+1) 113 self.change(+1)
116 elif symbol == pyglet.window.key.TAB: 114 elif symbol == pyglet.window.key.TAB:
117 self.toggle_sprites() 115 self.toggle_sprites()
116 elif symbol == pyglet.window.key.SPACE:
117 self.toggle_clear_color()
118 elif symbol >= pyglet.window.key.F1 and symbol <= pyglet.window.key.F12: 118 elif symbol >= pyglet.window.key.F1 and symbol <= pyglet.window.key.F12:
119 interrupt = symbol - pyglet.window.key.F1 + 1 119 interrupt = symbol - pyglet.window.key.F1 + 1
120 if modifiers == pyglet.window.key.MOD_CTRL: 120 if modifiers & pyglet.window.key.MOD_SHIFT:
121 interrupt += 12 121 interrupt += 12
122 self._anmrunner.interrupt(interrupt) 122 if not self.sprites:
123 self._anmrunner.interrupt(interrupt)
123 124
124 125
125 def load(self, index=None): 126 def load(self, index=None):
126 if index is None: 127 if index is None:
127 index = self.num 128 index = self.num
128 self._sprite = Sprite() 129 self._sprite = Sprite()
129 print index
130 if self.sprites: 130 if self.sprites:
131 self._sprite.anm, self._sprite.texcoords = self._anm_wrapper.get_sprite(index) 131 self._sprite.anm, self._sprite.texcoords = self._anm_wrapper.get_sprite(index)
132 print('Loaded sprite %d' % index)
132 else: 133 else:
133 self._anmrunner = ANMRunner(self._anm_wrapper, index, self._sprite) 134 self._anmrunner = ANMRunner(self._anm_wrapper, index, self._sprite)
134 self._anmrunner.run_frame() 135 print('Loading anim %d, handled events: %r' % (index, self._anmrunner.script.interrupts.keys()))
135 self.num = index 136 self.num = index
136 137
137 138
138 def change(self, diff): 139 def change(self, diff):
139 keys = self.items.keys() 140 keys = self.items.keys()
140 keys.sort() 141 keys.sort()
141 index = keys.index(self.num) + diff 142 index = (keys.index(self.num) + diff) % len(keys)
142 if index < 0 or index >= len(keys):
143 return
144 item = keys[index] 143 item = keys[index]
145 self.load(item) 144 self.load(item)
146 145
147 146
148 def toggle_sprites(self): 147 def toggle_sprites(self):
149 self.sprites = not(self.sprites) 148 self.sprites = not(self.sprites)
150 if self.sprites: 149 if self.sprites:
151 self.items = self.anm.sprites 150 self.items = self.anm.sprites
152 else: 151 else:
153 self.items = self.anm.scripts 152 self.items = self.anm.scripts
154 self.load() 153 self.load(0)
154
155
156 def toggle_clear_color(self):
157 if self.clear_color[0] == 0.:
158 self.clear_color = (1., 1., 1., 1.)
159 else:
160 self.clear_color = (0., 0., 0., 1.)
155 161
156 162
157 def update(self): 163 def update(self):
158 if not self.sprites: 164 if not self.sprites:
159 self._anmrunner.run_frame() 165 self._anmrunner.run_frame()
160 166
167 glClearColor(*self.clear_color)
161 glClear(GL_COLOR_BUFFER_BIT) 168 glClear(GL_COLOR_BUFFER_BIT)
162 self.render_elements([self]) 169 if not self._sprite._removed:
170 self.render_elements([self])
163 171