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 / platform / baseplatform.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  8.2 KB  |  283 lines

  1. """Base class for platform implementations
  2. """
  3. import ctypes
  4. from OpenGL.platform import ctypesloader
  5. import sys
  6. import OpenGL as top_level_module
  7. from OpenGL import logs
  8.  
  9. class BasePlatform( object ):
  10.     """Base class for per-platform implementations
  11.     
  12.     Attributes of note:
  13.     
  14.         EXPORTED_NAMES -- set of names exported via the platform 
  15.             module's namespace...
  16.     
  17.         GL, GLU, GLUT, GLE, OpenGL -- ctypes libraries
  18.     
  19.         DEFAULT_FUNCTION_TYPE -- used as the default function 
  20.             type for functions unless overridden on a per-DLL
  21.             basis with a "FunctionType" member
  22.         
  23.         GLUT_GUARD_CALLBACKS -- if True, the GLUT wrappers 
  24.             will provide guarding wrappers to prevent GLUT 
  25.             errors with uninitialised GLUT.
  26.         
  27.         EXTENSIONS_USE_BASE_FUNCTIONS -- if True, uses regular
  28.             dll attribute-based lookup to retrieve extension 
  29.             function pointers.
  30.     """
  31.     
  32.     EXPORTED_NAMES = [
  33.         'GetCurrentContext','CurrentContextIsValid','safeGetError',
  34.         'createBaseFunction', 'createExtensionFunction', 'copyBaseFunction',
  35.         'GL','GLU','GLUT','GLE','OpenGL',
  36.         'getGLUTFontPointer',
  37.         'GLUT_GUARD_CALLBACKS',
  38.     ]
  39.  
  40.     
  41.     DEFAULT_FUNCTION_TYPE = None
  42.     GLUT_GUARD_CALLBACKS = False
  43.     EXTENSIONS_USE_BASE_FUNCTIONS = False
  44.     
  45.     def install( self, namespace ):
  46.         """Install this platform instance into the platform module"""
  47.         for name in self.EXPORTED_NAMES:
  48.             namespace[ name ] = getattr(self,name)
  49.         namespace['PLATFORM'] = self
  50.         return self
  51.     
  52.     def functionTypeFor( self, dll ):
  53.         """Given a DLL, determine appropriate function type..."""
  54.         if hasattr( dll, 'FunctionType' ):
  55.             return dll.FunctionType
  56.         else:
  57.             return self.DEFAULT_FUNCTION_TYPE
  58.     
  59.     def errorChecking( self, func ):
  60.         """Add error checking to the function if appropriate"""
  61.         from OpenGL import error
  62.         if top_level_module.ERROR_CHECKING:
  63.             func.errcheck = error.glCheckError
  64.         return func
  65.     def wrapLogging( self, func ):
  66.         """Wrap function with logging operations if appropriate"""
  67.         return logs.logOnFail( func, logs.getLog( 'OpenGL.errors' ))
  68.     
  69.     def constructFunction( 
  70.         self,
  71.         functionName, dll, 
  72.         resultType=ctypes.c_int, argTypes=(),
  73.         doc = None, argNames = (),
  74.         extension = None,
  75.     ):
  76.         """Core operation to create a new base ctypes function
  77.         
  78.         raises AttributeError if can't find the procedure...
  79.         """
  80.         if extension and not self.checkExtension( extension ):
  81.             raise AttributeError( """Extension not available""" )
  82.         if extension and not self.EXTENSIONS_USE_BASE_FUNCTIONS:
  83.             # what about the VERSION values???
  84.             if self.checkExtension( extension ):
  85.                 pointer = self.getExtensionProcedure( functionName )
  86.                 if pointer:
  87.                     func = self.functionTypeFor( dll )(
  88.                         resultType,
  89.                         *argTypes
  90.                     )(
  91.                         pointer
  92.                     )
  93.                 else:
  94.                     AttributeError( """Extension %r available, but no pointer for function %r"""%(extension,functionName))
  95.             else:
  96.                 raise AttributeError( """No extension %r"""%(extension,))
  97.         else:
  98.             func = ctypesloader.buildFunction(
  99.                 self.functionTypeFor( dll )(
  100.                     resultType,
  101.                     *argTypes
  102.                 ),
  103.                 functionName,
  104.                 dll,
  105.             )
  106.         func.__doc__ = doc 
  107.         func.argNames = list(argNames or ())
  108.         func.__name__ = functionName
  109.         func.DLL = dll
  110.         func.extension = extension
  111.         func = self.wrapLogging( self.errorChecking( func ))
  112.         return func
  113.  
  114.     def createBaseFunction( 
  115.         self,
  116.         functionName, dll, 
  117.         resultType=ctypes.c_int, argTypes=(),
  118.         doc = None, argNames = (),
  119.         extension = None,
  120.     ):
  121.         """Create a base function for given name
  122.         
  123.         Normally you can just use the dll.name hook to get the object,
  124.         but we want to be able to create different bindings for the 
  125.         same function, so we do the work manually here to produce a
  126.         base function from a DLL.
  127.         """
  128.         from OpenGL import wrapper
  129.         try:
  130.             return self.constructFunction(
  131.                 functionName, dll, 
  132.                 resultType=resultType, argTypes=argTypes,
  133.                 doc = doc, argNames = argNames,
  134.                 extension = extension,
  135.             )
  136.         except AttributeError, err:
  137.             return self.nullFunction( 
  138.                 functionName, dll=dll,
  139.                 resultType=resultType, 
  140.                 argTypes=argTypes,
  141.                 doc = doc, argNames = argNames,
  142.                 extension = extension,
  143.             )
  144.     def checkExtension( self, name ):
  145.         """Check whether the given extension is supported by current context"""
  146.         if not name:
  147.             return True
  148.         context = self.GetCurrentContext()
  149.         if context:
  150.             from OpenGL import contextdata
  151.             from OpenGL.raw.GL import GL_EXTENSIONS
  152.             set = contextdata.getValue( GL_EXTENSIONS, context=context )
  153.             if set is None:
  154.                 set = {}
  155.                 contextdata.setValue( 
  156.                     GL_EXTENSIONS, set, context=context, weak=False 
  157.                 )
  158.             current = set.get( name )
  159.             if current is None:
  160.                 from OpenGL import extensions
  161.                 result = extensions.hasGLExtension( name )
  162.                 set[name] = result 
  163.                 return result
  164.             return current
  165.         else:
  166.             return False
  167.     createExtensionFunction = createBaseFunction
  168.  
  169.     def copyBaseFunction( self, original ):
  170.         """Create a new base function based on an already-created function
  171.         
  172.         This is normally used to provide type-specific convenience versions of
  173.         a definition created by the automated generator.
  174.         """
  175.         from OpenGL import wrapper, error
  176.         if isinstance( original, _NullFunctionPointer ):
  177.             return self.nullFunction(
  178.                 original.__name__,
  179.                 original.DLL,
  180.                 resultType = original.restype,
  181.                 argTypes= original.argtypes,
  182.                 doc = original.__doc__,
  183.                 argNames = original.argNames,
  184.                 extension = original.extension,
  185.             )
  186.         elif hasattr( original, 'originalFunction' ):
  187.             original = original.originalFunction
  188.         return self.createBaseFunction(
  189.             original.__name__, original.DLL, 
  190.             resultType=original.restype, argTypes=original.argtypes,
  191.             doc = original.__doc__, argNames = original.argNames,
  192.             extension = original.extension,
  193.         )
  194.     def nullFunction( 
  195.         self,
  196.         functionName, dll,
  197.         resultType=ctypes.c_int, 
  198.         argTypes=(),
  199.         doc = None, argNames = (),
  200.         extension = None,
  201.     ):
  202.         """Construct a "null" function pointer"""
  203.         cls = type( functionName, (_NullFunctionPointer,), {
  204.             '__doc__': doc,
  205.         } )
  206.         return cls(
  207.             functionName, dll, resultType, argTypes, argNames, extension=extension, doc=doc,
  208.         )
  209.     def GetCurrentContext( self ):
  210.         """Retrieve opaque pointer for the current context"""
  211.         raise NotImplementedError( 
  212.             """Platform does not define a GetCurrentContext function""" 
  213.         )
  214.     def CurrentContextIsValid( self ):
  215.         """Return boolean of whether current context is valid"""
  216.         raise NotImplementedError( 
  217.             """Platform does not define a CurrentContextIsValid function""" 
  218.         )
  219.     def getGLUTFontPointer(self, constant ):
  220.         """Retrieve a GLUT font pointer for this platform"""
  221.         raise NotImplementedError( 
  222.             """Platform does not define a GLUT font retrieval function""" 
  223.         )
  224.     def safeGetError( self ):
  225.         """Safety-checked version of glError() call (checks for valid context first)"""
  226.         raise NotImplementedError( 
  227.             """Platform does not define a safeGetError function""" 
  228.         )
  229.  
  230. class _NullFunctionPointer( object ):
  231.     """Function-pointer-like object for undefined functions"""
  232.     def __init__( self, name, dll, resultType, argTypes, argNames, extension=None, doc=None ):
  233.         from OpenGL import error
  234.         self.__name__ = name
  235.         self.DLL = dll
  236.         self.argNames = argNames
  237.         self.argtypes = argTypes
  238.         if top_level_module.ERROR_CHECKING:
  239.             self.errcheck = error.glCheckError
  240.         else:
  241.             self.errcheck = None
  242.         self.restype = resultType
  243.         self.extension = extension
  244.         self.doc = doc
  245.     resolved = False
  246.     def __nonzero__( self ):
  247.         """Make this object appear to be NULL"""
  248.         if self.extension and not self.resolved:
  249.             self.load()
  250.         return self.resolved
  251.     def load( self ):
  252.         """Attempt to load the function again, presumably with a context this time"""
  253.         from OpenGL import platform
  254.         if not platform.PLATFORM.checkExtension( self.extension ):
  255.             return None
  256.         try:
  257.             func = platform.PLATFORM.constructFunction(
  258.                 self.__name__, self.DLL, 
  259.                 resultType=self.restype, 
  260.                 argTypes=self.argtypes,
  261.                 doc = self.doc, 
  262.                 argNames = self.argNames,
  263.                 extension = self.extension,
  264.             )
  265.         except AttributeError, err:
  266.             return None 
  267.         else:
  268.             # now short-circuit so that we don't need to check again...
  269.             self.__class__.__call__ = staticmethod( func.__call__ )
  270.             self.resolved = True
  271.             return func
  272.         return None
  273.     def __call__( self, *args, **named ):
  274.         if self.load():
  275.             return self( *args, **named )
  276.         else:
  277.             from OpenGL import error
  278.             raise error.NullFunctionError(
  279.                 """Attempt to call an undefined function %s, check for bool(%s) before calling"""%(
  280.                     self.__name__, self.__name__,
  281.                 )
  282.             )
  283.