mirror of
https://github.com/love2d/love.git
synced 2026-08-19 20:19:42 +02:00
First attempt at implementing Memoization for certain Box2D objects
--HG-- branch : box2d-update
This commit is contained in:
@@ -21,6 +21,7 @@
|
|||||||
#include "Body.h"
|
#include "Body.h"
|
||||||
|
|
||||||
#include <common/math.h>
|
#include <common/math.h>
|
||||||
|
#include <common/Memoizer.h>
|
||||||
|
|
||||||
#include "Shape.h"
|
#include "Shape.h"
|
||||||
#include "Fixture.h"
|
#include "Fixture.h"
|
||||||
@@ -41,10 +42,20 @@ namespace box2d
|
|||||||
def.position = Physics::scaleDown(p);
|
def.position = Physics::scaleDown(p);
|
||||||
body = world->world->CreateBody(&def);
|
body = world->world->CreateBody(&def);
|
||||||
this->setType(type);
|
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()
|
Body::~Body()
|
||||||
{
|
{
|
||||||
|
Memoizer::remove(body);
|
||||||
world->world->DestroyBody(body);
|
world->world->DestroyBody(body);
|
||||||
world->release();
|
world->release();
|
||||||
body = 0;
|
body = 0;
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ namespace box2d
|
|||||||
* Create a Body at position p.
|
* Create a Body at position p.
|
||||||
**/
|
**/
|
||||||
Body(World * world, b2Vec2 p, Type type);
|
Body(World * world, b2Vec2 p, Type type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a Body from an extant b2Body.
|
||||||
|
**/
|
||||||
|
Body(b2Body * b);
|
||||||
|
|
||||||
virtual ~Body();
|
virtual ~Body();
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,8 @@
|
|||||||
#include "World.h"
|
#include "World.h"
|
||||||
#include "Physics.h"
|
#include "Physics.h"
|
||||||
|
|
||||||
|
#include <common/Memoizer.h>
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
{
|
{
|
||||||
namespace physics
|
namespace physics
|
||||||
@@ -35,10 +37,12 @@ namespace box2d
|
|||||||
: loop(loop)
|
: loop(loop)
|
||||||
{
|
{
|
||||||
shape = c;
|
shape = c;
|
||||||
|
Memoizer::add(shape, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
ChainShape::~ChainShape()
|
ChainShape::~ChainShape()
|
||||||
{
|
{
|
||||||
|
Memoizer::remove(shape);
|
||||||
delete shape;
|
delete shape;
|
||||||
shape = NULL;
|
shape = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@
|
|||||||
#include "Body.h"
|
#include "Body.h"
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
|
|
||||||
|
#include <common/Memoizer.h>
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
{
|
{
|
||||||
namespace physics
|
namespace physics
|
||||||
@@ -33,10 +35,12 @@ namespace box2d
|
|||||||
CircleShape::CircleShape(b2CircleShape * c)
|
CircleShape::CircleShape(b2CircleShape * c)
|
||||||
{
|
{
|
||||||
shape = c;
|
shape = c;
|
||||||
|
Memoizer::add(shape, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
CircleShape::~CircleShape()
|
CircleShape::~CircleShape()
|
||||||
{
|
{
|
||||||
|
Memoizer::remove(shape);
|
||||||
delete shape;
|
delete shape;
|
||||||
shape = NULL;
|
shape = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@
|
|||||||
#include "Body.h"
|
#include "Body.h"
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
|
|
||||||
|
#include <common/Memoizer.h>
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
{
|
{
|
||||||
namespace physics
|
namespace physics
|
||||||
@@ -33,10 +35,12 @@ namespace box2d
|
|||||||
EdgeShape::EdgeShape(b2EdgeShape * e)
|
EdgeShape::EdgeShape(b2EdgeShape * e)
|
||||||
{
|
{
|
||||||
shape = e;
|
shape = e;
|
||||||
|
Memoizer::add(shape, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
EdgeShape::~EdgeShape()
|
EdgeShape::~EdgeShape()
|
||||||
{
|
{
|
||||||
|
Memoizer::remove(shape);
|
||||||
delete shape;
|
delete shape;
|
||||||
shape = NULL;
|
shape = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@
|
|||||||
#include "World.h"
|
#include "World.h"
|
||||||
#include "Physics.h"
|
#include "Physics.h"
|
||||||
|
|
||||||
|
#include <common/Memoizer.h>
|
||||||
|
|
||||||
// STD
|
// STD
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
|
||||||
@@ -46,6 +48,20 @@ namespace box2d
|
|||||||
def.userData = (void *)data;
|
def.userData = (void *)data;
|
||||||
def.density = density;
|
def.density = density;
|
||||||
fixture = body->body->CreateFixture(&def);
|
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()
|
Fixture::~Fixture()
|
||||||
|
|||||||
@@ -70,6 +70,11 @@ namespace box2d
|
|||||||
* Creates a Fixture.
|
* Creates a Fixture.
|
||||||
**/
|
**/
|
||||||
Fixture(Body * body, Shape * shape, float density);
|
Fixture(Body * body, Shape * shape, float density);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Fixture.
|
||||||
|
**/
|
||||||
|
Fixture(b2Fixture * f);
|
||||||
|
|
||||||
virtual ~Fixture();
|
virtual ~Fixture();
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,8 @@
|
|||||||
#include "World.h"
|
#include "World.h"
|
||||||
#include "Physics.h"
|
#include "Physics.h"
|
||||||
|
|
||||||
|
#include <common/Memoizer.h>
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
{
|
{
|
||||||
namespace physics
|
namespace physics
|
||||||
@@ -34,10 +36,12 @@ namespace box2d
|
|||||||
PolygonShape::PolygonShape(b2PolygonShape * p)
|
PolygonShape::PolygonShape(b2PolygonShape * p)
|
||||||
{
|
{
|
||||||
shape = p;
|
shape = p;
|
||||||
|
Memoizer::add(shape, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
PolygonShape::~PolygonShape()
|
PolygonShape::~PolygonShape()
|
||||||
{
|
{
|
||||||
|
Memoizer::remove(shape);
|
||||||
delete shape;
|
delete shape;
|
||||||
shape = NULL;
|
shape = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@
|
|||||||
#include "World.h"
|
#include "World.h"
|
||||||
#include "Physics.h"
|
#include "Physics.h"
|
||||||
|
|
||||||
|
#include <common/Memoizer.h>
|
||||||
|
|
||||||
// STD
|
// STD
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
|
|
||||||
@@ -38,9 +40,18 @@ namespace box2d
|
|||||||
: shape(NULL)
|
: shape(NULL)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
Shape::Shape(b2Shape * shape)
|
||||||
|
: shape(shape)
|
||||||
|
{
|
||||||
|
Memoizer::add(shape, this);
|
||||||
|
}
|
||||||
|
|
||||||
Shape::~Shape()
|
Shape::~Shape()
|
||||||
{
|
{
|
||||||
|
if (shape) {
|
||||||
|
Memoizer::remove(shape);
|
||||||
|
delete shape;
|
||||||
|
}
|
||||||
shape = 0;
|
shape = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ namespace box2d
|
|||||||
* Creates a Shape.
|
* Creates a Shape.
|
||||||
**/
|
**/
|
||||||
Shape();
|
Shape();
|
||||||
|
Shape(b2Shape * shape);
|
||||||
|
|
||||||
virtual ~Shape();
|
virtual ~Shape();
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include "Shape.h"
|
#include "Shape.h"
|
||||||
#include "Contact.h"
|
#include "Contact.h"
|
||||||
#include "Physics.h"
|
#include "Physics.h"
|
||||||
|
#include <common/Memoizer.h>
|
||||||
#include <common/Reference.h>
|
#include <common/Reference.h>
|
||||||
|
|
||||||
namespace love
|
namespace love
|
||||||
@@ -125,6 +126,7 @@ namespace box2d
|
|||||||
world->SetContactListener(this);
|
world->SetContactListener(this);
|
||||||
b2BodyDef def;
|
b2BodyDef def;
|
||||||
groundBody = world->CreateBody(&def);
|
groundBody = world->CreateBody(&def);
|
||||||
|
Memoizer::add(world, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
World::World(b2Vec2 gravity, bool sleep)
|
World::World(b2Vec2 gravity, bool sleep)
|
||||||
@@ -135,11 +137,13 @@ namespace box2d
|
|||||||
world->SetContactListener(this);
|
world->SetContactListener(this);
|
||||||
b2BodyDef def;
|
b2BodyDef def;
|
||||||
groundBody = world->CreateBody(&def);
|
groundBody = world->CreateBody(&def);
|
||||||
|
Memoizer::add(world, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
World::~World()
|
World::~World()
|
||||||
{
|
{
|
||||||
world->DestroyBody(groundBody);
|
world->DestroyBody(groundBody);
|
||||||
|
Memoizer::remove(world);
|
||||||
delete world;
|
delete world;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user