annotate pytouhou/ui/anmrenderer.py @ 412:5fe6cd6ceb48

Refactor the maths functions out of Renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 12 Jun 2013 18:30:08 +0200
parents 402e96a0baeb
children b0b8825296d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 #!/usr/bin/env python
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 # -*- encoding: utf-8 -*-
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ##
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ##
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## This program is free software; you can redistribute it and/or modify
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## it under the terms of the GNU General Public License as published
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ## by the Free Software Foundation; version 3 only.
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ##
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## This program is distributed in the hope that it will be useful,
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ## GNU General Public License for more details.
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 ##
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 import pyglet
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 import traceback
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
19 from pyglet.gl import (glMatrixMode, glEnable,
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 glHint, glEnableClientState, glViewport,
382
b9aecdc6f72f Fix the anmviewer, broken since changeset 370:74471afbac37.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 304
diff changeset
21 glLoadMatrixf, GL_PROJECTION, GL_MODELVIEW,
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 GL_TEXTURE_2D, GL_BLEND,
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST,
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 GL_COLOR_ARRAY, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY,
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
25 glClearColor, glClear, GL_COLOR_BUFFER_BIT,
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
26 glGenBuffers)
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 from pytouhou.game.sprite import Sprite
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 from pytouhou.vm.anmrunner import ANMRunner
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 from pytouhou.utils.helpers import get_logger
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 405
diff changeset
32 from pytouhou.utils.maths import perspective, setup_camera
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 from .renderer import Renderer
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
35 from .shaders.eosd import GameShader
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
36
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
37 from ctypes import c_uint
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 logger = get_logger(__name__)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 class ANMRenderer(pyglet.window.Window, Renderer):
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
44 def __init__(self, resource_loader, anm_wrapper, index=0, sprites=False,
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
45 fixed_pipeline=False):
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 Renderer.__init__(self, resource_loader)
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
47 self.texture_manager.preload(resource_loader.instanced_anms.values())
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 width, height = 384, 448
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 pyglet.window.Window.__init__(self, width=width, height=height,
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 caption='PyTouhou', resizable=False)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
53 self.use_fixed_pipeline = fixed_pipeline
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
54
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 self._anm_wrapper = anm_wrapper
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 self.sprites = sprites
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
57 self.clear_color = (0., 0., 0., 1.)
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
58 self.force_allow_dest_offset = False
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
59 self.index_items()
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 self.load(index)
384
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 382
diff changeset
61 self.objects = [self]
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 self.x = width / 2
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 self.y = height / 2
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 def start(self, width=384, height=448):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 if (width, height) != (self.width, self.height):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 self.set_size(width, height)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 # Initialize OpenGL
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 glEnable(GL_BLEND)
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
73 if self.use_fixed_pipeline:
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
74 glEnable(GL_TEXTURE_2D)
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
75 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
76 glEnableClientState(GL_COLOR_ARRAY)
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
77 glEnableClientState(GL_VERTEX_ARRAY)
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
78 glEnableClientState(GL_TEXTURE_COORD_ARRAY)
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 # Switch to game projection
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 405
diff changeset
81 proj = perspective(30, float(self.width) / float(self.height),
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 405
diff changeset
82 101010101./2010101., 101010101./10101.)
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 405
diff changeset
83 view = setup_camera(0, 0, 1)
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
84
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
85 if not self.use_fixed_pipeline:
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
86 shader = GameShader()
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
87
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
88 vbo_array = (c_uint * 1)()
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
89 glGenBuffers(1, vbo_array)
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
90 self.vbo, = vbo_array
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91
405
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
92 mvp = view * proj
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
93 shader.bind()
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
94 shader.uniform_matrixf('mvp', mvp.get_c_data())
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
95 else:
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
96 glMatrixMode(GL_PROJECTION)
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
97 glLoadMatrixf(proj.get_c_data())
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
98
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
99 glMatrixMode(GL_MODELVIEW)
402e96a0baeb Make the anmviewer use the programmable pipeline too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 384
diff changeset
100 glLoadMatrixf(view.get_c_data())
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 # Use our own loop to ensure 60 fps
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 pyglet.clock.set_fps_limit(60)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 while not self.has_exit:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 pyglet.clock.tick()
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 self.dispatch_events()
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 self.update()
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 self.flip()
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 def on_resize(self, width, height):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 glViewport(0, 0, width, height)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 def _event_text_symbol(self, ev):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 # XXX: Ugly workaround to a pyglet bug on X11
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 #TODO: fix that bug in pyglet
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 try:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 return pyglet.window.Window._event_text_symbol(self, ev)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 except Exception as exc:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 logger.warn('Pyglet error: %s', traceback.format_exc(exc))
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 return None, None
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 def on_key_press(self, symbol, modifiers):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 if symbol == pyglet.window.key.ESCAPE:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 self.has_exit = True
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 elif symbol == pyglet.window.key.W:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 self.load()
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 elif symbol == pyglet.window.key.X:
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
131 self.x, self.y = {(192, 224): (0, 0),
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
132 (0, 0): (-224, 0),
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
133 (-224, 0): (192, 224)}[(self.x, self.y)]
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
134 elif symbol == pyglet.window.key.C:
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
135 self.force_allow_dest_offset = not self.force_allow_dest_offset
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
136 self.load()
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 elif symbol == pyglet.window.key.LEFT:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 self.change(-1)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 elif symbol == pyglet.window.key.RIGHT:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 self.change(+1)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 elif symbol == pyglet.window.key.TAB:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 self.toggle_sprites()
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
143 elif symbol == pyglet.window.key.SPACE:
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
144 self.toggle_clear_color()
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 elif symbol >= pyglet.window.key.F1 and symbol <= pyglet.window.key.F12:
238
0e1762b1ab9f Fix ANM interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 237
diff changeset
146 interrupt = symbol - pyglet.window.key.F1 + 1
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
147 if modifiers & pyglet.window.key.MOD_SHIFT:
238
0e1762b1ab9f Fix ANM interrupts
Thibaut Girka <thib@sitedethib.com>
parents: 237
diff changeset
148 interrupt += 12
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
149 if not self.sprites:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
150 self.anmrunner.interrupt(interrupt)
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 def load(self, index=None):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 if index is None:
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 index = self.num
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
156 self.sprite = Sprite()
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 if self.sprites:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
158 self.sprite.anm, self.sprite.texcoords = self._anm_wrapper.get_sprite(index)
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
159 print('Loaded sprite %d' % index)
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160 else:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
161 self.anmrunner = ANMRunner(self._anm_wrapper, index, self.sprite)
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
162 print('Loading anim %d, handled events: %r' % (index, self.anmrunner.script.interrupts.keys()))
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 self.num = index
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 def change(self, diff):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 keys = self.items.keys()
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 keys.sort()
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
169 index = (keys.index(self.num) + diff) % len(keys)
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170 item = keys[index]
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 self.load(item)
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
174 def index_items(self):
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
175 self.items = {}
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
176 if self.sprites:
288
d95beee0087a Make anmviewer work with newest anmwrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 241
diff changeset
177 self.items = self._anm_wrapper.sprites
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
178 else:
288
d95beee0087a Make anmviewer work with newest anmwrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 241
diff changeset
179 self.items = self._anm_wrapper.scripts
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
180
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
181
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 def toggle_sprites(self):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 self.sprites = not(self.sprites)
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
184 self.index_items()
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
185 self.load(0)
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
186
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
187
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
188 def toggle_clear_color(self):
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
189 if self.clear_color[0] == 0.:
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
190 self.clear_color = (1., 1., 1., 1.)
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
191 else:
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
192 self.clear_color = (0., 0., 0., 1.)
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 def update(self):
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 if not self.sprites:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
197 self.anmrunner.run_frame()
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
199 if self.force_allow_dest_offset:
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
200 self.sprite.allow_dest_offset = True
241
dd2bd7283bec Add support for multiple-file anms to anmviewer, add feature to force acceptance of translations
Thibaut Girka <thib@sitedethib.com>
parents: 239
diff changeset
201
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
202 glClearColor(*self.clear_color)
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 glClear(GL_COLOR_BUFFER_BIT)
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 288
diff changeset
204 if not self.sprite.removed:
239
901489c21d19 Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
Thibaut Girka <thib@sitedethib.com>
parents: 238
diff changeset
205 self.render_elements([self])
237
cbe9dbd80dfb Add an anmviewer script.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206