Skip to content

Commit f46e780

Browse files
Add attributes and begin to GL.
These provide a context manager for managing glPushAttrib, glPopAttrib, glBegin and glEnd.
1 parent 139b6d1 commit f46e780

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

pygly/gl.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
.. moduleauthor:: Adam Griffiths <[email protected]>
55
'''
66

7+
from contextlib import contextmanager
8+
79
from pyglet.gl import *
810

911
from pyrr import rectangle
@@ -12,6 +14,10 @@
1214
def set_viewport( rect ):
1315
"""Calls glViewport with the dimensions of
1416
the rectangle
17+
18+
This call can be undone by first calling
19+
glPushAttrib( GL_VIEWPORT_BIT )
20+
and later calling glPopAttrib().
1521
"""
1622
glViewport(
1723
int(rect[ 0 ][ 0 ]),
@@ -30,6 +36,10 @@ def set_scissor( rect ):
3036
.. seealso::
3137
Module :py:mod:`pygly.window`
3238
Documentation of the :py:mod:`pygly.window` module.
39+
40+
This call can be undone by first calling
41+
glPushAttrib( GL_SCISSOR_BIT )
42+
and later calling glPopAttrib().
3343
"""
3444
glScissor(
3545
int(rect[ (0,0) ]),
@@ -38,3 +48,34 @@ def set_scissor( rect ):
3848
int(rect[ (1,1) ])
3949
)
4050

51+
@contextmanager
52+
def attributes( attributes ):
53+
"""Wraps glPushAttrib and glPopAttrib
54+
in a context manager, providing the 'with'
55+
keyword.
56+
57+
For example:
58+
with gl.attributes( GL_VIEWPORT_BIT ):
59+
glViewport( 0, 0, 100, 100 )
60+
"""
61+
glPushAttrib( attributes )
62+
yield
63+
glPopAttrib()
64+
65+
@contextmanager
66+
def begin( mode ):
67+
"""Wraps glBegin and glEnd in a
68+
context manager, providing the 'with'
69+
keyword.
70+
71+
For example:
72+
with gl.begin( GL_TRIANGLES ):
73+
glVertex2f( 0.0, 0.0 )
74+
glVertex2f( 0.5, 1.0 )
75+
glVertex2f( 1.0, 0.0 )
76+
"""
77+
glBegin( mode )
78+
yield
79+
glEnd()
80+
81+

0 commit comments

Comments
 (0)