comparison pytouhou/ui/gamerenderer.pyx @ 459:6e733ed817bd

Move every rendering function from gamerunner to gamerenderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 06 Sep 2013 22:35:54 +0200
parents 1b56d62250ab
children a71b912b45b7
comparison
equal deleted inserted replaced
458:1b56d62250ab 459:6e733ed817bd
14 14
15 from itertools import chain 15 from itertools import chain
16 16
17 from pytouhou.lib.opengl cimport \ 17 from pytouhou.lib.opengl cimport \
18 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable, 18 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable,
19 glEnable, glFogi, glFogf, glFogfv, 19 glEnable, glFogi, glFogf, glFogfv, GL_PROJECTION, GL_MODELVIEW,
20 GL_PROJECTION, GL_MODELVIEW, GL_FOG, GL_FOG_MODE, GL_LINEAR, 20 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END,
21 GL_FOG_START, GL_FOG_END, GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat) 21 GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat, glViewport, glScissor,
22 22 GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT)
23 from pytouhou.utils.maths cimport setup_camera 23
24 from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d
25 from .shaders.eosd import GameShader, BackgroundShader
26
27 from collections import namedtuple
28 Rect = namedtuple('Rect', 'x y w h')
29 Color = namedtuple('Color', 'r g b a')
24 30
25 31
26 cdef class GameRenderer(Renderer): 32 cdef class GameRenderer(Renderer):
27 def __init__(self, resource_loader): 33 def __init__(self, resource_loader, use_fixed_pipeline):
34 self.use_fixed_pipeline = use_fixed_pipeline #XXX
35
28 Renderer.__init__(self, resource_loader) 36 Renderer.__init__(self, resource_loader)
29 37
30 38 if not self.use_fixed_pipeline:
31 def render(self): 39 self.game_shader = GameShader()
40 self.background_shader = BackgroundShader()
41 self.interface_shader = self.game_shader
42
43
44 cdef void load_background(self, background):
45 if background is not None:
46 self.background_renderer = BackgroundRenderer(self.use_fixed_pipeline)
47 self.background_renderer.load(background)
48 else:
49 self.background_renderer = None
50
51
52 cdef void start(self, game):
53 self.proj = perspective(30, float(game.width) / float(game.height),
54 101010101./2010101., 101010101./10101.)
55 game_view = setup_camera(0, 0, 1)
56 self.game_mvp = game_view * self.proj
57 self.interface_mvp = ortho_2d(0., float(game.interface.width), float(game.interface.height), 0.)
58
59
60 cdef void render(self, game):
61 self.render_game(game)
62 self.render_text(game.texts + game.native_texts)
63 self.render_interface(game.interface, game.boss)
64
65
66 cdef void render_game(self, game):
67 cdef long game_x, game_y
32 cdef float x, y, z, dx, dy, dz, fog_data[4], fog_start, fog_end 68 cdef float x, y, z, dx, dy, dz, fog_data[4], fog_start, fog_end
33 cdef unsigned char fog_r, fog_g, fog_b 69 cdef unsigned char fog_r, fog_g, fog_b
34 cdef Matrix mvp 70 cdef Matrix mvp
35 71
36 back = self.background 72 game_x, game_y = game.interface.game_pos
37 game = self.game 73 glViewport(game_x, game_y, game.width, game.height)
74 glClear(GL_DEPTH_BUFFER_BIT)
75 glScissor(game_x, game_y, game.width, game.height)
76 glEnable(GL_SCISSOR_TEST)
38 77
39 if self.use_fixed_pipeline: 78 if self.use_fixed_pipeline:
40 glMatrixMode(GL_PROJECTION) 79 glMatrixMode(GL_PROJECTION)
41 glLoadIdentity() 80 glLoadIdentity()
42 81
48 else: 87 else:
49 self.game_shader.bind() 88 self.game_shader.bind()
50 self.game_shader.uniform_matrix('mvp', self.game_mvp) 89 self.game_shader.uniform_matrix('mvp', self.game_mvp)
51 90
52 self.render_elements([game.spellcard_effect]) 91 self.render_elements([game.spellcard_effect])
53 elif back is not None: 92 elif self.background_renderer is not None:
93 back = self.background_renderer.background
54 x, y, z = back.position_interpolator.values 94 x, y, z = back.position_interpolator.values
55 dx, dy, dz = back.position2_interpolator.values 95 dx, dy, dz = back.position2_interpolator.values
56 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values 96 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values
57 97
58 # Those two lines may come from the difference between Direct3D and 98 # Those two lines may come from the difference between Direct3D and
104 glDisable(GL_FOG) 144 glDisable(GL_FOG)
105 else: 145 else:
106 self.game_shader.bind() 146 self.game_shader.bind()
107 self.game_shader.uniform_matrix('mvp', self.game_mvp) 147 self.game_shader.uniform_matrix('mvp', self.game_mvp)
108 148
109 self.render_elements(enemy for enemy in game.enemies if enemy.visible) 149 self.render_elements([enemy for enemy in game.enemies if enemy.visible])
110 self.render_elements(game.effects) 150 self.render_elements(game.effects)
111 self.render_elements(chain(game.players_bullets, 151 self.render_elements(chain(game.players_bullets,
112 game.lasers_sprites(), 152 game.lasers_sprites(),
113 game.players, 153 game.players,
114 game.msg_sprites())) 154 game.msg_sprites()))
115 self.render_elements(chain(game.bullets, game.lasers, 155 self.render_elements(chain(game.bullets, game.lasers,
116 game.cancelled_bullets, game.items, 156 game.cancelled_bullets, game.items,
117 game.labels)) 157 game.labels))
158
159 if game.msg_runner:
160 rect = Rect(48, 368, 288, 48)
161 color1 = Color(0, 0, 0, 192)
162 color2 = Color(0, 0, 0, 128)
163 self.render_quads([rect], [(color1, color1, color2, color2)], 0)
164
165 glDisable(GL_SCISSOR_TEST)
166
167
168 cdef void render_text(self, texts):
169 if self.font_manager is None:
170 return
171
172 labels = [label for label in texts if label is not None]
173 self.font_manager.load(labels)
174
175 black = Color(0, 0, 0, 255)
176
177 for label in labels:
178 if label is None:
179 continue
180
181 rect = Rect(label.x, label.y, label.width, label.height)
182 gradient = [Color(*color, a=label.alpha) for color in label.gradient]
183
184 if label.shadow:
185 shadow_rect = Rect(label.x + 1, label.y + 1, label.width, label.height)
186 shadow = [black._replace(a=label.alpha)] * 4
187 self.render_quads([shadow_rect, rect], [shadow, gradient], label.texture)
188 else:
189 self.render_quads([rect], [gradient], label.texture)
190
191
192 cdef void render_interface(self, interface, game_boss):
193 elements = []
194
195 if self.use_fixed_pipeline:
196 glMatrixMode(GL_MODELVIEW)
197 glLoadMatrixf(self.interface_mvp.data)
198 glDisable(GL_FOG)
199 else:
200 self.interface_shader.bind()
201 self.interface_shader.uniform_matrix('mvp', self.interface_mvp)
202 glViewport(0, 0, interface.width, interface.height)
203
204 items = [item for item in interface.items if item.anmrunner and item.anmrunner.running]
205 labels = interface.labels.values()
206
207 if items:
208 # Redraw all the interface
209 elements.extend(items)
210 else:
211 # Redraw only changed labels
212 labels = [label for label in labels if label.changed]
213
214 elements.extend(interface.level_start)
215
216 if game_boss is not None:
217 elements.extend(interface.boss_items)
218
219 elements.extend(labels)
220 self.render_elements(elements)
221 for label in labels:
222 label.changed = False