Mercurial > touhou
comparison pytouhou/ui/anmrenderer.py @ 237:cbe9dbd80dfb
Add an anmviewer script.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 01 Jan 2012 19:51:34 +0100 |
parents | |
children | 0e1762b1ab9f |
comparison
equal
deleted
inserted
replaced
236:741860192b56 | 237:cbe9dbd80dfb |
---|---|
1 #!/usr/bin/env python | |
2 # -*- encoding: utf-8 -*- | |
3 ## | |
4 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
5 ## | |
6 ## This program is free software; you can redistribute it and/or modify | |
7 ## it under the terms of the GNU General Public License as published | |
8 ## by the Free Software Foundation; version 3 only. | |
9 ## | |
10 ## This program is distributed in the hope that it will be useful, | |
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 ## GNU General Public License for more details. | |
14 ## | |
15 | |
16 import pyglet | |
17 import traceback | |
18 | |
19 from pyglet.gl import (glMatrixMode, glLoadIdentity, glEnable, | |
20 glHint, glEnableClientState, glViewport, | |
21 gluPerspective, GL_PROJECTION, | |
22 GL_TEXTURE_2D, GL_BLEND, | |
23 GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST, | |
24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY, | |
25 glClear, GL_COLOR_BUFFER_BIT) | |
26 | |
27 from pytouhou.game.sprite import Sprite | |
28 from pytouhou.vm.anmrunner import ANMRunner | |
29 | |
30 from pytouhou.utils.helpers import get_logger | |
31 | |
32 from .renderer import Renderer | |
33 | |
34 | |
35 logger = get_logger(__name__) | |
36 | |
37 | |
38 class ANMRenderer(pyglet.window.Window, Renderer): | |
39 def __init__(self, resource_loader, anm_wrapper, index=0, sprites=False): | |
40 Renderer.__init__(self, resource_loader) | |
41 | |
42 width, height = 384, 448 | |
43 pyglet.window.Window.__init__(self, width=width, height=height, | |
44 caption='PyTouhou', resizable=False) | |
45 | |
46 self._anm_wrapper = anm_wrapper | |
47 self.anm = anm_wrapper.anm_files[0] | |
48 self.sprites = sprites | |
49 if sprites: | |
50 self.items = self.anm.sprites | |
51 else: | |
52 self.items = self.anm.scripts | |
53 self.load(index) | |
54 | |
55 self.x = width / 2 | |
56 self.y = height / 2 | |
57 | |
58 | |
59 def start(self, width=384, height=448): | |
60 if (width, height) != (self.width, self.height): | |
61 self.set_size(width, height) | |
62 | |
63 # Initialize OpenGL | |
64 glEnable(GL_BLEND) | |
65 glEnable(GL_TEXTURE_2D) | |
66 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) | |
67 glEnableClientState(GL_COLOR_ARRAY) | |
68 glEnableClientState(GL_VERTEX_ARRAY) | |
69 glEnableClientState(GL_TEXTURE_COORD_ARRAY) | |
70 | |
71 # Switch to game projection | |
72 glMatrixMode(GL_PROJECTION) | |
73 glLoadIdentity() | |
74 gluPerspective(30, float(self.width) / float(self.height), | |
75 101010101./2010101., 101010101./10101.) | |
76 | |
77 self.setup_camera(0, 0, 1) | |
78 | |
79 # Use our own loop to ensure 60 fps | |
80 pyglet.clock.set_fps_limit(60) | |
81 while not self.has_exit: | |
82 pyglet.clock.tick() | |
83 self.dispatch_events() | |
84 self.update() | |
85 self.flip() | |
86 | |
87 | |
88 def on_resize(self, width, height): | |
89 glViewport(0, 0, width, height) | |
90 | |
91 | |
92 def _event_text_symbol(self, ev): | |
93 # XXX: Ugly workaround to a pyglet bug on X11 | |
94 #TODO: fix that bug in pyglet | |
95 try: | |
96 return pyglet.window.Window._event_text_symbol(self, ev) | |
97 except Exception as exc: | |
98 logger.warn('Pyglet error: %s', traceback.format_exc(exc)) | |
99 return None, None | |
100 | |
101 | |
102 def on_key_press(self, symbol, modifiers): | |
103 if symbol == pyglet.window.key.ESCAPE: | |
104 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: | |
109 self.load() | |
110 elif symbol == pyglet.window.key.X: | |
111 self.x, self.y = (192, 224) if self.x == 0 else (0, 0) | |
112 elif symbol == pyglet.window.key.LEFT: | |
113 self.change(-1) | |
114 elif symbol == pyglet.window.key.RIGHT: | |
115 self.change(+1) | |
116 elif symbol == pyglet.window.key.TAB: | |
117 self.toggle_sprites() | |
118 elif symbol >= pyglet.window.key.F1 and symbol <= pyglet.window.key.F12: | |
119 print (symbol - pyglet.window.key.F1 + (12 if modifiers == pyglet.window.key.MOD_CTRL else 0) + 1) | |
120 #self._anmrunner.interrupt(symbol - pyglet.window.key.F1 + (12 if modifiers == pyglet.window.key.MOD_CTRL) + 1) | |
121 | |
122 | |
123 def load(self, index=None): | |
124 if index is None: | |
125 index = self.num | |
126 self._sprite = Sprite() | |
127 print index | |
128 if self.sprites: | |
129 self._sprite.anm, self._sprite.texcoords = self._anm_wrapper.get_sprite(index) | |
130 else: | |
131 self._anmrunner = ANMRunner(self._anm_wrapper, index, self._sprite) | |
132 self._anmrunner.run_frame() | |
133 self.num = index | |
134 | |
135 | |
136 def change(self, diff): | |
137 keys = self.items.keys() | |
138 keys.sort() | |
139 index = keys.index(self.num) + diff | |
140 if index < 0 or index >= len(keys): | |
141 return | |
142 item = keys[index] | |
143 self.load(item) | |
144 | |
145 | |
146 def toggle_sprites(self): | |
147 self.sprites = not(self.sprites) | |
148 if self.sprites: | |
149 self.items = self.anm.sprites | |
150 else: | |
151 self.items = self.anm.scripts | |
152 self.load() | |
153 | |
154 | |
155 def update(self): | |
156 if not self.sprites: | |
157 self._anmrunner.run_frame() | |
158 | |
159 glClear(GL_COLOR_BUFFER_BIT) | |
160 self.render_elements([self]) | |
161 |