Commits

Anonymous committed 748504c

Add basic type memory management

  • Participants
  • Parent commits e63a081
  • Branches physics

Comments (0)

Files changed (5)

 	switch(key)
 	{
 	case 27:
+		PG_WorldDestroy(s_world);
 		exit(0);
 		break;
 	}

File include/pgShapeObject.h

 	int (*IsPointIn)(pgShapeObject* shape, pgVector2* point);
 } pgShapeObject;
 
+
 void	PG_ShapeDestroy(pgShapeObject* shape);
 
 //subclass type

File src/pgBodyObject.c

 #include <structmember.h>
 
 
+
 void PG_FreeUpdateBodyVel(pgWorldObject* world,pgBodyObject* body, double dt)
 {
 	pgVector2 totalVelAdd;
 	body->shape = PG_RectShapeNew(body, 20, 20, 0);
 }
 
+PyObject* _PG_BodyNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+	//TODO: parse args later on
+	pgBodyObject* op;
+	if(PyType_Ready(type)==-1) return NULL;
+	op = (pgBodyObject*)type->tp_alloc(type, 0);
+	PG_BodyInit(op);
+	return (PyObject*)op;
+}
+
+void PG_BodyDestroy(pgBodyObject* body)
+{
+	/*
+	* DECREF anything related to the Body, such as the lists and
+	* release any other memory hold by it.
+	*/
+
+	//delete shape
+	PG_ShapeObjectDestroy(body->shape);
+	body->ob_type->tp_free((PyObject*)body);
+}
+
+static PyTypeObject pgBodyType =
+{
+	PyObject_HEAD_INIT(NULL)
+	0,
+	"physics.Body",            /* tp_name */
+	sizeof(pgBodyObject),      /* tp_basicsize */
+	0,                          /* tp_itemsize */
+	(destructor)PG_BodyDestroy,/* tp_dealloc */
+	0,                          /* tp_print */
+	0,                          /* tp_getattr */
+	0,                          /* tp_setattr */
+	0,                          /* tp_compare */
+	0,                          /* tp_repr */
+	0,                          /* tp_as_number */
+	0,                          /* tp_as_sequence */
+	0,                          /* tp_as_mapping */
+	0,                          /* tp_hash */
+	0,                          /* tp_call */
+	0,                          /* tp_str */
+	0,                          /* tp_getattro */
+	0,                          /* tp_setattro */
+	0,                          /* tp_as_buffer */
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+	"",                         /* tp_doc */
+	0,                          /* tp_traverse */
+	0,                          /* tp_clear */
+	0,                          /* tp_richcompare */
+	0,                          /* tp_weaklistoffset */
+	0,                          /* tp_iter */
+	0,                          /* tp_iternext */
+	0,		   		            /* tp_methods */
+	0,                          /* tp_members */
+	0,                          /* tp_getset */
+	0,                          /* tp_base */
+	0,                          /* tp_dict */
+	0,                          /* tp_descr_get */
+	0,                          /* tp_descr_set */
+	0,                          /* tp_dictoffset */
+	0,                          /* tp_init */
+	0,                          /* tp_alloc */
+	_PG_BodyNew,                /* tp_new */
+	0,                          /* tp_free */
+	0,                          /* tp_is_gc */
+	0,                          /* tp_bases */
+	0,                          /* tp_mro */
+	0,                          /* tp_cache */
+	0,                          /* tp_subclasses */
+	0,                          /* tp_weaklist */
+	0                           /* tp_del */
+};
+
 pgBodyObject* PG_BodyNew()
 {
-	pgBodyObject* op;
-	op = (pgBodyObject*)PyObject_MALLOC(sizeof(pgBodyObject));
-	PG_BodyInit(op);
-	return op;
+	//pgBodyObject* op;
+	//op = (pgBodyObject*)PyObject_MALLOC(sizeof(pgBodyObject));
+	//PG_BodyInit(op);
+	//return op;
+	return (pgBodyObject*) _PG_BodyNew(&pgBodyType, NULL, NULL);
 }
 
 pgVector2 PG_GetGlobalCor(pgBodyObject* body, pgVector2* local)
 
 
 
+
 //static PyMemberDef Body_members[] = {
 //	{"mass", T_FLOAT, offsetof(pgBodyObject,fMass), 0,"Mass"},
 //	{"linear_velocity",T_DOUBLE,offsetof(pyBodyObject,vecLinearVelocity),"Linear Velocity"},
 //    {NULL}  /* Sentinel */
 //};
 //
-//static PyTypeObject BodyType = {
-//    PyObject_HEAD_INIT(NULL)
-//    0,                         /*ob_size*/
-//    "physics.body",			/*tp_name*/
-//    sizeof(pgBodyObject),	/*tp_basicsize*/
-//    0,                         /*tp_itemsize*/
-//    (destructor)Body_dealloc, /*tp_dealloc*/
-//    0,                         /*tp_print*/
-//    0,                         /*tp_getattr*/
-//    0,                         /*tp_setattr*/
-//    0,                         /*tp_compare*/
-//    0,                         /*tp_repr*/
-//    0,                         /*tp_as_number*/
-//    0,                         /*tp_as_sequence*/
-//    0,                         /*tp_as_mapping*/
-//    0,                         /*tp_hash */
-//    0,                         /*tp_call*/
-//    0,                         /*tp_str*/
-//    0,                         /*tp_getattro*/
-//    0,                         /*tp_setattro*/
-//    0,                         /*tp_as_buffer*/
-//    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
-//    "Body objects",           /* tp_doc */
-//    0,		               /* tp_traverse */
-//    0,		               /* tp_clear */
-//    0,		               /* tp_richcompare */
-//    0,		               /* tp_weaklistoffset */
-//    0,		               /* tp_iter */
-//    0,		               /* tp_iternext */
-//    Body_methods,             /* tp_methods */
-//    Body_members,             /* tp_members */
-//    0,                         /* tp_getset */
-//    0,                         /* tp_base */
-//    0,                         /* tp_dict */
-//    0,                         /* tp_descr_get */
-//    0,                         /* tp_descr_set */
-//    0,                         /* tp_dictoffset */
-//    (initproc)Body_init,      /* tp_init */
-//    0,                         /* tp_alloc */
-//    Body_new,                 /* tp_new */
-//};
 
 

File src/pgShapeObject.c

 	//shape->centroid.imag = 0;
 }
 
-
-void PG_RectShapeDestroy()
+void PG_ShapeObjectDestroy(pgShapeObject* shape)
 {
-	//TODO: add destroy code
+	shape->Destroy(shape);
 }
 
 int PG_RectShapeIsPointIn(pgShapeObject* shape, pgVector2* point)
 	t1 = c_diff(ps->bottomRight, ps->bottomLeft);
 	t2 = c_diff(ps->topLeft, ps->bottomLeft);
 	s1 = fabs(c_cross(t1, t2));
-	
+
 	s2 = 0;
 
 	for(i = 0; i < 4; ++i)
 	return is_equal(s1, s2);
 }
 
+void PG_RectShapeDestroy(pgShapeObject* rectShape)
+{
+	PyObject_Free((pgRectShape*)rectShape);
+}
+
 pgShapeObject*	PG_RectShapeNew(pgBodyObject* body, double width, double height, double seta)
 {
 	int i;
 	pgRectShape* p = (pgRectShape*)PyObject_MALLOC(sizeof(pgRectShape));
-	
+
 	PG_ShapeObjectInit(&(p->shape));
 	p->shape.IsPointIn = PG_RectShapeIsPointIn;
-	
+	p->shape.Destroy = PG_RectShapeDestroy;
+
 	PG_Set_Vector2(p->bottomLeft, -width/2, -height/2);
 	PG_Set_Vector2(p->bottomRight, width/2, -height/2);
 	PG_Set_Vector2(p->topRight, width/2, height/2);

File src/pgWorldObject.c

 
 #define MAX_SOLVE_INTERAT 20
 
+static PyTypeObject pgWorldType;
+
 void _PG_FreeBodySimulation(pgWorldObject* world,double stepTime)
 {
 	Py_ssize_t size = PyList_Size((PyObject*)(world->bodyList));
 
 }
 
-pgWorldObject* PG_WorldNew()
+pgWorldObject* _PG_WorldNewInternal(PyTypeObject *type)
 {
-	pgWorldObject* op;
-	op = (pgWorldObject*)PyObject_MALLOC(sizeof(pgWorldObject));
+	pgWorldObject* op = (pgWorldObject*)type->tp_alloc(type, 0);
 	PG_WorldInit(op);
 	return op;
 }
 
-void	PG_WorldDestroy(pgWorldObject* world);
+PyObject* _PG_WorldNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
+{
+	/* In case we have arguments in the python code, parse them later
+	* on.
+	*/
+	if(PyType_Ready(type)==-1) return NULL;
+	return (PyObject*) _PG_WorldNewInternal(type);
+}
+
+pgWorldObject* PG_WorldNew()
+{
+	return (pgWorldObject*) _PG_WorldNew(&pgWorldType, NULL, NULL);
+}
+
+void PG_WorldDestroy(pgWorldObject* world)
+{
+	/*
+	* DECREF anything related to the world, such as the lists and
+	* release any other memory hold by it.
+	*/
+	Py_XDECREF(world->bodyList);
+	//TODO: i don't know how to release jointObject here,
+	//		since it's sub class's constructor function is diff from each other
+	//Py_XDECREF(world->jointList);
+
+	world->ob_type->tp_free((PyObject*)world);
+}
+
+static PyObject* _world_test_noargs(pgWorldObject *world)
+{
+	/* Do some things here */
+	Py_RETURN_NONE;
+}
+
+static PyObject* _world_test_args(pgWorldObject *world, PyObject *args)
+{
+	/* Parse arguments and do some things here */
+	Py_RETURN_NONE;
+}
+
+/**
+* Here we allow the Python object to do stuff like
+*
+*  myworld.test_noargs ()
+*  myworld.test_args (arg1, arg2, ...)
+*/
+static PyMethodDef _pgWorld_methods[] =
+{
+	{ "test_noargs", (PyCFunction) _world_test_noargs, METH_NOARGS, "" },
+	{ "test_args", (PyCFunction) _world_test_args, METH_VARARGS, "" },
+	{ NULL, NULL, 0, NULL } /* The NULL sentinel is important! */
+};
+
+static PyTypeObject pgWorldType =
+{
+	PyObject_HEAD_INIT(NULL)
+	0,
+	"physics.World",            /* tp_name */
+	sizeof(pgWorldObject),      /* tp_basicsize */
+	0,                          /* tp_itemsize */
+	(destructor) PG_WorldDestroy,/* tp_dealloc */
+	0,                          /* tp_print */
+	0,                          /* tp_getattr */
+	0,                          /* tp_setattr */
+	0,                          /* tp_compare */
+	0,                          /* tp_repr */
+	0,                          /* tp_as_number */
+	0,                          /* tp_as_sequence */
+	0,                          /* tp_as_mapping */
+	0,                          /* tp_hash */
+	0,                          /* tp_call */
+	0,                          /* tp_str */
+	0,                          /* tp_getattro */
+	0,                          /* tp_setattro */
+	0,                          /* tp_as_buffer */
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+	"",                         /* tp_doc */
+	0,                          /* tp_traverse */
+	0,                          /* tp_clear */
+	0,                          /* tp_richcompare */
+	0,                          /* tp_weaklistoffset */
+	0,                          /* tp_iter */
+	0,                          /* tp_iternext */
+	_pgWorld_methods,           /* tp_methods */
+	0,                          /* tp_members */
+	0,                          /* tp_getset */
+	0,                          /* tp_base */
+	0,                          /* tp_dict */
+	0,                          /* tp_descr_get */
+	0,                          /* tp_descr_set */
+	0,                          /* tp_dictoffset */
+	0,                          /* tp_init */
+	0,                          /* tp_alloc */
+	_PG_WorldNew,               /* tp_new */
+	0,                          /* tp_free */
+	0,                          /* tp_is_gc */
+	0,                          /* tp_bases */
+	0,                          /* tp_mro */
+	0,                          /* tp_cache */
+	0,                          /* tp_subclasses */
+	0,                          /* tp_weaklist */
+	0                           /* tp_del */
+};