home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / extensions.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  2.5 KB  |  77 lines

  1. """Extension module support methods
  2.  
  3. This module provides the tools required to check whether
  4. an extension is available
  5. """
  6. VERSION_PREFIX = 'GL_VERSION_GL_'
  7. CURRENT_GL_VERSION = ''
  8. AVAILABLE_GL_EXTENSIONS = []
  9. AVAILABLE_GLU_EXTENSIONS = []
  10.  
  11. def hasGLExtension( specifier ):
  12.     """Given a string specifier, check for extension being available"""
  13.     global AVAILABLE_GL_EXTENSIONS, CURRENT_GL_VERSION
  14.     specifier = specifier.replace('.','_')
  15.     if specifier.startswith( VERSION_PREFIX ):
  16.         from OpenGL.GL import glGetString, GL_VERSION
  17.         if not CURRENT_GL_VERSION:
  18.             new = glGetString( GL_VERSION )
  19.             if new:
  20.                 CURRENT_GL_VERSION = [
  21.                     int(x) for x in new.split(' ',1)[0].split( '.' )
  22.                 ]
  23.             else:
  24.                 return False # not yet loaded/supported
  25.         specifier = [
  26.             int(x) 
  27.             for x in specifier[ len(VERSION_PREFIX):].split('_')
  28.         ]
  29.         return specifier <= CURRENT_GL_VERSION
  30.     else:
  31.         from OpenGL.GL import glGetString, GL_EXTENSIONS
  32.         if not AVAILABLE_GL_EXTENSIONS:
  33.             AVAILABLE_GL_EXTENSIONS[:] = glGetString( GL_EXTENSIONS ).split()
  34.         return specifier in AVAILABLE_GL_EXTENSIONS
  35.  
  36. def hasGLUExtension( specifier ):
  37.     """Given a string specifier, check for extension being available"""
  38.     from OpenGL.GLU import gluGetString, GLU_EXTENSIONS
  39.     if not AVAILABLE_GLU_EXTENSIONS:
  40.         AVAILABLE_GLU_EXTENSIONS[:] = gluGetString( GLU_EXTENSIONS )
  41.     return specifier.replace('.','_') in AVAILABLE_GLU_EXTENSIONS
  42.  
  43. class _Alternate( object ):
  44.     resolved = False 
  45.     implementation = None
  46.     def __init__( self, name, *alternates ):
  47.         """Initialize set of alternative implementations of the same function"""
  48.         self.__name__ = name
  49.         self._alternatives = alternates
  50.     def __nonzero__( self ):
  51.         for alternate in self._alternatives:
  52.             if alternate:
  53.                 return True 
  54.         return False
  55.     def __call__( self, *args, **named ):
  56.         """Call, doing a late lookup and bind to find an implementation"""
  57.         for alternate in self._alternatives:
  58.             if alternate:
  59.                 self.__class__.__call__ = alternate.__call__
  60.                 return self( *args, **named )
  61.         from OpenGL import error
  62.         raise error.NullFunctionError(
  63.             """Attempt to call an undefined alterate function (%s), check for bool(%s) before calling"""%(
  64.                 ', '.join([x.__name__ for x in self._alternatives]),
  65.                 self.__name__,
  66.             )
  67.         )
  68. def alternate( name, *functions ):
  69.     """Construct a callable that functions as the first implementation found of given set of alternatives
  70.     
  71.     if name is a function then its name will be used....
  72.     """
  73.     if not isinstance( name, (str,unicode)):
  74.         functions = (name,)+functions
  75.         name = name.__name__
  76.     return type( name, (_Alternate,), {} )( name, *functions )
  77.