comparison pytouhou/ui/gamerenderer.py @ 391:84b151962708

Convert pytouhou.ui.gamerenderer back to pure python, it doesn’t use or need any cython feature.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 07 Jan 2013 21:13:32 +0100
parents pytouhou/ui/gamerenderer.pyx@690b5faaa0e6
children 9e2cbb2c2c64
comparison
equal deleted inserted replaced
390:b11953cf1d3b 391:84b151962708
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
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
16 from itertools import chain
17
18 from pyglet.gl import (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf,
19 glDisable, glEnable, glFogi, glFogf, glFogfv,
20 glBlendFunc, glBindTexture, glVertexPointer,
21 glTexCoordPointer, glColorPointer, glDrawArrays,
22 GL_DEPTH_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW,
23 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START,
24 GL_FOG_END, GL_FOG_COLOR, GL_DEPTH_TEST, GL_SRC_ALPHA,
25 GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_TEXTURE_2D,
26 GL_UNSIGNED_BYTE, GL_FLOAT, GL_QUADS,
27 GL_COLOR_BUFFER_BIT, GLfloat)
28
29 from pytouhou.utils.matrix import Matrix
30
31 from .renderer import Renderer
32 from .background import get_background_rendering_data
33
34
35
36 class GameRenderer(Renderer):
37 __slots__ = ('game', 'background')
38
39 def __init__(self, resource_loader, game=None, background=None):
40 Renderer.__init__(self, resource_loader)
41 if game:
42 self.load_game(game, background)
43
44
45 def load_game(self, game=None, background=None):
46 self.game = game
47 self.background = background
48
49 if game:
50 # Preload textures
51 self.texture_manager.preload(game.resource_loader.instanced_anms.values())
52
53
54 def render(self):
55 glClear(GL_DEPTH_BUFFER_BIT)
56
57 back = self.background
58 game = self.game
59 texture_manager = self.texture_manager
60
61 if self.use_fixed_pipeline:
62 glMatrixMode(GL_PROJECTION)
63 glLoadIdentity()
64
65 if game is not None and game.spellcard_effect is not None:
66 if self.use_fixed_pipeline:
67 glMatrixMode(GL_MODELVIEW)
68 glLoadMatrixf(self.game_mvp.get_c_data())
69 glDisable(GL_FOG)
70 else:
71 self.game_shader.bind()
72 self.game_shader.uniform_matrixf('mvp', self.game_mvp.get_c_data())
73
74 self.render_elements([game.spellcard_effect])
75 elif back is not None:
76 if self.use_fixed_pipeline:
77 glEnable(GL_FOG)
78 else:
79 self.background_shader.bind()
80 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values
81 x, y, z = back.position_interpolator.values
82 dx, dy, dz = back.position2_interpolator.values
83
84 glFogi(GL_FOG_MODE, GL_LINEAR)
85 glFogf(GL_FOG_START, fog_start)
86 glFogf(GL_FOG_END, fog_end)
87 glFogfv(GL_FOG_COLOR, (GLfloat * 4)(fog_r / 255., fog_g / 255., fog_b / 255., 1.))
88
89 model = Matrix()
90 model.data[3] = [-x, -y, -z, 1]
91 view = self.setup_camera(dx, dy, dz)
92 model_view = model * view
93 model_view_projection = model * view * self.proj
94
95 if self.use_fixed_pipeline:
96 glMatrixMode(GL_MODELVIEW)
97 glLoadMatrixf(model_view_projection.get_c_data())
98 else:
99 self.background_shader.uniform_matrixf('model_view', model_view.get_c_data())
100 self.background_shader.uniform_matrixf('projection', self.proj.get_c_data())
101
102 glEnable(GL_DEPTH_TEST)
103 for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in get_background_rendering_data(back):
104 glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
105 glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key].id)
106 glVertexPointer(3, GL_FLOAT, 0, vertices)
107 glTexCoordPointer(2, GL_FLOAT, 0, uvs)
108 glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
109 glDrawArrays(GL_QUADS, 0, nb_vertices)
110 glDisable(GL_DEPTH_TEST)
111 else:
112 glClear(GL_COLOR_BUFFER_BIT)
113
114 if game is not None:
115 if self.use_fixed_pipeline:
116 glMatrixMode(GL_MODELVIEW)
117 glLoadMatrixf(self.game_mvp.get_c_data())
118 glDisable(GL_FOG)
119 else:
120 self.game_shader.bind()
121 self.game_shader.uniform_matrixf('mvp', self.game_mvp.get_c_data())
122
123 self.render_elements(enemy for enemy in game.enemies if enemy.visible)
124 self.render_elements(game.effects)
125 self.render_elements(chain(game.players_bullets,
126 game.lasers_sprites(),
127 game.players,
128 game.msg_sprites()))
129 self.render_elements(chain(game.bullets, game.lasers,
130 game.cancelled_bullets, game.items,
131 game.labels))
132