Skip to content

Commit a24b6e6

Browse files
committed
Use context managers in examples
1 parent 26dae41 commit a24b6e6

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

demosys/effects/default/effect.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def draw(self, time, target):
2525
m_normal = self.create_normal_matrix(m_mv)
2626

2727
# Draw the cube
28-
self.cube.bind(self.shader)
29-
self.shader.uniform_mat4("m_proj", self.sys_camera.projection)
30-
self.shader.uniform_mat4("m_mv", m_mv)
31-
self.shader.uniform_mat3("m_normal", m_normal)
28+
with self.cube.bind(self.shader) as shader:
29+
shader.uniform_mat4("m_proj", self.sys_camera.projection)
30+
shader.uniform_mat4("m_mv", m_mv)
31+
shader.uniform_mat3("m_normal", m_normal)
3232
self.cube.draw()

demosys_test/cube/effect.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,20 @@ def draw(self, time, target):
2525
GL.glEnable(GL.GL_DEPTH_TEST)
2626
GL.glEnable(GL.GL_CULL_FACE)
2727

28-
self.fbo.bind()
29-
3028
mv_m = self.create_transformation(rotation=(time * 1.2, time * 2.1, time * 0.25),
3129
translation=(0.0, 0.0, -8.0))
3230
normal_m = self.create_normal_matrix(mv_m)
3331
proj_m = self.create_projection(fov=60.0, ratio=1.0)
3432

35-
self.cube.bind(self.cube_shader1)
36-
self.cube_shader1.uniform_mat4("m_proj", proj_m)
37-
self.cube_shader1.uniform_mat4("m_mv", mv_m)
38-
self.cube_shader1.uniform_mat3("m_normal", normal_m)
39-
self.cube_shader1.uniform_sampler_2d(0, "texture0", self.texture1)
40-
self.cube_shader1.uniform_sampler_2d(1, "texture1", self.texture2)
41-
self.cube_shader1.uniform_1f("time", time)
42-
self.cube.draw()
43-
44-
self.fbo.release()
33+
with self.fbo:
34+
with self.cube.bind(self.cube_shader1) as shader:
35+
shader.uniform_mat4("m_proj", proj_m)
36+
shader.uniform_mat4("m_mv", mv_m)
37+
shader.uniform_mat3("m_normal", normal_m)
38+
shader.uniform_sampler_2d(0, "texture0", self.texture1)
39+
shader.uniform_sampler_2d(1, "texture1", self.texture2)
40+
shader.uniform_1f("time", time)
41+
self.cube.draw()
4542

4643
# Test camera
4744
self.sys_camera.set_projection(near=0.1, far=1000)
@@ -53,13 +50,13 @@ def draw(self, time, target):
5350
view_m = self.sys_camera.view_matrix
5451
normal_m = self.create_normal_matrix(view_m)
5552

56-
self.points.bind(self.cube_shader2)
57-
self.cube_shader2.uniform_mat4("m_proj", self.sys_camera.projection)
58-
self.cube_shader2.uniform_mat4("m_mv", view_m)
59-
self.cube_shader2.uniform_mat3("m_normal", normal_m)
60-
self.cube_shader2.uniform_sampler_2d(0, "texture0", self.fbo.color_buffers[0])
61-
self.cube_shader2.uniform_1f("time", time)
62-
self.cube_shader2.uniform_3f("lightpos", 0.0, 0.0, 0.0)
53+
with self.points.bind(self.cube_shader2) as shader:
54+
shader.uniform_mat4("m_proj", self.sys_camera.projection)
55+
shader.uniform_mat4("m_mv", view_m)
56+
shader.uniform_mat3("m_normal", normal_m)
57+
shader.uniform_sampler_2d(0, "texture0", self.fbo.color_buffers[0])
58+
shader.uniform_1f("time", time)
59+
shader.uniform_3f("lightpos", 0.0, 0.0, 0.0)
6360
self.points.draw(mode=GL.GL_POINTS)
6461

6562
GL.glClearColor(0.5, 0.5, 0.5, 1)

0 commit comments

Comments
 (0)