📄 glesmodule.cpp
字号:
/**
* ====================================================================
* glesmodule.cpp
*
* Copyright (c) 2006 Nokia Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "glesmodule.h"
extern "C" PyObject *new_array_object(PyObject */*self*/, PyObject *args)
{
array_object *op = NULL;
PyObject *sequence;
void *arrdata = NULL;
int type;
int dimension;
#ifdef DEBUG_GLES
DEBUGMSG("new_array_object()");
#endif
if (!PyArg_ParseTuple(args, "iiO:array", &type, &dimension, &sequence)) {
return NULL;
}
if (!PySequence_Check(sequence) ) {
PyErr_SetString(PyExc_TypeError, "Expecting a sequence");
return NULL;
}
op = PyObject_GC_New(array_object, GLES_ARRAY_TYPE);
if (!op) {
return PyErr_NoMemory();
}
sequence = gles_PySequence_Collapse(type, sequence, NULL);
if(sequence==NULL) {
// There should be an exception
assert(PyErr_Occurred() != NULL);
// Delete the object since we're not going to use it anymore anyways
PyObject_Del(op);
return NULL;
}
switch(type) {
case GL_FLOAT:
arrdata = gles_PySequence_AsGLfloatArray(sequence);
op->item_size = sizeof(GLfloat);
break;
case GL_BYTE:
arrdata = gles_PySequence_AsGLbyteArray(sequence);
op->item_size = sizeof(GLbyte);
break;
case GL_UNSIGNED_BYTE:
arrdata = gles_PySequence_AsGLubyteArray(sequence);
op->item_size = sizeof(GLubyte);
break;
case GL_SHORT:
arrdata = gles_PySequence_AsGLshortArray(sequence);
op->item_size = sizeof(GLshort);
break;
case GL_UNSIGNED_SHORT:
arrdata = gles_PySequence_AsGLushortArray(sequence);
op->item_size = sizeof(GLushort);
break;
case GL_FIXED:
arrdata = gles_PySequence_AsGLfixedArray(sequence);
op->item_size = sizeof(GLfixed);
break;
default:
PyErr_SetString(PyExc_ValueError, "Invalid array type specified");
return NULL;
}
// We should have an exception from the previous functions if arrdata==NULL
if(arrdata == NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
op->arrdata = arrdata;
op->len = PySequence_Length(sequence);
// Calculate the length in memory
op->real_len = op->item_size * op->len;
op->arrtype = type;
op->dimension = dimension;
if (!op->arrdata) {
PyObject_Del(op);
return PyErr_NoMemory();
}
_PyObject_GC_TRACK(op);
return (PyObject*)op;
}
extern "C" int array_length(PyObject *self) {
array_object *op = (array_object*)self;
return op->len;
}
extern "C" PyObject *array_getitem(PyObject *self, int idx) {
array_object *op = (array_object*)self;
PyObject *ret = NULL;
if(idx > op->len-1 ) {
PyErr_SetString(PyExc_IndexError, "list index out of range");
return NULL;
}
switch(op->arrtype) {
case GL_FLOAT:
ret = PyFloat_FromDouble(*((GLfloat*)op->arrdata+idx));
break;
case GL_BYTE:
ret = PyInt_FromLong(*((GLbyte*)op->arrdata+idx));
break;
case GL_UNSIGNED_BYTE:
/// Python 2.2.2 does not have PyInt_FromUnsignedLong()
ret = PyLong_FromUnsignedLong(*((GLubyte*)op->arrdata+idx));
break;
case GL_SHORT:
ret = PyInt_FromLong(*((GLshort*)op->arrdata+idx));
break;
case GL_UNSIGNED_SHORT:
ret = PyLong_FromUnsignedLong(*((GLushort*)op->arrdata+idx));
break;
case GL_FIXED:
ret = PyInt_FromLong(*((GLfixed*)op->arrdata+idx));
break;
default:
// This should never happen, but it's never too safe not to check
PyErr_SetString(PyExc_RuntimeError, "Unknown type of array");
return NULL;
}
return ret;
}
extern "C" int array_setitem(PyObject *self, int idx, PyObject *v) {
array_object *op = (array_object*)self;
if(idx > op->len-1 ) {
PyErr_SetString(PyExc_IndexError, "list index out of range");
return -1;
}
if(!PyNumber_Check(v)) {
PyErr_SetString(PyExc_TypeError, "Expecting a number");
return -1;
}
switch(op->arrtype) {
case GL_FLOAT:
*((GLfloat*)op->arrdata+idx) = (GLfloat)PyFloat_AsDouble(v);
break;
case GL_BYTE:
*((GLbyte*)op->arrdata+idx) = (GLbyte)PyInt_AsLong(v);
break;
case GL_UNSIGNED_BYTE:
*((GLubyte*)op->arrdata+idx) = (GLubyte)PyInt_AsLong(v);
break;
case GL_SHORT:
*((GLshort*)op->arrdata+idx) = (GLshort)PyInt_AsLong(v);
break;
case GL_UNSIGNED_SHORT:
*((GLushort*)op->arrdata+idx) = (GLushort)PyInt_AsLong(v);
break;
case GL_FIXED:
*((GLfixed*)op->arrdata+idx) = (GLfixed)PyInt_AsLong(v);
break;
default:
// This should never happen, but it's never too safe not to check
PyErr_SetString(PyExc_RuntimeError, "Unknown type of array");
return 0;
}
return 0;
}
extern "C" {
static void
array_dealloc(array_object *op)
{
#ifdef DEBUG_GLES
DEBUGMSG("array_dealloc\n");
#endif
if(op->arrdata) {
gles_free(op->arrdata);
}
//PyObject_GC_UnTrack((PyObject*)op);
PyObject_GC_Del(op);
}
const static PyMethodDef array_methods[] = {
{NULL, NULL} // sentinel
};
const static PySequenceMethods array_as_sequence = {
(inquiry)array_length, /* sq_length */
/*(binaryfunc)*/0, /* sq_concat */
/*(intargfunc)*/0, /* sq_repeat */
(intargfunc)array_getitem, /* sq_item */
/*(intintargfunc)*/0, /* sq_slice */
(intobjargproc)array_setitem, /* sq_ass_item */
0, /* sq_ass_slice */
/*(objobjproc)*/0, /* sq_contains */
};
static PyObject *
array_getattr(array_object *op, char *name)
{
PyObject *ret;
if (!strcmp(name,"len")) {
ret = PyInt_FromLong(op->len);
return ret;
}
return Py_FindMethod((PyMethodDef*)array_methods,
(PyObject*)op, name);
}
static int
array_setattr(array_object */*op*/, char */*name*/, PyObject */*v*/ )
{
PyErr_SetString(PyExc_AttributeError, "no such attribute");
return -1;
}
static const PyTypeObject c_array_type = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"GLESArray", /*tp_name*/
sizeof(array_object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)array_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)array_getattr, /*tp_getattr*/
(setattrfunc)array_setattr, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
&array_as_sequence, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
};
} // extern "C"
/***********************************************
* The bindings code starts here *
***********************************************/
extern "C" PyObject *gles_glActiveTexture(PyObject *self, PyObject *args) {
GLenum texture;
if( !PyArg_ParseTuple(args, "i:glActiveTexture", &texture) ) {
return NULL;
}
glActiveTexture(texture);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glAlphaFunc(PyObject *self, PyObject *args) {
GLenum func;
GLclampf ref;
if( !PyArg_ParseTuple(args, "if:glAlphaFunc", &func, &ref) ) {
return NULL;
}
glAlphaFunc(func, ref);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glAlphaFuncx(PyObject *self, PyObject *args) {
GLenum func;
GLclampx ref;
if( !PyArg_ParseTuple(args, "ii:glAlphaFuncx", &func, &ref) ) {
return NULL;
}
glAlphaFuncx(func, ref);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glBindTexture(PyObject *self, PyObject *args) {
GLenum target;
GLuint texture;
if( !PyArg_ParseTuple(args, "ii:glBindTexture", &target, &texture)) {
return NULL;
}
glBindTexture(target, texture);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glBlendFunc(PyObject *self, PyObject *args) {
GLenum sfactor;
GLenum dfactor;
if( !PyArg_ParseTuple(args, "ii:glBlendFunc", &sfactor, &dfactor) ) {
return NULL;
}
glBlendFunc(sfactor, dfactor);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glClear (PyObject *self, PyObject *args) {
unsigned int mask;
if ( !PyArg_ParseTuple(args, "i:glClear", &mask) ) {
return NULL;
}
glClear(mask);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glClearColor (PyObject *self, PyObject *args) {
GLclampf red;
GLclampf green;
GLclampf blue;
GLclampf alpha;
if ( !PyArg_ParseTuple(args, "ffff:glClearColor", &red, &green, &blue, &alpha ) ) {
return NULL;
}
glClearColor(red, green, blue, alpha);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glClearColorx(PyObject *self, PyObject *args) {
GLclampx red;
GLclampx green;
GLclampx blue;
GLclampx alpha;
if ( !PyArg_ParseTuple(args, "iiii:glClearColorx", &red, &green, &blue, &alpha ) ) {
return NULL;
}
glClearColorx(red, green, blue, alpha);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glClearDepthf(PyObject *self, PyObject *args) {
GLclampf depth;
if ( !PyArg_ParseTuple(args, "f:glClearDepthf", &depth) ) {
return NULL;
}
glClearDepthf(depth);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glClearDepthx(PyObject *self, PyObject *args) {
GLclampx depth;
if ( !PyArg_ParseTuple(args, "i:glClearDepthx", &depth) ) {
return NULL;
}
glClearDepthx(depth);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glClearStencil(PyObject *self, PyObject *args) {
GLint s;
if ( !PyArg_ParseTuple(args, "i:glClearStencil", &s) ) {
return NULL;
}
glClearStencil(s);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glClientActiveTexture(PyObject *self, PyObject *args) {
GLenum texture;
if ( !PyArg_ParseTuple(args, "i:glClientActiveTexture", &texture) ) {
return NULL;
}
glClientActiveTexture(texture);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glColor4f(PyObject *self, PyObject *args) {
GLfloat red;
GLfloat green;
GLfloat blue;
GLfloat alpha;
if ( !PyArg_ParseTuple(args, "ffff:glColor4f", &red, &green, &blue, &alpha) ) {
return NULL;
}
glColor4f(red, green, blue, alpha);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glColor4x(PyObject *self, PyObject *args) {
GLfixed red;
GLfixed green;
GLfixed blue;
GLfixed alpha;
if ( !PyArg_ParseTuple(args, "iiii:glColor4x", &red, &green, &blue, &alpha) ) {
return NULL;
}
glColor4x(red, green, blue, alpha);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
extern "C" PyObject *gles_glColorMask(PyObject *self, PyObject *args) {
GLboolean red;
GLboolean green;
GLboolean blue;
GLboolean alpha;
if ( !PyArg_ParseTuple(args, "iiii:glColorMask", &red, &green, &blue, &alpha) ) {
return NULL;
}
glColorMask(red, green, blue, alpha);
RETURN_IF_GLERROR;
RETURN_PYNONE;
}
PyObject *gles_wrap_glColorPointer(GLint size, GLenum type, GLsizei stride, PyObject *pointerPy) {
void *cptr = NULL;
array_object *arrobj=NULL;
// We can use either the array type or a sequence
// Check the array type first
if(PyObject_TypeCheck(pointerPy, GLES_ARRAY_TYPE) ) {
arrobj = (array_object*)pointerPy;
cptr = arrobj->arrdata;
}
#ifdef GL_OES_VERSION_1_1
else if(pointerPy == Py_None) {
cptr = NULL;
}
#endif
// Try to convert it as a sequence
else {
pointerPy = gles_PySequence_Collapse(type, pointerPy, NULL);
if(pointerPy == NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
// glColorPointer accepts only GL_UNSIGNED_BYTE, GL_FLOAT or GL_FIXED types
switch(type) {
case GL_UNSIGNED_BYTE:
cptr = gles_PySequence_AsGLubyteArray(pointerPy);
break;
case GL_FLOAT:
cptr = gles_PySequence_AsGLfloatArray(pointerPy);
break;
case GL_FIXED:
cptr = gles_PySequence_AsGLfixedArray(pointerPy);
break;
default:
PyErr_SetString(PyExc_ValueError, "Unsupported array type");
return NULL;
}
if(cptr == NULL) {
assert(PyErr_Occurred() != NULL);
return NULL;
}
}
#ifndef GL_OES_VERSION_1_1
if(cptr == NULL) {
// Something went horribly wrong
// However, we should have an exception already
assert(PyErr_Occurred() != NULL);
return NULL;
}
#endif
gles_assign_array(GL_COLOR_ARRAY, cptr, arrobj);
glColorPointer(size, type, stride, cptr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -