Mercurial > touhou
comparison pytouhou/ui/anmrenderer.pyx @ 425:1104dc2553ee
Make the anmviewer use the new architecture.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 16 Jul 2013 21:07:15 +0200 |
parents | pytouhou/ui/anmrenderer.py@b0b8825296d0 |
children | 5d7bb2fd74f7 |
comparison
equal
deleted
inserted
replaced
424:f4d76d3d6f2a | 425:1104dc2553ee |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
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 from pytouhou.lib.opengl cimport \ | |
16 (glClearColor, glClear, GL_COLOR_BUFFER_BIT) | |
17 | |
18 from pytouhou.game.sprite import Sprite | |
19 from pytouhou.vm.anmrunner import ANMRunner | |
20 | |
21 from pytouhou.utils.helpers import get_logger | |
22 from pytouhou.utils.maths cimport perspective, setup_camera | |
23 | |
24 from .renderer import Renderer | |
25 from .shaders.eosd import GameShader | |
26 | |
27 from pytouhou.lib import sdl | |
28 | |
29 | |
30 logger = get_logger(__name__) | |
31 | |
32 | |
33 class ANMRenderer(Renderer): | |
34 def __init__(self, window, resource_loader, anm_wrapper, index=0, sprites=False): | |
35 self.use_fixed_pipeline = window.use_fixed_pipeline #XXX | |
36 | |
37 Renderer.__init__(self, resource_loader) | |
38 | |
39 self.window = window | |
40 self.texture_manager.preload(resource_loader.instanced_anms.values()) | |
41 | |
42 self._anm_wrapper = anm_wrapper | |
43 self.sprites = sprites | |
44 self.clear_color = (0., 0., 0., 1.) | |
45 self.force_allow_dest_offset = False | |
46 self.index_items() | |
47 self.load(index) | |
48 self.objects = [self] | |
49 | |
50 self.width = 384 | |
51 self.height = 448 | |
52 | |
53 self.x = self.width / 2 | |
54 self.y = self.height / 2 | |
55 | |
56 | |
57 def start(self, width=384, height=448): | |
58 self.window.set_size(width, height) | |
59 | |
60 # Switch to game projection | |
61 proj = perspective(30, float(width) / float(height), | |
62 101010101./2010101., 101010101./10101.) | |
63 view = setup_camera(0, 0, 1) | |
64 | |
65 shader = GameShader() | |
66 | |
67 mvp = view * proj | |
68 shader.bind() | |
69 shader.uniform_matrix('mvp', mvp) | |
70 | |
71 | |
72 def load(self, index=None): | |
73 if index is None: | |
74 index = self.num | |
75 self.sprite = Sprite() | |
76 if self.sprites: | |
77 self.sprite.anm, self.sprite.texcoords = self._anm_wrapper.get_sprite(index) | |
78 print('Loaded sprite %d' % index) | |
79 else: | |
80 self.anmrunner = ANMRunner(self._anm_wrapper, index, self.sprite) | |
81 print('Loading anim %d, handled events: %r' % (index, self.anmrunner.script.interrupts.keys())) | |
82 self.num = index | |
83 | |
84 | |
85 def change(self, diff): | |
86 keys = self.items.keys() | |
87 keys.sort() | |
88 index = (keys.index(self.num) + diff) % len(keys) | |
89 item = keys[index] | |
90 self.load(item) | |
91 | |
92 | |
93 def index_items(self): | |
94 self.items = {} | |
95 if self.sprites: | |
96 self.items = self._anm_wrapper.sprites | |
97 else: | |
98 self.items = self._anm_wrapper.scripts | |
99 | |
100 | |
101 def toggle_sprites(self): | |
102 self.sprites = not(self.sprites) | |
103 self.index_items() | |
104 self.load(0) | |
105 | |
106 | |
107 def toggle_clear_color(self): | |
108 if self.clear_color[0] == 0.: | |
109 self.clear_color = (1., 1., 1., 1.) | |
110 else: | |
111 self.clear_color = (0., 0., 0., 1.) | |
112 | |
113 | |
114 def update(self): | |
115 sdl.SCANCODE_C = 6 | |
116 sdl.SCANCODE_TAB = 43 | |
117 sdl.SCANCODE_SPACE = 44 | |
118 sdl.SCANCODE_F1 = 58 | |
119 sdl.SCANCODE_F12 = 69 | |
120 for event in sdl.poll_events(): | |
121 type_ = event[0] | |
122 if type_ == sdl.KEYDOWN: | |
123 scancode = event[1] | |
124 if scancode == sdl.SCANCODE_Z: | |
125 self.load() | |
126 elif scancode == sdl.SCANCODE_X: | |
127 self.x, self.y = {(192, 224): (0, 0), | |
128 (0, 0): (-224, 0), | |
129 (-224, 0): (192, 224)}[(self.x, self.y)] | |
130 elif scancode == sdl.SCANCODE_C: | |
131 self.force_allow_dest_offset = not self.force_allow_dest_offset | |
132 self.load() | |
133 elif scancode == sdl.SCANCODE_LEFT: | |
134 self.change(-1) | |
135 elif scancode == sdl.SCANCODE_RIGHT: | |
136 self.change(+1) | |
137 elif scancode == sdl.SCANCODE_TAB: | |
138 self.toggle_sprites() | |
139 elif scancode == sdl.SCANCODE_SPACE: | |
140 self.toggle_clear_color() | |
141 elif sdl.SCANCODE_F1 <= scancode <= sdl.SCANCODE_F12: | |
142 interrupt = scancode - sdl.SCANCODE_F1 + 1 | |
143 keys = sdl.get_keyboard_state() | |
144 if keys[sdl.SCANCODE_LSHIFT]: | |
145 interrupt += 12 | |
146 if not self.sprites: | |
147 self.anmrunner.interrupt(interrupt) | |
148 elif scancode == sdl.SCANCODE_ESCAPE: | |
149 return False | |
150 elif type_ == sdl.QUIT: | |
151 return False | |
152 elif type_ == sdl.WINDOWEVENT: | |
153 event_ = event[1] | |
154 if event_ == sdl.WINDOWEVENT_RESIZED: | |
155 self.window.set_size(event[2], event[3]) | |
156 | |
157 if not self.sprites: | |
158 self.anmrunner.run_frame() | |
159 | |
160 if self.force_allow_dest_offset: | |
161 self.sprite.allow_dest_offset = True | |
162 | |
163 glClearColor(self.clear_color[0], self.clear_color[1], self.clear_color[2], self.clear_color[3]) | |
164 glClear(GL_COLOR_BUFFER_BIT) | |
165 if not self.sprite.removed: | |
166 self.render_elements([self]) | |
167 return True | |
168 | |
169 | |
170 def finish(self): | |
171 pass |