Browse Source

First attempt at implementing Memoization for certain Box2D objects

--HG--
branch : box2d-update
Bill Meltsner 14 years ago
parent
commit
c705063597

+ 11 - 0
src/modules/physics/box2d/Body.cpp

@@ -21,6 +21,7 @@
 #include "Body.h"
 
 #include <common/math.h>
+#include <common/Memoizer.h>
 
 #include "Shape.h"
 #include "Fixture.h"
@@ -41,10 +42,20 @@ namespace box2d
 		def.position = Physics::scaleDown(p);
 		body = world->world->CreateBody(&def);
 		this->setType(type);
+		Memoizer::add(body, this);
+	}
+	
+	Body::Body(b2Body * b)
+		: body(b)
+	{
+		world = (World *)Memoizer::find(b->GetWorld());
+		world->retain();
+		Memoizer::add(body, this);
 	}
 
 	Body::~Body()
 	{
+		Memoizer::remove(body);
 		world->world->DestroyBody(body);
 		world->release();
 		body = 0;

+ 5 - 0
src/modules/physics/box2d/Body.h

@@ -76,6 +76,11 @@ namespace box2d
 		* Create a Body at position p.
 		**/
 		Body(World * world, b2Vec2 p, Type type);
+		
+		/**
+		* Create a Body from an extant b2Body.
+		**/
+		Body(b2Body * b);
 
 		virtual ~Body();
 

+ 4 - 0
src/modules/physics/box2d/ChainShape.cpp

@@ -25,6 +25,8 @@
 #include "World.h"
 #include "Physics.h"
 
+#include <common/Memoizer.h>
+
 namespace love
 {
 namespace physics
@@ -35,10 +37,12 @@ namespace box2d
 		: loop(loop)
 	{
 		shape = c;
+		Memoizer::add(shape, this);
 	}
 
 	ChainShape::~ChainShape()
 	{
+		Memoizer::remove(shape);
 		delete shape;
 		shape = NULL;
 	}

+ 4 - 0
src/modules/physics/box2d/CircleShape.cpp

@@ -24,6 +24,8 @@
 #include "Body.h"
 #include "World.h"
 
+#include <common/Memoizer.h>
+
 namespace love
 {
 namespace physics
@@ -33,10 +35,12 @@ namespace box2d
 	CircleShape::CircleShape(b2CircleShape * c)
 	{
 		shape = c;
+		Memoizer::add(shape, this);
 	}
 
 	CircleShape::~CircleShape()
 	{
+		Memoizer::remove(shape);
 		delete shape;
         shape = NULL;
 	}

+ 4 - 0
src/modules/physics/box2d/EdgeShape.cpp

@@ -24,6 +24,8 @@
 #include "Body.h"
 #include "World.h"
 
+#include <common/Memoizer.h>
+
 namespace love
 {
 namespace physics
@@ -33,10 +35,12 @@ namespace box2d
 	EdgeShape::EdgeShape(b2EdgeShape * e)
 	{
 		shape = e;
+		Memoizer::add(shape, this);
 	}
 
 	EdgeShape::~EdgeShape()
 	{
+		Memoizer::remove(shape);
 		delete shape;
 		shape = NULL;
 	}

+ 16 - 0
src/modules/physics/box2d/Fixture.cpp

@@ -25,6 +25,8 @@
 #include "World.h"
 #include "Physics.h"
 
+#include <common/Memoizer.h>
+
 // STD
 #include <bitset>
 
@@ -46,6 +48,20 @@ namespace box2d
 		def.userData = (void *)data;
 		def.density = density;
 		fixture = body->body->CreateFixture(&def);
+		Memoizer::add(fixture, this);
+	}
+	
+	Fixture::Fixture(b2Fixture * f)
+		: fixture(f)
+	{
+		data = (fixtureudata *)f->GetUserData();
+		body = (Body *)Memoizer::find(f->GetBody());
+		if (!body) body = new Body(f->GetBody());
+		body->retain();
+		shape = (Shape *)Memoizer::find(f->GetShape());
+		if (!shape) shape = new Shape(f->GetShape());
+		shape->retain();
+		Memoizer::add(fixture, this);
 	}
 
 	Fixture::~Fixture()

+ 5 - 0
src/modules/physics/box2d/Fixture.h

@@ -70,6 +70,11 @@ namespace box2d
 		* Creates a Fixture.
 		**/
 		Fixture(Body * body, Shape * shape, float density);
+		
+		/**
+		* Creates a Fixture.
+		**/
+		Fixture(b2Fixture * f);
 
 		virtual ~Fixture();
 

+ 4 - 0
src/modules/physics/box2d/PolygonShape.cpp

@@ -25,6 +25,8 @@
 #include "World.h"
 #include "Physics.h"
 
+#include <common/Memoizer.h>
+
 namespace love
 {
 namespace physics
@@ -34,10 +36,12 @@ namespace box2d
 	PolygonShape::PolygonShape(b2PolygonShape * p)
 	{
 		shape = p;
+		Memoizer::add(shape, this);
 	}
 
 	PolygonShape::~PolygonShape()
 	{
+		Memoizer::remove(shape);
 		delete shape;
 		shape = NULL;
 	}

+ 11 - 0
src/modules/physics/box2d/Shape.cpp

@@ -25,6 +25,8 @@
 #include "World.h"
 #include "Physics.h"
 
+#include <common/Memoizer.h>
+
 // STD
 #include <bitset>
 
@@ -38,9 +40,18 @@ namespace box2d
 		: shape(NULL)
 	{
 	}
+	Shape::Shape(b2Shape * shape)
+		: shape(shape)
+	{
+		Memoizer::add(shape, this);
+	}
 
 	Shape::~Shape()
 	{
+		if (shape) {
+			Memoizer::remove(shape);
+			delete shape;
+		}
 		shape = 0;
 	}
 

+ 1 - 0
src/modules/physics/box2d/Shape.h

@@ -57,6 +57,7 @@ namespace box2d
 		* Creates a Shape.
 		**/
 		Shape();
+		Shape(b2Shape * shape);
 
 		virtual ~Shape();
 

+ 4 - 0
src/modules/physics/box2d/World.cpp

@@ -24,6 +24,7 @@
 #include "Shape.h"
 #include "Contact.h"
 #include "Physics.h"
+#include <common/Memoizer.h>
 #include <common/Reference.h>
 
 namespace love
@@ -125,6 +126,7 @@ namespace box2d
 		world->SetContactListener(this);
 		b2BodyDef def;
 		groundBody = world->CreateBody(&def);
+		Memoizer::add(world, this);
 	}
 
 	World::World(b2Vec2 gravity, bool sleep)
@@ -135,11 +137,13 @@ namespace box2d
 		world->SetContactListener(this);
 		b2BodyDef def;
 		groundBody = world->CreateBody(&def);
+		Memoizer::add(world, this);
 	}
 
 	World::~World()
 	{
 		world->DestroyBody(groundBody);
+		Memoizer::remove(world);
 		delete world;
 	}