comparison pytouhou/ui/gamerenderer.py @ 398:8d252cdb495f

Move the background rendering code to pytouhou.ui.renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 12 Feb 2013 18:20:49 +0100
parents 346614f788f1
children 1c773544eaeb
comparison
equal deleted inserted replaced
397:c5ba11ede097 398:8d252cdb495f
15 15
16 from itertools import chain 16 from itertools import chain
17 17
18 from pyglet.gl import (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, 18 from pyglet.gl import (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf,
19 glDisable, glEnable, glFogi, glFogf, glFogfv, 19 glDisable, glEnable, glFogi, glFogf, glFogfv,
20 glBlendFunc, glBindTexture, glVertexPointer,
21 glTexCoordPointer, glColorPointer, glDrawArrays,
22 glVertexAttribPointer, glEnableVertexAttribArray,
23 GL_DEPTH_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW, 20 GL_DEPTH_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW,
24 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, 21 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START,
25 GL_FOG_END, GL_FOG_COLOR, GL_DEPTH_TEST, GL_SRC_ALPHA, 22 GL_FOG_END, GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat)
26 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D,
27 GL_UNSIGNED_BYTE, GL_FLOAT, GL_QUADS,
28 GL_COLOR_BUFFER_BIT, GLfloat)
29 23
30 from pytouhou.utils.matrix import Matrix 24 from pytouhou.utils.matrix import Matrix
31 25
32 from .renderer import Renderer 26 from .renderer import Renderer
33 from .background import get_background_rendering_data
34 27
35 28
36 29
37 class GameRenderer(Renderer): 30 class GameRenderer(Renderer):
38 __slots__ = ('game', 'background') 31 __slots__ = ('game', 'background')
55 def render(self): 48 def render(self):
56 glClear(GL_DEPTH_BUFFER_BIT) 49 glClear(GL_DEPTH_BUFFER_BIT)
57 50
58 back = self.background 51 back = self.background
59 game = self.game 52 game = self.game
60 texture_manager = self.texture_manager
61 53
62 if self.use_fixed_pipeline: 54 if self.use_fixed_pipeline:
63 glMatrixMode(GL_PROJECTION) 55 glMatrixMode(GL_PROJECTION)
64 glLoadIdentity() 56 glLoadIdentity()
65 57
98 else: 90 else:
99 model_view = model * view 91 model_view = model * view
100 self.background_shader.uniform_matrixf('model_view', model_view.get_c_data()) 92 self.background_shader.uniform_matrixf('model_view', model_view.get_c_data())
101 self.background_shader.uniform_matrixf('projection', self.proj.get_c_data()) 93 self.background_shader.uniform_matrixf('projection', self.proj.get_c_data())
102 94
103 glEnable(GL_DEPTH_TEST) 95 self.render_background(back)
104 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back):
105 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
106 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
107 if self.use_fixed_pipeline:
108 glVertexPointer(3, GL_FLOAT, 0, vertices)
109 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
110 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
111 else:
112 glVertexAttribPointer(0, 3, GL_FLOAT, False, 0, vertices)
113 glEnableVertexAttribArray(0)
114 glVertexAttribPointer(1, 2, GL_FLOAT, False, 0, uvs)
115 glEnableVertexAttribArray(1)
116 glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, True, 0, colors)
117 glEnableVertexAttribArray(2)
118 glDrawArrays(GL_QUADS, 0, nb_vertices)
119 glDisable(GL_DEPTH_TEST)
120 else: 96 else:
121 glClear(GL_COLOR_BUFFER_BIT) 97 glClear(GL_COLOR_BUFFER_BIT)
122 98
123 if game is not None: 99 if game is not None:
124 if self.use_fixed_pipeline: 100 if self.use_fixed_pipeline:
136 game.players, 112 game.players,
137 game.msg_sprites())) 113 game.msg_sprites()))
138 self.render_elements(chain(game.bullets, game.lasers, 114 self.render_elements(chain(game.bullets, game.lasers,
139 game.cancelled_bullets, game.items, 115 game.cancelled_bullets, game.items,
140 game.labels)) 116 game.labels))
141