File: extensions.py

package info (click to toggle)
pyopengl 3.0.0~b6-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 5,696 kB
  • ctags: 26,182
  • sloc: python: 34,233; ansic: 70; sh: 26; makefile: 15
file content (70 lines) | stat: -rw-r--r-- 2,445 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Extension module support methods

This module provides the tools required to check whether
an extension is available
"""
VERSION_PREFIX = 'GL_VERSION_GL_'
CURRENT_GL_VERSION = ''
AVAILABLE_GL_EXTENSIONS = []
AVAILABLE_GLU_EXTENSIONS = []

def hasGLExtension( specifier ):
	"""Given a string specifier, check for extension being available"""
	global AVAILABLE_GL_EXTENSIONS, CURRENT_GL_VERSION
	specifier = specifier.replace('.','_')
	if specifier.startswith( VERSION_PREFIX ):
		from OpenGL.GL import glGetString, GL_VERSION
		if not CURRENT_GL_VERSION:
			new = glGetString( GL_VERSION )
			if new:
				CURRENT_GL_VERSION = [
					int(x) for x in new.split(' ',1)[0].split( '.' )
				]
			else:
				return False # not yet loaded/supported
		specifier = [
			int(x) 
			for x in specifier[ len(VERSION_PREFIX):].split('_')
		]
		return specifier <= CURRENT_GL_VERSION
	else:
		from OpenGL.GL import glGetString, GL_EXTENSIONS
		if not AVAILABLE_GL_EXTENSIONS:
			AVAILABLE_GL_EXTENSIONS[:] = glGetString( GL_EXTENSIONS ).split()
		return specifier in AVAILABLE_GL_EXTENSIONS

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( object ):
	resolved = False 
	implementation = None
	def __init__( self, name, *alternates ):
		"""Initialize set of alternative implementations of the same function"""
		self.__name__ = name
		self._alternatives = alternates
	def __nonzero__( self ):
		for alternate in self._alternatives:
			if alternate:
				return True 
		return False
	def __call__( self, *args, **named ):
		"""Call, doing a late lookup and bind to find an implementation"""
		for alternate in self._alternatives:
			if alternate:
				self.__class__.__call__ = alternate.__call__
				return self( *args, **named )
		from OpenGL import error
		raise error.NullFunctionError(
			"""Attempt to call an undefined alterate 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"""
	return type( name, (_Alternate,), {} )( name, *functions )