gles_util.cpp

来自「python s60 1.4.5版本的源代码」· C++ 代码 · 共 953 行 · 第 1/2 页

CPP
953
字号
  for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyFloat_Check(item) ) {
      PyErr_SetString(PyExc_TypeError, "Expecting a float");
      return NULL;
    }
    dataptr[i] = (GLfloat)PyFloat_AsDouble(item);
	}
  
  return dataptr;
}

/**
 * Convert a Python sequence into unsigned short array
 *
 * @param seq A Python sequence object
 * @return A pointer to a C array of GLushort values
 */
GLushort *gles_PySequence_AsGLushortArray(PyObject *seq) {
  int count;
  GLushort *dataptr;
  int i;
  PyObject *item;
  
  if(! PySequence_Check(seq)) {
    PyErr_SetString(PyExc_TypeError, "Expecting a sequence");
    return NULL;
  }
  
  count = PySequence_Length(seq);
  dataptr = (GLushort*)gles_alloc( sizeof(GLushort)*count);
  if(dataptr == NULL) {
    return (GLushort*)PyErr_NoMemory();
  }
  
  for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyInt_Check(item) ) {
      gles_free(dataptr);
      PyErr_SetString(PyExc_TypeError, "Expecting an integer");
      return NULL;
    }
    dataptr[i] = (GLushort)PyInt_AsLong(item);
	}
  
  return dataptr;
}

/**
 * Convert a Python sequence into short array
 *
 * @param seq A Python sequence object
 * @return A pointer to a C array of GLshort values
 */
GLshort *gles_PySequence_AsGLshortArray(PyObject *seq) {
  int count;
  GLshort *dataptr;
  int i;
  PyObject *item;
  
  if(! PySequence_Check(seq)) {
    PyErr_SetString(PyExc_TypeError, "Expecting a sequence");
    return NULL;
  }
  
  count = PySequence_Length(seq);
  dataptr = (GLshort*)gles_alloc( sizeof(GLshort)*count);
  if(dataptr == NULL) {
    return (GLshort*)PyErr_NoMemory();
  }
  
  for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyInt_Check(item) ) {
      gles_free(dataptr);
      PyErr_SetString(PyExc_TypeError, "Expecting an integer");
      return NULL;
    }
    dataptr[i] = (GLshort)PyInt_AsLong(item);
	}
  
  return dataptr;
}

/**
 * Convert a Python sequence into a byte array
 *
 * @param seq A Python sequence object
 * @return A pointer to a C array of GLbyte values
 */
GLbyte *gles_PySequence_AsGLbyteArray(PyObject *seq) {
  int count;
  GLbyte *dataptr;
  int i;
  PyObject *item;
  
  if(! PySequence_Check(seq)) {
    PyErr_SetString(PyExc_TypeError, "Invalid type");
    return NULL;
  }
  
  count = PySequence_Length(seq);
  dataptr = (GLbyte*)gles_alloc( sizeof(GLbyte)*count);
  if(dataptr == NULL) {
    return (GLbyte*)PyErr_NoMemory();
  }
  
  for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyInt_Check(item) ) {
      // Free the indices.
      gles_free(dataptr);
      PyErr_SetString(PyExc_TypeError, "Expecting an integer");
      return NULL;
    }
    dataptr[i] = (GLbyte)PyInt_AsLong(item);
	}
  
  return dataptr;
}

/**
 * Converts a Python sequence into unsigned byte array
 *
 * @param seq A Python sequence object
 * @return A pointer to a C array of GLubyte values
 */
GLubyte *gles_PySequence_AsGLubyteArray(PyObject *seq) {
  int count;
  GLubyte *dataptr;
  int i;
  PyObject *item;
  
  if(! PySequence_Check(seq)) {
    PyErr_SetString(PyExc_TypeError, "Invalid type");
    return NULL;
  }
  
  count = PySequence_Length(seq);
  dataptr = (GLubyte*)gles_alloc( sizeof(GLubyte)*count);
  if(dataptr == NULL) {
    return (GLubyte*)PyErr_NoMemory();
  }
  
    for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyInt_Check(item) ) {
      // Free the indices
      gles_free(dataptr);
      PyErr_SetString(PyExc_TypeError, "Expecting an integer");
      return NULL;
    }
    dataptr[i] = (GLubyte)PyInt_AsLong(item);
	}
  
  return dataptr;
}

/**
 * Converts a Python sequence into int array
 *
 * @param seq A Python sequence object
 * @return A pointer to a C array of GLint values
 */
GLint *gles_PySequence_AsGLintArray(PyObject *seq) {
  int count;
  GLint *dataptr;
  int i;
  PyObject *item;
  
  if(! PySequence_Check(seq)) {
    PyErr_SetString(PyExc_TypeError, "Invalid type");
    return NULL;
  }
  
  count = PySequence_Length(seq);
  dataptr = (GLint*)gles_alloc( sizeof(GLint)*count);
  if(dataptr == NULL) {
    return (GLint*)PyErr_NoMemory();
  }
  
  for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyInt_Check(item) ) {
      // Free the indices
      gles_free(dataptr);
      PyErr_SetString(PyExc_TypeError, "Expecting an integer");
      return NULL;
    }
    dataptr[i] = (GLint)PyInt_AsLong(item);
	}
  
  return dataptr;
}

/**
 * Converts a Python sequence into unsigned int array
 *
 * @param seq A Python sequence object
 * @return A pointer to a C array of GLuint values
 */
GLuint *gles_PySequence_AsGLuintArray(PyObject *seq) {
  int count;
  GLuint *dataptr;
  int i;
  PyObject *item;
  
  if(! PySequence_Check(seq)) {
    PyErr_SetString(PyExc_TypeError, "Invalid type");
    return NULL;
  }
  
  count = PySequence_Length(seq);
  dataptr = (GLuint*)gles_alloc( sizeof(GLuint)*count);
  if(dataptr == NULL) {
    return (GLuint*)PyErr_NoMemory();
  }
  
    for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyInt_Check(item) ) {
      // Free the indices
      gles_free(dataptr);
      PyErr_SetString(PyExc_TypeError, "Expecting an integer");
      return NULL;
    }
    dataptr[i] = (GLuint)PyInt_AsLong(item);
	}
  
  return dataptr;
}

/**
 * Converts a Python sequence into fixed array
 *
 * @param seq A Python sequence object
 * @return A pointer to a C array of GLfixed values
 */
GLfixed *gles_PySequence_AsGLfixedArray(PyObject *seq) {
  int count;
  GLfixed *dataptr;
  int i;
  PyObject *item;
  
  if(! PySequence_Check(seq)) {
    PyErr_SetString(PyExc_TypeError, "Invalid type");
    return NULL;
  }
  
  count = PySequence_Length(seq);
  dataptr = (GLfixed*)gles_alloc( sizeof(GLfixed)*count);
  if(dataptr == NULL) {
    return (GLfixed*)PyErr_NoMemory();
  }
  
    for( i = 0; i < count; i++ ) {
		item = PySequence_GetItem(seq, i);
    if( !PyInt_Check(item) ) {
      // Free the indices
      gles_free(dataptr);
      PyErr_SetString(PyExc_TypeError, "Expecting an integer");
      return NULL;
    }
    dataptr[i] = (GLfixed)PyInt_AsLong(item);
	}
  
  return dataptr;
}

/**
 * Frees all the internally used array memory
 *
 */
void gles_uninit_arrays() {
    GLES_Arrays *arrayData;
    arrayData = (GLES_Arrays*)Dll::Tls();
    if(arrayData != NULL) {
      gles_free_array(GL_VERTEX_ARRAY);
      gles_free_array(GL_NORMAL_ARRAY);
      gles_free_array(GL_COLOR_ARRAY);
      gles_free_array(GL_TEXTURE_COORD_ARRAY);
#ifdef GL_OES_VERSION_1_1
      gles_free_array(GL_MATRIX_INDEX_ARRAY_OES);
      gles_free_array(GL_POINT_SIZE_ARRAY_OES);
      gles_free_array(GL_WEIGHT_ARRAY_OES);
#endif
      gles_free(arrayData);
    }
    Dll::FreeTls();
}

/**
 * Initializes internal arrays used for glXXXPointer data
 *
 */
void gles_init_arrays() {
    GLES_Arrays *arrayData;
    
    arrayData = (GLES_Arrays*)gles_alloc( sizeof(GLES_Arrays) );
    if(arrayData == NULL) {
      PyErr_NoMemory();
      return;
    }
    
    // Reset array pointers
    arrayData->vertex.ptr = NULL;
    arrayData->vertex.obj = NULL;
    
    arrayData->color.ptr = NULL;
    arrayData->color.obj = NULL;
    
    arrayData->normal.ptr = NULL;
    arrayData->normal.obj = NULL;
    
    arrayData->texcoord.ptr = NULL;
    arrayData->texcoord.obj = NULL;
    
#ifdef GL_OES_VERSION_1_1
    arrayData->matrix.ptr = NULL;
    arrayData->matrix.obj = NULL;
    
    arrayData->pointsize.ptr = NULL;
    arrayData->pointsize.obj = NULL;
    
    arrayData->weight.ptr = NULL;
    arrayData->weight.obj = NULL;
#endif
    Dll::SetTls(arrayData);
}

/**
 * Frees a specified array from internal memory
 *
 * @param arrtype Which array to free. Can be one of: GL_VERTEX_ARRAY, GL_NORMAL_ARRAY, GL_COLOR_ARRAY or GL_TEXTURE_COORD_ARRAY.
*/
void gles_free_array(GLenum arrtype) {
  GLES_Arrays *arrays=(GLES_Arrays*)Dll::Tls();
  gles_array_t *arr=NULL;
  
  switch(arrtype) {
    case GL_VERTEX_ARRAY:
      arr = &(arrays->vertex);
#ifdef DEBUG_GLES
      DEBUGMSG("gles_free_array() (VERTEX)");
#endif
      break;
    case GL_NORMAL_ARRAY:
      arr = &(arrays->normal);
#ifdef DEBUG_GLES
      DEBUGMSG("gles_free_array() (NORMAL)");
#endif
      break;
    case GL_COLOR_ARRAY:
      arr = &(arrays->color);
#ifdef DEBUG_GLES
      DEBUGMSG("gles_free_array() (COLOR)");
#endif
      break;
    case GL_TEXTURE_COORD_ARRAY:
      arr = &(arrays->texcoord);
#ifdef DEBUG_GLES
      DEBUGMSG("gles_free_array() (TEXTURE_COORD)");
#endif
      break;
#ifdef GL_OES_VERSION_1_1
    case GL_MATRIX_INDEX_ARRAY_OES:
      arr = &(arrays->matrix);
#ifdef DEBUG_GLES
      DEBUGMSG("gles_free_array() (GL_MATRIX_INDEX_ARRAY_OES)");
#endif
      break;
    case GL_POINT_SIZE_ARRAY_OES:
      arr = &(arrays->pointsize);
#ifdef DEBUG_GLES
      DEBUGMSG("gles_free_array() (GL_POINT_SIZE_ARRAY_OES)");
#endif
      break;
    case GL_WEIGHT_ARRAY_OES:
      arr = &(arrays->weight);
#ifdef DEBUG_GLES
      DEBUGMSG("gles_free_array() (GL_WEIGHT_ARRAY_OES)");
#endif
      break;
#endif
    default:
      return;
  }
  
  if(arr->obj != NULL) {
    Py_XDECREF(arr->obj);
  } else if(arr->ptr != NULL) {
    gles_free(arr->ptr);
  }
  arr->ptr=NULL;
}

/**
 * Checks if an OpenGL error has occurred, and sets an exception if necessary.
 *
 * @return 1 if an OpenGL error has occurred, 0 otherwise
 */
unsigned int gles_check_error() {
  GLenum error = GL_NO_ERROR;
  
  error = glGetError();
  if(error != GL_NO_ERROR) {
    PyObject *exc;
    exc = PyErr_NewException("gles.GLerror", NULL, NULL);
    switch(error) {
      case GL_INVALID_ENUM:
        PyErr_Format(exc, "Invalid enum [Errno %x]", error);
#ifdef DEBUG_GLES
        DEBUGMSG1("gles_check_error(): Invalid enum [Errno %x]", error);
#endif
        break;
      case GL_INVALID_VALUE:
        PyErr_Format(exc, "Invalid value [Errno %x]", error);
#ifdef DEBUG_GLES
        DEBUGMSG1("gles_check_error(): Invalid value [Errno %x]", error);
#endif
        break;
      case GL_INVALID_OPERATION:
        PyErr_Format(exc, "Invalid operation [Errno %x]", error);
#ifdef DEBUG_GLES
        DEBUGMSG1("gles_check_error(): Invalid operation [Errno %x]", error);
#endif
        break;
      case GL_STACK_OVERFLOW:
        PyErr_Format(exc, "Stack overflow [Errno %x]", error);
#ifdef DEBUG_GLES
        DEBUGMSG1("gles_check_error(): Stack overflow [Errno %x]", error);
#endif
        break;
      case GL_STACK_UNDERFLOW:
        PyErr_Format(exc, "Stack underflow [Errno %x]", error);
#ifdef DEBUG_GLES
        DEBUGMSG1("gles_check_error(): Stack underflow [Errno %x]", error);
#endif
        break;
      case GL_OUT_OF_MEMORY:
        PyErr_NoMemory();
#ifdef DEBUG_GLES
        DEBUGMSG("gles_check_error(): Out of memory");
#endif
        break;
      default:
        PyErr_Format(PyExc_Exception, "Unknown error: %x", error );
#ifdef DEBUG_GLES
        DEBUGMSG1("gles_check_error(): Unknown error: %x", error);
#endif
        break;
    }
    return 1;
  }
  return 0;
}

/**
 * Helper function for allocating memory
 *
 * @param size Size of desired memory block
 * @return A pointer to a newly allocated block of memory
 */
void *gles_alloc(size_t size) {
  return User::Alloc(size);
}

/**
 * Helper function for freeing allocated memory
 *
 * @param ptr A pointer to a block of memory to be freed
 */
void gles_free(void *ptr) {
  User::Free(ptr);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?