From fd2b1156783d52dbb7c93c53fe008d9e14cbffdd Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 3 Apr 2009 14:51:06 +0000 Subject: Python BGE API - Initialize python types with PyType_Ready, which adds methods to the type dictionary. - use Pythons get/setattro (uses a python string for the attribute rather then char*). Using basic C strings seems nice but internally python converts them to python strings and discards them for most functions that accept char arrays. - Method lookups use the PyTypes dictionary (should be faster then Py_FindMethod) - Renamed __getattr -> py_base_getattro, _getattr -> py_getattro, __repr -> py_base_repr, py_delattro, py_getattro_self etc. From here is possible to put all the parent classes methods into each python types dictionary to avoid nested lookups (api has 4 levels of lookups in some places), tested this but its not ready yet. Simple tests for getting a method within a loop show this to be between 0.5 and 3.2x faster then using Py_FindMethod() --- source/gameengine/Ketsji/KX_Camera.cpp | 60 +++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 27 deletions(-) (limited to 'source/gameengine/Ketsji/KX_Camera.cpp') diff --git a/source/gameengine/Ketsji/KX_Camera.cpp b/source/gameengine/Ketsji/KX_Camera.cpp index cb138efba89..19370d83322 100644 --- a/source/gameengine/Ketsji/KX_Camera.cpp +++ b/source/gameengine/Ketsji/KX_Camera.cpp @@ -488,18 +488,21 @@ PyAttributeDef KX_Camera::Attributes[] = { }; PyTypeObject KX_Camera::Type = { - PyObject_HEAD_INIT(&PyType_Type) + PyObject_HEAD_INIT(NULL) 0, "KX_Camera", sizeof(KX_Camera), 0, PyDestructor, 0, - __getattr, - __setattr, 0, - __repr, - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0, + 0, + py_base_repr, + 0,0,0,0,0,0, + py_base_getattro, + py_base_setattro, + 0,0,0,0,0,0,0,0,0, Methods }; @@ -511,48 +514,51 @@ PyParentObject KX_Camera::Parents[] = { NULL }; -PyObject* KX_Camera::_getattr(const char *attr) +PyObject* KX_Camera::py_getattro(PyObject *attr) { - if (!strcmp(attr, "INSIDE")) + char *attr_str= PyString_AsString(attr); + if (!strcmp(attr_str, "INSIDE")) return PyInt_FromLong(INSIDE); /* new ref */ - if (!strcmp(attr, "OUTSIDE")) + if (!strcmp(attr_str, "OUTSIDE")) return PyInt_FromLong(OUTSIDE); /* new ref */ - if (!strcmp(attr, "INTERSECT")) + if (!strcmp(attr_str, "INTERSECT")) return PyInt_FromLong(INTERSECT); /* new ref */ - if (!strcmp(attr, "lens")) + if (!strcmp(attr_str, "lens")) return PyFloat_FromDouble(GetLens()); /* new ref */ - if (!strcmp(attr, "near")) + if (!strcmp(attr_str, "near")) return PyFloat_FromDouble(GetCameraNear()); /* new ref */ - if (!strcmp(attr, "far")) + if (!strcmp(attr_str, "far")) return PyFloat_FromDouble(GetCameraFar()); /* new ref */ - if (!strcmp(attr, "frustum_culling")) + if (!strcmp(attr_str, "frustum_culling")) return PyInt_FromLong(m_frustum_culling); /* new ref */ - if (!strcmp(attr, "perspective")) + if (!strcmp(attr_str, "perspective")) return PyInt_FromLong(m_camdata.m_perspective); /* new ref */ - if (!strcmp(attr, "projection_matrix")) + if (!strcmp(attr_str, "projection_matrix")) return PyObjectFrom(GetProjectionMatrix()); /* new ref */ - if (!strcmp(attr, "modelview_matrix")) + if (!strcmp(attr_str, "modelview_matrix")) return PyObjectFrom(GetModelviewMatrix()); /* new ref */ - if (!strcmp(attr, "camera_to_world")) + if (!strcmp(attr_str, "camera_to_world")) return PyObjectFrom(GetCameraToWorld()); /* new ref */ - if (!strcmp(attr, "world_to_camera")) + if (!strcmp(attr_str, "world_to_camera")) return PyObjectFrom(GetWorldToCamera()); /* new ref */ - _getattr_up(KX_GameObject); + py_getattro_up(KX_GameObject); } -int KX_Camera::_setattr(const char *attr, PyObject *pyvalue) +int KX_Camera::py_setattro(PyObject *attr, PyObject *pyvalue) { + char *attr_str= PyString_AsString(attr); + if (PyInt_Check(pyvalue)) { - if (!strcmp(attr, "frustum_culling")) + if (!strcmp(attr_str, "frustum_culling")) { m_frustum_culling = PyInt_AsLong(pyvalue); return 0; } - if (!strcmp(attr, "perspective")) + if (!strcmp(attr_str, "perspective")) { m_camdata.m_perspective = PyInt_AsLong(pyvalue); return 0; @@ -561,19 +567,19 @@ int KX_Camera::_setattr(const char *attr, PyObject *pyvalue) if (PyFloat_Check(pyvalue)) { - if (!strcmp(attr, "lens")) + if (!strcmp(attr_str, "lens")) { m_camdata.m_lens = PyFloat_AsDouble(pyvalue); m_set_projection_matrix = false; return 0; } - if (!strcmp(attr, "near")) + if (!strcmp(attr_str, "near")) { m_camdata.m_clipstart = PyFloat_AsDouble(pyvalue); m_set_projection_matrix = false; return 0; } - if (!strcmp(attr, "far")) + if (!strcmp(attr_str, "far")) { m_camdata.m_clipend = PyFloat_AsDouble(pyvalue); m_set_projection_matrix = false; @@ -583,7 +589,7 @@ int KX_Camera::_setattr(const char *attr, PyObject *pyvalue) if (PyObject_IsMT_Matrix(pyvalue, 4)) { - if (!strcmp(attr, "projection_matrix")) + if (!strcmp(attr_str, "projection_matrix")) { MT_Matrix4x4 mat; if (PyMatTo(pyvalue, mat)) @@ -594,7 +600,7 @@ int KX_Camera::_setattr(const char *attr, PyObject *pyvalue) return 1; } } - return KX_GameObject::_setattr(attr, pyvalue); + return KX_GameObject::py_setattro(attr, pyvalue); } KX_PYMETHODDEF_DOC_VARARGS(KX_Camera, sphereInsideFrustum, -- cgit v1.2.3