annotate pytouhou/ui/opengl/gamerenderer.pyx @ 596:ab131d04987d

Fix a regression introduced in the previous commit when the interface has a different resolution than 640×480 and framebuffer_blit is enabled.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 25 Oct 2014 18:49:41 +0200
parents 19d930f9e3f0
children 3c2f96f1d715
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 ## GNU General Public License for more details.
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 ##
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
15 from libc.stdlib cimport malloc, free
201
220c122f428c Batch more sprites!
Thibaut Girka <thib@sitedethib.com>
parents: 199
diff changeset
16 from itertools import chain
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents: 130
diff changeset
17
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
18 from pytouhou.lib.opengl cimport \
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
19 (glClear, glMatrixMode, glLoadIdentity, glLoadMatrixf, glDisable,
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
20 glEnable, glFogi, glFogf, glFogfv, GL_PROJECTION, GL_MODELVIEW,
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
21 GL_FOG, GL_FOG_MODE, GL_LINEAR, GL_FOG_START, GL_FOG_END,
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
22 GL_FOG_COLOR, GL_COLOR_BUFFER_BIT, GLfloat, glViewport, glScissor,
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 569
diff changeset
23 GL_SCISSOR_TEST, GL_DEPTH_BUFFER_BIT, glPushDebugGroup,
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
24 GL_DEBUG_SOURCE_APPLICATION, glPopDebugGroup, glBindTexture,
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
25 glGetTexImage, GL_TEXTURE_2D, GL_RGB, GL_UNSIGNED_BYTE)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
27 from pytouhou.utils.matrix cimport mul, new_identity
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
28 from pytouhou.utils.maths cimport perspective, setup_camera, ortho_2d
473
1c891c71cf22 Cythonize pytouhou.game.text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 468
diff changeset
29 from pytouhou.game.text cimport NativeText, GlyphCollection
596
ab131d04987d Fix a regression introduced in the previous commit when the interface has a different resolution than 640×480 and framebuffer_blit is enabled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 592
diff changeset
30 from pytouhou.ui.window cimport Window
462
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
31 from .shaders.eosd import GameShader, BackgroundShader, PassthroughShader
505
bfea9e9a6845 Manage the texture-specific indices in the Texture, and some more renderer optimisations.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 503
diff changeset
32 from .renderer cimport Texture
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
33 from .backend cimport is_legacy, use_debug_group, use_pack_invert
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
34
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
35 from collections import namedtuple
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
36 Rect = namedtuple('Rect', 'x y w h')
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
37 Color = namedtuple('Color', 'r g b a')
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
38
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
458
1b56d62250ab Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
40 cdef class GameRenderer(Renderer):
596
ab131d04987d Fix a regression introduced in the previous commit when the interface has a different resolution than 640×480 and framebuffer_blit is enabled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 592
diff changeset
41 def __init__(self, resource_loader, Window window):
222
5cac48b328ad Refactor rendering code a bit.
Thibaut Girka <thib@sitedethib.com>
parents: 221
diff changeset
42 Renderer.__init__(self, resource_loader)
254
6bd565019f9a Preload textures to avoid slowdowns during gameplay
Thibaut Girka <thib@sitedethib.com>
parents: 242
diff changeset
43
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
44 if not is_legacy:
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
45 self.game_shader = GameShader()
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
46 self.background_shader = BackgroundShader()
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
47 self.interface_shader = self.game_shader
462
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
48 self.passthrough_shader = PassthroughShader()
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
49
596
ab131d04987d Fix a regression introduced in the previous commit when the interface has a different resolution than 640×480 and framebuffer_blit is enabled.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 592
diff changeset
50 self.framebuffer = Framebuffer(0, 0, window.width, window.height)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
52
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
53 def __dealloc__(self):
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
54 if self.game_mvp != NULL:
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
55 free(self.game_mvp)
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
56 if self.interface_mvp != NULL:
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
57 free(self.interface_mvp)
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
58 if self.proj != NULL:
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
59 free(self.proj)
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
60
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
61
511
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
62 property size:
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
63 # We never need to get back the computed size, so size is write-only.
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
64 def __set__(self, tuple size):
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
65 self.x, self.y, self.width, self.height = size
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
66
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
67
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
68 def load_textures(self, dict anms):
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
69 self.texture_manager.load(anms)
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
70
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
71
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
72 def load_background(self, background):
519
b18f0bd30ad0 Optimise background rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 517
diff changeset
73 self.background = background
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
74 if background is not None:
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
75 self.background_renderer = BackgroundRenderer()
586
4b0593da29d5 Simplify framebuffer rendering with glDrawArrays, and move it all to its own file.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 582
diff changeset
76 self.background_renderer.load(background, self.textures)
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
77 else:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
78 self.background_renderer = None
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
79
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
80
511
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
81 def start(self, common):
503
c622eaf64428 Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 473
diff changeset
82 self.proj = perspective(30, float(common.width) / float(common.height),
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
83 101010101./2010101., 101010101./10101.)
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
84 self.game_mvp = setup_camera(0, 0, 1)
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
85 mul(self.game_mvp, self.proj)
503
c622eaf64428 Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 473
diff changeset
86 self.interface_mvp = ortho_2d(0., float(common.interface.width),
c622eaf64428 Optimize GameRunner some more, fix replay, and remove Window dependency in Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 473
diff changeset
87 float(common.interface.height), 0.)
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
88
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
89
511
2e8ceaa85d5c Don’t type the renderer in GameRunner, so that we can switch it without type issues.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 505
diff changeset
90 def render(self, Game game):
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
91 if not is_legacy:
462
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
92 self.framebuffer.bind()
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
93
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
94 self.render_game(game)
516
577c3a88fb67 Merge the lists Game.texts and Game.native_texts into the Game.texts dict.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
95 self.render_text(game.texts)
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
96 self.render_interface(game.interface, game.boss)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
97
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
98 if not is_legacy:
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
99 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
100 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Scaled rendering")
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
101
462
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
102 self.passthrough_shader.bind()
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
103 self.passthrough_shader.uniform_matrix('mvp', self.interface_mvp)
587
6c9d8a3d853f Use ARB_framebuffer_blit instead of a second rendering pass for scaled rendering, if supported, and remove framebuffer stuff from the Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 586
diff changeset
104 self.framebuffer.render(self.x, self.y, self.width, self.height)
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
105
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
106 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
107 glPopDebugGroup()
462
a71b912b45b7 Render to framebuffers first, and reposition some interface elements in the game area.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 459
diff changeset
108
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
109
592
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
110 def capture(self, filename, int width, int height):
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
111 capture_memory = <char*>malloc(width * height * 3)
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
112
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
113 glBindTexture(GL_TEXTURE_2D, self.framebuffer.texture)
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
114 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, capture_memory)
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
115 glBindTexture(GL_TEXTURE_2D, 0)
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
116
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
117 # TODO: output to PNG instead.
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
118
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
119 # PPM output, bottom to top.
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
120 with open(filename, 'wb') as ppm:
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
121 ppm.write(('P6\n%d %d\n 255\n' % (width, height)).encode())
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
122 if use_pack_invert:
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
123 ppm.write(capture_memory[:width * height * 3])
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
124 else:
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
125 for i in range(width * (height - 1), -1, -width):
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
126 ppm.write(capture_memory[i * 3:(i + width) * 3])
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
127
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
128 # Cleanup.
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
129 free(capture_memory)
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
130
19d930f9e3f0 Add the screenshot feature, using P or Home like the original game.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 590
diff changeset
131
468
feecdb4a8928 Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 464
diff changeset
132 cdef void render_game(self, Game game):
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
133 cdef long game_x, game_y
540
53fa73932e9a Fix warnings introduced in Cython 0.20, when more than one pointer is defined on the same line.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
134 cdef float x, y, z, dx, dy, dz
53fa73932e9a Fix warnings introduced in Cython 0.20, when more than one pointer is defined on the same line.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
135 cdef float fog_data[4]
53fa73932e9a Fix warnings introduced in Cython 0.20, when more than one pointer is defined on the same line.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
136 cdef float fog_start, fog_end
458
1b56d62250ab Make pytouhou.ui.{window,shader,game{runner,renderer}} extension types.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
137 cdef unsigned char fog_r, fog_g, fog_b
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
138 cdef Matrix *mvp
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
139
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
140 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
141 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Game rendering")
579
b8df946d394d Add grouping for OpenGL calls, making traces much more readable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 569
diff changeset
142
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
143 game_x, game_y = game.interface.game_pos
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
144 glViewport(game_x, game_y, game.width, game.height)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
145 glClear(GL_DEPTH_BUFFER_BIT)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
146 glScissor(game_x, game_y, game.width, game.height)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
147 glEnable(GL_SCISSOR_TEST)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
148
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
149 if is_legacy:
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
150 glMatrixMode(GL_PROJECTION)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
151 glLoadIdentity()
217
577f45454402 Change background during spellcards.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 205
diff changeset
152
569
b21922a03830 Don’t render spellcard background when --no-background is passed.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 560
diff changeset
153 if self.background_renderer is None:
b21922a03830 Don’t render spellcard background when --no-background is passed.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 560
diff changeset
154 glClear(GL_COLOR_BUFFER_BIT)
b21922a03830 Don’t render spellcard background when --no-background is passed.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 560
diff changeset
155 elif game is not None and game.spellcard_effect is not None:
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
156 if is_legacy:
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
157 glMatrixMode(GL_MODELVIEW)
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
158 glLoadMatrixf(<GLfloat*>self.game_mvp)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
159 glDisable(GL_FOG)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
160 else:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
161 self.game_shader.bind()
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
162 self.game_shader.uniform_matrix('mvp', self.game_mvp)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
163
306
52d791bb7c32 Rename a few attributes/methods to make a little more sense.
Thibaut Girka <thib@sitedethib.com>
parents: 304
diff changeset
164 self.render_elements([game.spellcard_effect])
569
b21922a03830 Don’t render spellcard background when --no-background is passed.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 560
diff changeset
165 else:
519
b18f0bd30ad0 Optimise background rendering.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 517
diff changeset
166 back = self.background
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
167 x, y, z = back.position_interpolator.values
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
168 dx, dy, dz = back.position2_interpolator.values
399
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 398
diff changeset
169 fog_b, fog_g, fog_r, fog_start, fog_end = back.fog_interpolator.values
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 398
diff changeset
170
401
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
171 # Those two lines may come from the difference between Direct3D and
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
172 # OpenGL’s distance handling. The first one seem to calculate fog
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
173 # from the eye, while the second does that starting from the near
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
174 # plane.
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
175 #TODO: investigate, and use a variable to keep the near plane
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
176 # distance at a single place.
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
177 fog_start -= 101010101./2010101.
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
178 fog_end -= 101010101./2010101.
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
179
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
180 mvp = new_identity()
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
181 mvp_data = <GLfloat*>mvp
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
182 mvp_data[12] = -x
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
183 mvp_data[13] = -y
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
184 mvp_data[14] = -z
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 406
diff changeset
185 view = setup_camera(dx, dy, dz)
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
186 mul(mvp, view)
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
187 free(view)
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
188 mul(mvp, self.proj)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
189
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
190 if is_legacy:
401
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
191 glMatrixMode(GL_MODELVIEW)
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
192 glLoadMatrixf(mvp_data)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
193
401
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
194 glEnable(GL_FOG)
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
195 glFogi(GL_FOG_MODE, GL_LINEAR)
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
196 glFogf(GL_FOG_START, fog_start)
3ce4065840e9 Calculate the fog per-fragment and remove the fixed-pipeline glFog* functions; now both vertex shaders are the same.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 399
diff changeset
197 glFogf(GL_FOG_END, fog_end)
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
198
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
199 fog_data[0] = fog_r / 255.
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
200 fog_data[1] = fog_g / 255.
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
201 fog_data[2] = fog_b / 255.
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
202 fog_data[3] = 1.
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
203 glFogfv(GL_FOG_COLOR, fog_data)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
204 else:
399
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 398
diff changeset
205 self.background_shader.bind()
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
206 self.background_shader.uniform_matrix('mvp', mvp)
399
1c773544eaeb Make the background use a single vbo and offsets, just like the 2D code.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 398
diff changeset
207
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
208 self.background_shader.uniform_1('fog_scale', 1. / (fog_end - fog_start))
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
209 self.background_shader.uniform_1('fog_end', fog_end)
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
210 self.background_shader.uniform_4('fog_color', fog_r / 255., fog_g / 255., fog_b / 255., 1.)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
211
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
212 free(mvp)
423
d8630c086926 Replace Pyglet with our own Cython OpenGL wrapper.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 422
diff changeset
213 self.background_renderer.render_background()
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
214
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
215 if game is not None:
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
216 if is_legacy:
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
217 glMatrixMode(GL_MODELVIEW)
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
218 glLoadMatrixf(<GLfloat*>self.game_mvp)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
219 glDisable(GL_FOG)
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
220 else:
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 345
diff changeset
221 self.game_shader.bind()
424
f4d76d3d6f2a Make the Shader class use cython too.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 423
diff changeset
222 self.game_shader.uniform_matrix('mvp', self.game_mvp)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
223
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
224 self.render_elements([enemy for enemy in game.enemies if enemy.visible])
166
dcf32488a2c9 Better enemy death, with animation and (hopefully) correct flags handling.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 164
diff changeset
225 self.render_elements(game.effects)
201
220c122f428c Batch more sprites!
Thibaut Girka <thib@sitedethib.com>
parents: 199
diff changeset
226 self.render_elements(chain(game.players_bullets,
294
94c636f8f863 Add player lasers for MarisaB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 286
diff changeset
227 game.lasers_sprites(),
201
220c122f428c Batch more sprites!
Thibaut Girka <thib@sitedethib.com>
parents: 199
diff changeset
228 game.players,
384
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
229 game.msg_sprites()))
315
e935ed8dc5e6 Add out-of-screen item indicators.
Thibaut Girka <thib@sitedethib.com>
parents: 306
diff changeset
230 self.render_elements(chain(game.bullets, game.lasers,
e935ed8dc5e6 Add out-of-screen item indicators.
Thibaut Girka <thib@sitedethib.com>
parents: 306
diff changeset
231 game.cancelled_bullets, game.items,
384
690b5faaa0e6 Make rendering of multiple-sprites elements work like single-sprites.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 383
diff changeset
232 game.labels))
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
233
468
feecdb4a8928 Add “except *” to cdef void functions, and type some more.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 464
diff changeset
234 if game.msg_runner is not None:
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
235 rect = Rect(48, 368, 288, 48)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
236 color1 = Color(0, 0, 0, 192)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
237 color2 = Color(0, 0, 0, 128)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
238 self.render_quads([rect], [(color1, color1, color2, color2)], 0)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
239
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
240 glDisable(GL_SCISSOR_TEST)
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
241
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
242 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
243 glPopDebugGroup()
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
244
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
245
516
577c3a88fb67 Merge the lists Game.texts and Game.native_texts into the Game.texts dict.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
246 cdef void render_text(self, dict texts):
473
1c891c71cf22 Cythonize pytouhou.game.text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 468
diff changeset
247 cdef NativeText label
1c891c71cf22 Cythonize pytouhou.game.text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 468
diff changeset
248
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
249 if self.font_manager is None:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
250 return
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
251
517
dec43940f092 Don’t crash if SDL2_ttf couldn’t render a specific string.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 516
diff changeset
252 self.font_manager.load(texts)
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
253
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
254 black = Color(0, 0, 0, 255)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
255
590
e15672733c93 Switch to Python 3.x instead of 2.7.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
256 for label in texts.values():
517
dec43940f092 Don’t crash if SDL2_ttf couldn’t render a specific string.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 516
diff changeset
257 texture = (<Texture>label.texture).texture
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
258 rect = Rect(label.x, label.y, label.width, label.height)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
259 gradient = [Color(*color, a=label.alpha) for color in label.gradient]
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
260
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
261 if label.shadow:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
262 shadow_rect = Rect(label.x + 1, label.y + 1, label.width, label.height)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
263 shadow = [black._replace(a=label.alpha)] * 4
517
dec43940f092 Don’t crash if SDL2_ttf couldn’t render a specific string.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 516
diff changeset
264 self.render_quads([shadow_rect, rect], [shadow, gradient], texture)
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
265 else:
517
dec43940f092 Don’t crash if SDL2_ttf couldn’t render a specific string.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 516
diff changeset
266 self.render_quads([rect], [gradient], texture)
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
267
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
268
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
269 cdef void render_interface(self, interface, game_boss):
473
1c891c71cf22 Cythonize pytouhou.game.text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 468
diff changeset
270 cdef GlyphCollection label
1c891c71cf22 Cythonize pytouhou.game.text.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 468
diff changeset
271
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
272 elements = []
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
273
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
274 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
275 glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Interface rendering")
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
276
560
c759b97f4f81 Remove all reference to the old fixed pipeline option in the OpenGL backend, use the newer is_legacy one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 553
diff changeset
277 if is_legacy:
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
278 glMatrixMode(GL_MODELVIEW)
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 519
diff changeset
279 glLoadMatrixf(<GLfloat*>self.interface_mvp)
459
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
280 glDisable(GL_FOG)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
281 else:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
282 self.interface_shader.bind()
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
283 self.interface_shader.uniform_matrix('mvp', self.interface_mvp)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
284 glViewport(0, 0, interface.width, interface.height)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
285
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
286 items = [item for item in interface.items if item.anmrunner and item.anmrunner.running]
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
287 labels = interface.labels.values()
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
288
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
289 if items:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
290 # Redraw all the interface
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
291 elements.extend(items)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
292 else:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
293 # Redraw only changed labels
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
294 labels = [label for label in labels if label.changed]
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
295
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
296 elements.extend(interface.level_start)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
297
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
298 if game_boss is not None:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
299 elements.extend(interface.boss_items)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
300
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
301 elements.extend(labels)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
302 self.render_elements(elements)
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
303 for label in labels:
6e733ed817bd Move every rendering function from gamerunner to gamerenderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 458
diff changeset
304 label.changed = False
582
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
305
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
306 if use_debug_group:
6e79756b7f42 Don’t call gl*DebugGroup if it isn’t exposed by the driver.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 579
diff changeset
307 glPopDebugGroup()