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 / arrays / strings.py < prev    next >
Encoding:
Python Source  |  2008-12-20  |  2.4 KB  |  59 lines

  1. """String-array-handling code for PyOpenGL
  2. """
  3. from OpenGL.arrays._strings import dataPointer
  4. from OpenGL import constants
  5. from OpenGL.arrays import formathandler
  6. import ctypes
  7.  
  8. class StringHandler( formathandler.FormatHandler ):
  9.     """String-specific data-type handler for OpenGL"""
  10.     HANDLED_TYPES = (str, )
  11.     @classmethod
  12.     def from_param( cls, value, typeCode=None ):
  13.         return dataPointer( value )
  14.     dataPointer = staticmethod( dataPointer )
  15.     def zeros( self, dims, typeCode=None ):
  16.         """Currently don't allow strings as output types!"""
  17.         raise NotImplemented( """Don't currently support strings as output arrays""" )
  18.     def ones( self, dims, typeCode=None ):
  19.         """Currently don't allow strings as output types!"""
  20.         raise NotImplemented( """Don't currently support strings as output arrays""" )
  21.     def arrayToGLType( self, value ):
  22.         """Given a value, guess OpenGL type of the corresponding pointer"""
  23.         raise NotImplemented( """Can't guess data-type from a string-type argument""" )
  24.     def arraySize( self, value, typeCode = None ):
  25.         """Given a data-value, calculate ravelled size for the array"""
  26.         # need to get bits-per-element...
  27.         byteCount = BYTE_SIZES[ typeCode ]
  28.         return len(value)//byteCount
  29.     def arrayByteCount( self, value, typeCode = None ):
  30.         """Given a data-value, calculate number of bytes required to represent"""
  31.         return len(value)
  32.     def asArray( self, value, typeCode=None ):
  33.         """Convert given value to an array value of given typeCode"""
  34.         if isinstance( value, str ):
  35.             return value
  36.         elif hasattr( value, 'tostring' ):
  37.             return value.tostring()
  38.         elif hasattr( value, 'raw' ):
  39.             return value.raw
  40.         # could convert types to string here, but we're not registered for 
  41.         # anything save string types...
  42.         raise TypeError( """String handler got non-string object: %r"""%(type(value)))
  43.     def dimensions( self, value, typeCode=None ):
  44.         """Determine dimensions of the passed array value (if possible)"""
  45.         raise TypeError(
  46.             """Cannot calculate dimensions for a String data-type"""
  47.         )
  48.  
  49. BYTE_SIZES = {
  50.     constants.GL_DOUBLE: ctypes.sizeof( constants.GLdouble ),
  51.     constants.GL_FLOAT: ctypes.sizeof( constants.GLfloat ),
  52.     constants.GL_INT: ctypes.sizeof( constants.GLint ),
  53.     constants.GL_SHORT: ctypes.sizeof( constants.GLshort ),
  54.     constants.GL_UNSIGNED_BYTE: ctypes.sizeof( constants.GLubyte ),
  55.     constants.GL_UNSIGNED_SHORT: ctypes.sizeof( constants.GLshort ),
  56.     constants.GL_BYTE: ctypes.sizeof( constants.GLbyte ),
  57.     constants.GL_UNSIGNED_INT: ctypes.sizeof( constants.GLuint ),
  58. }
  59.