comparison pytouhou/ui/opengl/gamerenderer.pyx @ 592:19d930f9e3f0

Add the screenshot feature, using P or Home like the original game.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 23 Apr 2014 19:19:32 +0200
parents e15672733c93
children ab131d04987d
comparison
equal deleted inserted replaced
591:2dfa4aa135d2 592:19d930f9e3f0
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 from libc.stdlib cimport free 15 from libc.stdlib cimport malloc, free
16 from itertools import chain 16 from itertools import chain
17 17
18 from pytouhou.lib.opengl cimport \ 18 from pytouhou.lib.opengl cimport \
19 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable, 19 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable,
20 glEnable, glFogi, glFogf, glFogfv, GL_PROJECTION, GL_MODELVIEW, 20 glEnable, glFogi, glFogf, glFogfv, GL_PROJECTION, GL_MODELVIEW,
21 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END, 21 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END,
22 GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat, glViewport, glScissor, 22 GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat, glViewport, glScissor,
23 GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT, glPushDebugGroup, 23 GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT, glPushDebugGroup,
24 GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup) 24 GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup, glBindTexture,
25 glGetTexImage, GL_TEXTURE_2D, GL_RGB, GL_UNSIGNED_BYTE)
25 26
26 from pytouhou.utils.matrix cimport mul, new_identity 27 from pytouhou.utils.matrix cimport mul, new_identity
27 from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d 28 from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d
28 from pytouhou.game.text cimport NativeText, GlyphCollection 29 from pytouhou.game.text cimport NativeText, GlyphCollection
29 from .shaders.eosd import GameShader, BackgroundShader, PassthroughShader 30 from .shaders.eosd import GameShader, BackgroundShader, PassthroughShader
30 from .renderer cimport Texture 31 from .renderer cimport Texture
31 from .backend cimport is_legacy, use_debug_group 32 from .backend cimport is_legacy, use_debug_group, use_pack_invert
32 33
33 from collections import namedtuple 34 from collections import namedtuple
34 Rect = namedtuple('Rect', 'x y w h') 35 Rect = namedtuple('Rect', 'x y w h')
35 Color = namedtuple('Color', 'r g b a') 36 Color = namedtuple('Color', 'r g b a')
36 37
101 self.passthrough_shader.uniform_matrix('mvp', self.interface_mvp) 102 self.passthrough_shader.uniform_matrix('mvp', self.interface_mvp)
102 self.framebuffer.render(self.x, self.y, self.width, self.height) 103 self.framebuffer.render(self.x, self.y, self.width, self.height)
103 104
104 if use_debug_group: 105 if use_debug_group:
105 glPopDebugGroup() 106 glPopDebugGroup()
107
108
109 def capture(self, filename, int width, int height):
110 capture_memory = <char*>malloc(width * height * 3)
111
112 glBindTexture(GL_TEXTURE_2D, self.framebuffer.texture)
113 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, capture_memory)
114 glBindTexture(GL_TEXTURE_2D, 0)
115
116 # TODO: output to PNG instead.
117
118 # PPM output, bottom to top.
119 with open(filename, 'wb') as ppm:
120 ppm.write(('P6\n%d %d\n 255\n' % (width, height)).encode())
121 if use_pack_invert:
122 ppm.write(capture_memory[:width * height * 3])
123 else:
124 for i in range(width * (height - 1), -1, -width):
125 ppm.write(capture_memory[i * 3:(i + width) * 3])
126
127 # Cleanup.
128 free(capture_memory)
106 129
107 130
108 cdef void render_game(self, Game game): 131 cdef void render_game(self, Game game):
109 cdef long game_x, game_y 132 cdef long game_x, game_y
110 cdef float x, y, z, dx, dy, dz 133 cdef float x, y, z, dx, dy, dz