<s> from pyglet . canvas . win32 import Win32Canvas <EOL> from base import Config , CanvasConfig , Context <EOL> from pyglet import gl <EOL> from pyglet . gl import gl_info <EOL> from pyglet . gl import wgl <EOL> from pyglet . gl import wglext_arb <EOL> from pyglet . gl import wgl_info <EOL> from pyglet . libs . win32 import _user32 , _kernel32 , _gdi32 <EOL> from pyglet . libs . win32 . constants import * <EOL> from pyglet . libs . win32 . types import * <EOL> class Win32Config ( Config ) : <EOL> def match ( self , canvas ) : <EOL> if not isinstance ( canvas , Win32Canvas ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if ( gl_info . have_context ( ) and <EOL> wgl_info . have_extension ( '<STR_LIT>' ) ) : <EOL> return self . _get_arb_pixel_format_matching_configs ( canvas ) <EOL> else : <EOL> return self . _get_pixel_format_descriptor_matching_configs ( canvas ) <EOL> def _get_pixel_format_descriptor_matching_configs ( self , canvas ) : <EOL> '''<STR_LIT>''' <EOL> pfd = PIXELFORMATDESCRIPTOR ( ) <EOL> pfd . nSize = sizeof ( PIXELFORMATDESCRIPTOR ) <EOL> pfd . nVersion = <NUM_LIT:1> <EOL> pfd . dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL <EOL> if self . double_buffer : <EOL> pfd . dwFlags |= PFD_DOUBLEBUFFER <EOL> else : <EOL> pfd . dwFlags |= PFD_DOUBLEBUFFER_DONTCARE <EOL> if self . stereo : <EOL> pfd . dwFlags |= PFD_STEREO <EOL> else : <EOL> pfd . dwFlags |= PFD_STEREO_DONTCARE <EOL> '''<STR_LIT>''' <EOL> if not self . depth_size : <EOL> pfd . dwFlags |= PFD_DEPTH_DONTCARE <EOL> pfd . iPixelType = PFD_TYPE_RGBA <EOL> pfd . cColorBits = self . buffer_size or <NUM_LIT:0> <EOL> pfd . cRedBits = self . red_size or <NUM_LIT:0> <EOL> pfd . cGreenBits = self . green_size or <NUM_LIT:0> <EOL> pfd . cBlueBits = self . blue_size or <NUM_LIT:0> <EOL> pfd . cAlphaBits = self . alpha_size or <NUM_LIT:0> <EOL> pfd . cAccumRedBits = self . accum_red_size or <NUM_LIT:0> <EOL> pfd . cAccumGreenBits = self . accum_green_size or <NUM_LIT:0> <EOL> pfd . cAccumBlueBits = self . accum_blue_size or <NUM_LIT:0> <EOL> pfd . cAccumAlphaBits = self . accum_alpha_size or <NUM_LIT:0> <EOL> pfd . cDepthBits = self . depth_size or <NUM_LIT:0> <EOL> pfd . cStencilBits = self . stencil_size or <NUM_LIT:0> <EOL> pfd . cAuxBuffers = self . aux_buffers or <NUM_LIT:0> <EOL> pf = _gdi32 . ChoosePixelFormat ( canvas . hdc , byref ( pfd ) ) <EOL> if pf : <EOL> return [ Win32CanvasConfig ( canvas , pf , self ) ] <EOL> else : <EOL> return [ ] <EOL> def _get_arb_pixel_format_matching_configs ( self , canvas ) : <EOL> '''<STR_LIT>''' <EOL> if self . sample_buffers or self . samples : <EOL> if not gl_info . have_extension ( '<STR_LIT>' ) : <EOL> return [ ] <EOL> attrs = [ ] <EOL> for name , value in self . get_gl_attributes ( ) : <EOL> attr = Win32CanvasConfigARB . attribute_ids . get ( name , None ) <EOL> if attr and value is not None : <EOL> attrs . extend ( [ attr , int ( value ) ] ) <EOL> attrs . append ( <NUM_LIT:0> ) <EOL> attrs = ( c_int * len ( attrs ) ) ( * attrs ) <EOL> pformats = ( c_int * <NUM_LIT:16> ) ( ) <EOL> nformats = c_uint ( <NUM_LIT:16> ) <EOL> wglext_arb . wglChoosePixelFormatARB ( canvas . hdc , attrs , None , <EOL> nformats , pformats , nformats ) <EOL> formats = [ Win32CanvasConfigARB ( canvas , pf , self ) for pf in pformats [ : nformats . value ] ] <EOL> return formats <EOL> class Win32CanvasConfig ( CanvasConfig ) : <EOL> def __init__ ( self , canvas , pf , config ) : <EOL> super ( Win32CanvasConfig , self ) . __init__ ( canvas , config ) <EOL> self . _pf = pf <EOL> self . _pfd = PIXELFORMATDESCRIPTOR ( ) <EOL> _gdi32 . DescribePixelFormat ( canvas . hdc , <EOL> self . _pf , sizeof ( PIXELFORMATDESCRIPTOR ) , byref ( self . _pfd ) ) <EOL> self . double_buffer = bool ( self . _pfd . dwFlags & PFD_DOUBLEBUFFER ) <EOL> self . sample_buffers = <NUM_LIT:0> <EOL> self . samples = <NUM_LIT:0> <EOL> self . stereo = bool ( self . _pfd . dwFlags & PFD_STEREO ) <EOL> self . buffer_size = self . _pfd . cColorBits <EOL> self . red_size = self . _pfd . cRedBits <EOL> self . green_size = self . _pfd . cGreenBits <EOL> self . blue_size = self . _pfd . cBlueBits <EOL> self . alpha_size = self . _pfd . cAlphaBits <EOL> self . accum_red_size = self . _pfd . cAccumRedBits <EOL> self . accum_green_size = self . _pfd . cAccumGreenBits <EOL> self . accum_blue_size = self . _pfd . cAccumBlueBits <EOL> self . accum_alpha_size = self . _pfd . cAccumAlphaBits <EOL> self . depth_size = self . _pfd . cDepthBits <EOL> self . stencil_size = self . _pfd . cStencilBits <EOL> self . aux_buffers = self . _pfd . cAuxBuffers <EOL> def compatible ( self , canvas ) : <EOL> return isinstance ( canvas , Win32Canvas ) <EOL> def create_context ( self , share ) : <EOL> return Win32Context ( self , share ) <EOL> def _set_pixel_format ( self , canvas ) : <EOL> _gdi32 . SetPixelFormat ( canvas . hdc , self . _pf , byref ( self . _pfd ) ) <EOL> class Win32CanvasConfigARB ( CanvasConfig ) : <EOL> attribute_ids = { <EOL> '<STR_LIT>' : wglext_arb . WGL_DOUBLE_BUFFER_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_STEREO_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_COLOR_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_AUX_BUFFERS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_SAMPLE_BUFFERS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_SAMPLES_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_RED_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_GREEN_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_BLUE_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_ALPHA_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_DEPTH_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_STENCIL_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_ACCUM_RED_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_ACCUM_GREEN_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_ACCUM_BLUE_BITS_ARB , <EOL> '<STR_LIT>' : wglext_arb . WGL_ACCUM_ALPHA_BITS_ARB , <EOL> } <EOL> def __init__ ( self , canvas , pf , config ) : <EOL> super ( Win32CanvasConfigARB , self ) . __init__ ( canvas , config ) <EOL> self . _pf = pf <EOL> names = self . attribute_ids . keys ( ) <EOL> attrs = self . attribute_ids . values ( ) <EOL> attrs = ( c_int * len ( attrs ) ) ( * attrs ) <EOL> values = ( c_int * len ( attrs ) ) ( ) <EOL> result = wglext_arb . wglGetPixelFormatAttribivARB ( canvas . hdc , <EOL> pf , <NUM_LIT:0> , len ( attrs ) , attrs , values ) <EOL> for name , value in zip ( names , values ) : <EOL> setattr ( self , name , value ) <EOL> def compatible ( self , canvas ) : <EOL> return isinstance ( canvas , Win32Canvas ) <EOL> def create_context ( self , share ) : <EOL> if wgl_info . have_extension ( '<STR_LIT>' ) : <EOL> return Win32ARBContext ( self , share ) <EOL> else : <EOL> return Win32Context ( self , share ) <EOL> def _set_pixel_format ( self , canvas ) : <EOL> _gdi32 . SetPixelFormat ( canvas . hdc , self . _pf , None ) <EOL> class Win32Context ( Context ) : <EOL> def __init__ ( self , config , share ) : <EOL> super ( Win32Context , self ) . __init__ ( config , share ) <EOL> self . _context = None <EOL> def attach ( self , canvas ) : <EOL> if self . config . _requires_gl_3 ( ) : <EOL> raise gl . ContextException ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> super ( Win32Context , self ) . attach ( canvas ) <EOL> self . config . _set_pixel_format ( canvas ) <EOL> self . _context = wgl . wglCreateContext ( canvas . hdc ) <EOL> share = self . context_share <EOL> if share : <EOL> if not share . canvas : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if not wgl . wglShareLists ( share . _context , self . _context ) : <EOL> raise gl . ContextException ( '<STR_LIT>' ) <EOL> def set_current ( self ) : <EOL> wgl . wglMakeCurrent ( self . canvas . hdc , self . _context ) <EOL> super ( Win32Context , self ) . set_current ( ) <EOL> def detach ( self ) : <EOL> if self . canvas : <EOL> wgl . wglDeleteContext ( self . _context ) <EOL> self . _context = None <EOL> super ( Win32Context , self ) . detach ( ) <EOL> def flip ( self ) : <EOL> wgl . wglSwapLayerBuffers ( self . canvas . hdc , wgl . WGL_SWAP_MAIN_PLANE ) <EOL> def get_vsync ( self ) : <EOL> if wgl_info . have_extension ( '<STR_LIT>' ) : <EOL> return bool ( wglext_arb . wglGetSwapIntervalEXT ( ) ) <EOL> def set_vsync ( self , vsync ) : <EOL> if wgl_info . have_extension ( '<STR_LIT>' ) : <EOL> wglext_arb . wglSwapIntervalEXT ( int ( vsync ) ) <EOL> class Win32ARBContext ( Win32Context ) : <EOL> def __init__ ( self , config , share ) : <EOL> super ( Win32ARBContext , self ) . __init__ ( config , share ) <EOL> def attach ( self , canvas ) : <EOL> super ( Win32ARBContext , self ) . attach ( canvas ) <EOL> share = self . context_share <EOL> if share : <EOL> if not share . canvas : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> share = share . _context <EOL> attribs = [ ] <EOL> if self . config . major_version is not None : <EOL> attribs . extend ( [ wglext_arb . WGL_CONTEXT_MAJOR_VERSION_ARB , <EOL> self . config . major_version ] ) <EOL> if self . config . minor_version is not None : <EOL> attribs . extend ( [ wglext_arb . WGL_CONTEXT_MINOR_VERSION_ARB , <EOL> self . config . minor_version ] ) <EOL> flags = <NUM_LIT:0> <EOL> if self . config . forward_compatible : <EOL> flags |= wglext_arb . WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB <EOL> if self . config . debug : <EOL> flags |= wglext_arb . WGL_DEBUG_BIT_ARB <EOL> if flags : <EOL> attribs . extend ( [ wglext_arb . WGL_CONTEXT_FLAGS_ARB , flags ] ) <EOL> attribs . append ( <NUM_LIT:0> ) <EOL> attribs = ( c_int * len ( attribs ) ) ( * attribs ) <EOL> self . config . _set_pixel_format ( canvas ) <EOL> self . _context = wglext_arb . wglCreateContextAttribsARB ( canvas . hdc , <EOL> share , attribs ) </s>
