extensions.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-3.0.1 » OpenGL » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PyOpenGL 
PyOpenGL » PyOpenGL 3.0.1 » OpenGL » extensions.py
"""Extension module support methods

This module provides the tools required to check whether
an extension is available
"""
from OpenGL.latebind import LateBind
import logging
log = logging.getLogger( 'OpenGL.extensions' )
VERSION_PREFIX = 'GL_VERSION_GL_'
CURRENT_GL_VERSION = None
AVAILABLE_GL_EXTENSIONS = []
AVAILABLE_GLU_EXTENSIONS = []

def getGLVersion( ):
    """Retrieve 2-int declaration of major/minor GL version

    returns [int(major),int(minor)] or False if not loaded
    """
    global CURRENT_GL_VERSION
    if not CURRENT_GL_VERSION:
        from OpenGL.GL import glGetString,GL_VERSION
        new = glGetString( GL_VERSION )
        log.info( 'OpenGL Version: %s', new )
        if new:
            CURRENT_GL_VERSION = [
                int(x) for x in new.split(' ',1)[0].split( '.' )
            ]
        else:
            return False # not yet loaded/supported
    return CURRENT_GL_VERSION


def hasGLExtension( specifier ):
    """Given a string specifier, check for extension being available"""
    global AVAILABLE_GL_EXTENSIONS
    specifier = specifier.replace('.','_')
    if specifier.startswith( VERSION_PREFIX ):
        specifier = [
            int(x)
            for x in specifier[ len(VERSION_PREFIX):].split('_')
        ]
        version = getGLVersion()
        if not version:
            return version
        return specifier <= version
    else:
        from OpenGL.GL import glGetString,GL_EXTENSIONS
        if not AVAILABLE_GL_EXTENSIONS:
            AVAILABLE_GL_EXTENSIONS[:] = glGetString( GL_EXTENSIONS ).split()
        result = specifier in AVAILABLE_GL_EXTENSIONS
        log.info(
            'GL Extension %s %s',
            specifier,
            ['unavailable','available'][bool(result)]
        )
        return result

def hasGLUExtension( specifier ):
    """Given a string specifier, check for extension being available"""
    from OpenGL.GLU import gluGetString,GLU_EXTENSIONS
    if not AVAILABLE_GLU_EXTENSIONS:
        AVAILABLE_GLU_EXTENSIONS[:] = gluGetString( GLU_EXTENSIONS )
    return specifier.replace('.','_') in AVAILABLE_GLU_EXTENSIONS

class _Alternate( LateBind ):
    def __init__( self, name, *alternates ):
        """Initialize set of alternative implementations of the same function"""
        self.__name__ = name
        self._alternatives = alternates
    def __nonzero__( self ):
        from OpenGL import error
        try:
            return bool( self.getFinalCall())
        except error.NullFunctionError, err:
            return False
    def finalise( self ):
        """Call, doing a late lookup and bind to find an implementation"""
        for alternate in self._alternatives:
            if alternate:
                log.info(
                    """Chose alternate: %s from %s""",
                    alternate.__name__,
                    ", ".join([x.__name__ for x in self._alternatives])
                )
                return alternate
        from OpenGL import error
        raise error.NullFunctionError(
            """Attempt to call an undefined alternate function (%s), check for bool(%s) before calling"""%(
                ', '.join([x.__name__ for x in self._alternatives]),
                self.__name__,
            )
        )
def alternate( name, *functions ):
    """Construct a callable that functions as the first implementation found of given set of alternatives

    if name is a function then its name will be used....
    """
    if not isinstance( name, (str,unicode)):
        functions = (name,)+functions
        name = name.__name__
    return type( name, (_Alternate,), {} )( name, *functions )
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.