Skip to content

Commit 541a811

Browse files
committed
Support using FBO as context manager
1 parent bdf9cec commit 541a811

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

demosys/opengl/fbo.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ def __init__(self):
3434
self.depth_buffer = None
3535
self.fbo = GL.glGenFramebuffers(1)
3636

37+
@property
38+
def size(self):
39+
# FIXME: How do we deal with attachments of different sizes?
40+
if self.color_buffers:
41+
return self.color_buffers[0].size
42+
if self.depth_buffer:
43+
return self.depth_buffer.size
44+
raise FBOError("Cannot determine size of FBO. No attachments.")
45+
46+
def __enter__(self):
47+
"""Entering context manager"""
48+
self.bind()
49+
return self
50+
51+
def __exit__(self, exc_type, exc_val, exc_tb):
52+
"""Exit context manager"""
53+
self.release()
54+
# We let exceptions propagate, so not returning anything
55+
3756
def bind(self, stack=True):
3857
"""Bind FBO adding it to the stack"""
3958
GL.glBindFramebuffer(GL.GL_FRAMEBUFFER, self.fbo)
@@ -64,14 +83,6 @@ def clear(self):
6483
GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT)
6584
self.release(stack=False)
6685

67-
@property
68-
def size(self):
69-
if self.color_buffers:
70-
return self.color_buffers[0].size
71-
if self.depth_buffer:
72-
return self.depth_buffer.size
73-
raise FBOError("Cannot determine size of FBO. No attachments.")
74-
7586
@classmethod
7687
def create(cls, width, height, depth=False, stencil=True,
7788
internal_format=GL.GL_RGBA8, format=GL.GL_RGBA, type=GL.GL_UNSIGNED_BYTE):

0 commit comments

Comments
 (0)