diff --git a/CMakeLists.txt b/CMakeLists.txt index e99a42835..e777f598b 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,8 +285,6 @@ set(LOVE_SRC_COMMON src/common/math.h src/common/Matrix.cpp src/common/Matrix.h - src/common/Memoizer.cpp - src/common/Memoizer.h src/common/memory.cpp src/common/memory.h src/common/Module.cpp diff --git a/platform/xcode/liblove.xcodeproj/project.pbxproj b/platform/xcode/liblove.xcodeproj/project.pbxproj index cd580a651..ff00ff418 100644 --- a/platform/xcode/liblove.xcodeproj/project.pbxproj +++ b/platform/xcode/liblove.xcodeproj/project.pbxproj @@ -81,9 +81,6 @@ FA0B79291A958E3B000E1D17 /* Matrix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B79021A958E3B000E1D17 /* Matrix.cpp */; }; FA0B792A1A958E3B000E1D17 /* Matrix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B79021A958E3B000E1D17 /* Matrix.cpp */; }; FA0B792B1A958E3B000E1D17 /* Matrix.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B79031A958E3B000E1D17 /* Matrix.h */; }; - FA0B792C1A958E3B000E1D17 /* Memoizer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B79041A958E3B000E1D17 /* Memoizer.cpp */; }; - FA0B792D1A958E3B000E1D17 /* Memoizer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B79041A958E3B000E1D17 /* Memoizer.cpp */; }; - FA0B792E1A958E3B000E1D17 /* Memoizer.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B79051A958E3B000E1D17 /* Memoizer.h */; }; FA0B792F1A958E3B000E1D17 /* Module.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B79061A958E3B000E1D17 /* Module.cpp */; }; FA0B79301A958E3B000E1D17 /* Module.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0B79061A958E3B000E1D17 /* Module.cpp */; }; FA0B79311A958E3B000E1D17 /* Module.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0B79071A958E3B000E1D17 /* Module.h */; }; @@ -1297,8 +1294,6 @@ FA0B79011A958E3B000E1D17 /* math.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = math.h; sourceTree = ""; }; FA0B79021A958E3B000E1D17 /* Matrix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Matrix.cpp; sourceTree = ""; }; FA0B79031A958E3B000E1D17 /* Matrix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Matrix.h; sourceTree = ""; }; - FA0B79041A958E3B000E1D17 /* Memoizer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Memoizer.cpp; sourceTree = ""; }; - FA0B79051A958E3B000E1D17 /* Memoizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Memoizer.h; sourceTree = ""; }; FA0B79061A958E3B000E1D17 /* Module.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Module.cpp; sourceTree = ""; }; FA0B79071A958E3B000E1D17 /* Module.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Module.h; sourceTree = ""; }; FA0B79081A958E3B000E1D17 /* Object.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Object.cpp; sourceTree = ""; }; @@ -2176,8 +2171,6 @@ FA0B79011A958E3B000E1D17 /* math.h */, FA0B79021A958E3B000E1D17 /* Matrix.cpp */, FA0B79031A958E3B000E1D17 /* Matrix.h */, - FA0B79041A958E3B000E1D17 /* Memoizer.cpp */, - FA0B79051A958E3B000E1D17 /* Memoizer.h */, FA56AA361FAFF02000A43D5F /* memory.cpp */, FA56AA371FAFF02000A43D5F /* memory.h */, FA0B79061A958E3B000E1D17 /* Module.cpp */, @@ -4000,7 +3993,6 @@ FA0B7E321A95902C000E1D17 /* Shape.h in Headers */, FA620A371AA2F8DB005DB4C2 /* wrap_Texture.h in Headers */, FA0B7DBA1A95902C000E1D17 /* JoystickModule.h in Headers */, - FA0B792E1A958E3B000E1D17 /* Memoizer.h in Headers */, FA0B7A731A958EA3000E1D17 /* b2ChainAndCircleContact.h in Headers */, FA0B7AA01A958EA3000E1D17 /* b2PrismaticJoint.h in Headers */, FA0B7DEA1A95902C000E1D17 /* Mouse.h in Headers */, @@ -4368,7 +4360,6 @@ FAF140561E20934C00F898D2 /* Link.cpp in Sources */, FAF140851E20934C00F898D2 /* ParseHelper.cpp in Sources */, FA0B7D801A95902C000E1D17 /* Volatile.cpp in Sources */, - FA0B792D1A958E3B000E1D17 /* Memoizer.cpp in Sources */, FA1BA0B21E16FD0800AA2803 /* Shader.cpp in Sources */, FA0B7EBC1A95902C000E1D17 /* LuaThread.cpp in Sources */, FA0B7A871A958EA3000E1D17 /* b2PolygonAndCircleContact.cpp in Sources */, @@ -4694,7 +4685,6 @@ FA0B7E421A95902C000E1D17 /* wrap_CircleShape.cpp in Sources */, FA0B7CE51A95902C000E1D17 /* wrap_Source.cpp in Sources */, FAC7CD921FE35E95006A60C7 /* physfs_archiver_hog.c in Sources */, - FA0B792C1A958E3B000E1D17 /* Memoizer.cpp in Sources */, FAF140931E20934C00F898D2 /* PpScanner.cpp in Sources */, FA9D53AC1F5307E900125C6B /* Deprecations.cpp in Sources */, FA0B7CCD1A95902C000E1D17 /* Audio.cpp in Sources */, diff --git a/src/common/Memoizer.cpp b/src/common/Memoizer.cpp deleted file mode 100644 index b172c9542..000000000 --- a/src/common/Memoizer.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (c) 2006-2019 LOVE Development Team - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - **/ - -#include "Memoizer.h" - -#include - -namespace love -{ - -static std::unordered_map objectMap; - -void Memoizer::add(void *key, void *val) -{ - objectMap[key] = val; -} - -void Memoizer::remove(void *key) -{ - objectMap.erase(key); -} - -void *Memoizer::find(void *key) -{ - auto it = objectMap.find(key); - - if (it != objectMap.end()) - return it->second; - else - return nullptr; -} - -} // love diff --git a/src/common/Memoizer.h b/src/common/Memoizer.h deleted file mode 100644 index ead11f290..000000000 --- a/src/common/Memoizer.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) 2006-2019 LOVE Development Team - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * 3. This notice may not be removed or altered from any source distribution. - **/ - -#ifndef LOVE_MEMOIZER_H -#define LOVE_MEMOIZER_H - -namespace love -{ - -class Memoizer -{ -public: - - static void add(void *key, void *val); - static void remove(void *key); - static void *find(void *key); - -}; // Memoizer - -} // love - -#endif // LOVE_MEMOIZER_H diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index f608ce38a..ee133fa31 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -21,7 +21,6 @@ #include "Body.h" #include "common/math.h" -#include "common/Memoizer.h" #include "Shape.h" #include "Fixture.h" @@ -51,18 +50,7 @@ Body::Body(World *world, b2Vec2 p, Body::Type type) // Box2D body holds a reference to the love Body. this->retain(); this->setType(type); - Memoizer::add(body, this); -} - -Body::Body(b2Body *b) - : body(b) - , udata(nullptr) -{ - udata = (bodyudata *) b->GetUserData(); - world = (World *) Memoizer::find(b->GetWorld()); - // Box2D body holds a reference to the love Body. - this->retain(); - Memoizer::add(body, this); + world->registerObject(body, this); } Body::~Body() @@ -452,7 +440,7 @@ int Body::getFixtures(lua_State *L) const { if (!f) break; - Fixture *fixture = (Fixture *)Memoizer::find(f); + Fixture *fixture = (Fixture *)world->findObject(f); if (!fixture) throw love::Exception("A fixture has escaped Memoizer!"); luax_pushtype(L, fixture); @@ -474,7 +462,7 @@ int Body::getJoints(lua_State *L) const if (!je) break; - Joint *joint = (Joint *) Memoizer::find(je->joint); + Joint *joint = (Joint *) world->findObject(je->joint); if (!joint) throw love::Exception("A joint has escaped Memoizer!"); @@ -497,9 +485,9 @@ int Body::getContacts(lua_State *L) const if (!ce) break; - Contact *contact = (Contact *) Memoizer::find(ce->contact); + Contact *contact = (Contact *) world->findObject(ce->contact); if (!contact) - contact = new Contact(ce->contact); + contact = new Contact(world, ce->contact); else contact->retain(); @@ -523,7 +511,7 @@ void Body::destroy() } world->world->DestroyBody(body); - Memoizer::remove(body); + world->unregisterObject(body); body = NULL; // Remove userdata reference to avoid it sticking around after GC diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index 56b85ab85..dc9512140 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -77,11 +77,6 @@ public: **/ Body(World *world, b2Vec2 p, Type type); - /** - * Create a Body from an extant b2Body. - **/ - Body(b2Body *b); - virtual ~Body(); /** diff --git a/src/modules/physics/box2d/ChainShape.cpp b/src/modules/physics/box2d/ChainShape.cpp index 1e669b913..e6881c856 100644 --- a/src/modules/physics/box2d/ChainShape.cpp +++ b/src/modules/physics/box2d/ChainShape.cpp @@ -25,8 +25,6 @@ #include "World.h" #include "Physics.h" -#include "common/Memoizer.h" - namespace love { namespace physics diff --git a/src/modules/physics/box2d/CircleShape.cpp b/src/modules/physics/box2d/CircleShape.cpp index bfa69236b..e086f54ee 100644 --- a/src/modules/physics/box2d/CircleShape.cpp +++ b/src/modules/physics/box2d/CircleShape.cpp @@ -25,8 +25,6 @@ #include "World.h" #include "Physics.h" -#include "common/Memoizer.h" - namespace love { namespace physics diff --git a/src/modules/physics/box2d/Contact.cpp b/src/modules/physics/box2d/Contact.cpp index 9d624fe16..7a5934608 100644 --- a/src/modules/physics/box2d/Contact.cpp +++ b/src/modules/physics/box2d/Contact.cpp @@ -22,8 +22,6 @@ #include "World.h" #include "Physics.h" -#include "common/Memoizer.h" - namespace love { namespace physics @@ -33,10 +31,11 @@ namespace box2d love::Type Contact::type("Contact", &Object::type); -Contact::Contact(b2Contact *contact) +Contact::Contact(World *world, b2Contact *contact) : contact(contact) + , world(world) { - Memoizer::add(contact, this); + world->registerObject(contact, this); } Contact::~Contact() @@ -48,14 +47,14 @@ void Contact::invalidate() { if (contact != NULL) { - Memoizer::remove(contact); + world->unregisterObject(contact); contact = NULL; } } bool Contact::isValid() { - return contact != NULL ? true : false; + return contact != NULL; } int Contact::getPositions(lua_State *L) @@ -146,8 +145,8 @@ void Contact::getChildren(int &childA, int &childB) void Contact::getFixtures(Fixture *&fixtureA, Fixture *&fixtureB) { - fixtureA = (Fixture *) Memoizer::find(contact->GetFixtureA()); - fixtureB = (Fixture *) Memoizer::find(contact->GetFixtureB()); + fixtureA = (Fixture *) world->findObject(contact->GetFixtureA()); + fixtureB = (Fixture *) world->findObject(contact->GetFixtureB()); if (!fixtureA || !fixtureB) throw love::Exception("A fixture has escaped Memoizer!"); diff --git a/src/modules/physics/box2d/Contact.h b/src/modules/physics/box2d/Contact.h index 00327fad0..e06e9e961 100644 --- a/src/modules/physics/box2d/Contact.h +++ b/src/modules/physics/box2d/Contact.h @@ -57,7 +57,7 @@ public: * data pointed to. * @param contact Pointer to the Box2D contact. **/ - Contact(b2Contact *contact); + Contact(World *world, b2Contact *contact); virtual ~Contact(); @@ -159,6 +159,8 @@ private: // The Box2D contact. b2Contact *contact; + + World *world; }; } // box2d diff --git a/src/modules/physics/box2d/EdgeShape.cpp b/src/modules/physics/box2d/EdgeShape.cpp index b540b419f..568b02d1d 100644 --- a/src/modules/physics/box2d/EdgeShape.cpp +++ b/src/modules/physics/box2d/EdgeShape.cpp @@ -25,8 +25,6 @@ #include "World.h" #include "Physics.h" -#include "common/Memoizer.h" - namespace love { namespace physics diff --git a/src/modules/physics/box2d/Fixture.cpp b/src/modules/physics/box2d/Fixture.cpp index 21eff0722..e2392e530 100644 --- a/src/modules/physics/box2d/Fixture.cpp +++ b/src/modules/physics/box2d/Fixture.cpp @@ -25,8 +25,6 @@ #include "World.h" #include "Physics.h" -#include "common/Memoizer.h" - // STD #include @@ -51,18 +49,7 @@ Fixture::Fixture(Body *body, Shape *shape, float density) def.density = density; fixture = body->body->CreateFixture(&def); this->retain(); - Memoizer::add(fixture, this); -} - -Fixture::Fixture(b2Fixture *f) - : fixture(f) -{ - udata = (fixtureudata *)f->GetUserData(); - body = (Body *)Memoizer::find(f->GetBody()); - if (!body) - body = new Body(f->GetBody()); - this->retain(); - Memoizer::add(fixture, this); + body->world->registerObject(fixture, this); } Fixture::~Fixture() @@ -361,7 +348,7 @@ void Fixture::destroy(bool implicit) if (!implicit && fixture != nullptr) body->body->DestroyFixture(fixture); - Memoizer::remove(fixture); + body->world->unregisterObject(fixture); fixture = nullptr; // Remove userdata reference to avoid it sticking around after GC diff --git a/src/modules/physics/box2d/Fixture.h b/src/modules/physics/box2d/Fixture.h index d6daa4838..28172dcc4 100644 --- a/src/modules/physics/box2d/Fixture.h +++ b/src/modules/physics/box2d/Fixture.h @@ -38,6 +38,8 @@ namespace physics namespace box2d { +class World; + /** * This struct is stored in a void pointer * in the Box2D Fixture class. For now, all we @@ -68,11 +70,6 @@ public: **/ Fixture(Body *body, Shape *shape, float density); - /** - * Creates a Fixture. - **/ - Fixture(b2Fixture *f); - virtual ~Fixture(); /** diff --git a/src/modules/physics/box2d/GearJoint.cpp b/src/modules/physics/box2d/GearJoint.cpp index f8cdad86d..822e2f561 100644 --- a/src/modules/physics/box2d/GearJoint.cpp +++ b/src/modules/physics/box2d/GearJoint.cpp @@ -23,7 +23,6 @@ // Module #include "Body.h" #include "World.h" -#include "common/Memoizer.h" namespace love { @@ -69,7 +68,7 @@ Joint *GearJoint::getJointA() const if (b2joint == nullptr) return nullptr; - Joint *j = (Joint *) Memoizer::find(b2joint); + Joint *j = (Joint *) world->findObject(b2joint); if (j == nullptr) throw love::Exception("A joint has escaped Memoizer!"); @@ -82,7 +81,7 @@ Joint *GearJoint::getJointB() const if (b2joint == nullptr) return nullptr; - Joint *j = (Joint *) Memoizer::find(b2joint); + Joint *j = (Joint *) world->findObject(b2joint); if (j == nullptr) throw love::Exception("A joint has escaped Memoizer!"); diff --git a/src/modules/physics/box2d/Joint.cpp b/src/modules/physics/box2d/Joint.cpp index 2c665de91..3d266cc5d 100644 --- a/src/modules/physics/box2d/Joint.cpp +++ b/src/modules/physics/box2d/Joint.cpp @@ -23,9 +23,6 @@ // STD #include -// LOVE -#include "common/Memoizer.h" - // Module #include "Body.h" #include "World.h" @@ -107,7 +104,7 @@ Body *Joint::getBodyA() const if (b2body == nullptr) return nullptr; - Body *body = (Body *) Memoizer::find(b2body); + Body *body = (Body *) world->findObject(b2body); if (body == nullptr) throw love::Exception("A body has escaped Memoizer!"); @@ -120,7 +117,7 @@ Body *Joint::getBodyB() const if (b2body == nullptr) return nullptr; - Body *body = (Body *) Memoizer::find(b2body); + Body *body = (Body *) world->findObject(b2body); if (body == nullptr) throw love::Exception("A body has escaped Memoizer!"); @@ -129,7 +126,7 @@ Body *Joint::getBodyB() const bool Joint::isValid() const { - return joint != 0; + return joint != nullptr; } int Joint::getAnchors(lua_State *L) @@ -159,7 +156,7 @@ b2Joint *Joint::createJoint(b2JointDef *def) { def->userData = udata; joint = world->world->CreateJoint(def); - Memoizer::add(joint, this); + world->registerObject(joint, this); // Box2D joint has a reference to this love Joint. this->retain(); return joint; @@ -175,9 +172,9 @@ void Joint::destroyJoint(bool implicit) return; } - if (!implicit && joint != 0) + if (!implicit && joint != nullptr) world->world->DestroyJoint(joint); - Memoizer::remove(joint); + world->unregisterObject(joint); joint = NULL; // Remove userdata reference to avoid it sticking around after GC diff --git a/src/modules/physics/box2d/Joint.h b/src/modules/physics/box2d/Joint.h index e5fa55293..4d294f0a6 100644 --- a/src/modules/physics/box2d/Joint.h +++ b/src/modules/physics/box2d/Joint.h @@ -153,7 +153,7 @@ private: // The Box2D joint object. b2Joint *joint; -}; +}; // Joint } // box2d } // physics diff --git a/src/modules/physics/box2d/PolygonShape.cpp b/src/modules/physics/box2d/PolygonShape.cpp index ebbfc1be6..49ef03736 100644 --- a/src/modules/physics/box2d/PolygonShape.cpp +++ b/src/modules/physics/box2d/PolygonShape.cpp @@ -25,8 +25,6 @@ #include "World.h" #include "Physics.h" -#include "common/Memoizer.h" - namespace love { namespace physics diff --git a/src/modules/physics/box2d/Shape.cpp b/src/modules/physics/box2d/Shape.cpp index 5bb201156..118438ad0 100644 --- a/src/modules/physics/box2d/Shape.cpp +++ b/src/modules/physics/box2d/Shape.cpp @@ -25,8 +25,6 @@ #include "World.h" #include "Physics.h" -#include "common/Memoizer.h" - // STD #include @@ -47,17 +45,12 @@ Shape::Shape(b2Shape *shape, bool own) : shape(shape) , own(own) { - if (own) - Memoizer::add(shape, this); } Shape::~Shape() { if (shape && own) - { - Memoizer::remove(shape); delete shape; - } shape = nullptr; } diff --git a/src/modules/physics/box2d/World.cpp b/src/modules/physics/box2d/World.cpp index 6d706dd2a..b5edc5495 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -24,7 +24,6 @@ #include "Shape.h" #include "Contact.h" #include "Physics.h" -#include "common/Memoizer.h" #include "common/Reference.h" namespace love @@ -36,9 +35,10 @@ namespace box2d love::Type World::type("World", &Object::type); -World::ContactCallback::ContactCallback() +World::ContactCallback::ContactCallback(World *world) : ref(nullptr) , L(nullptr) + , world(world) { } @@ -57,7 +57,7 @@ void World::ContactCallback::process(b2Contact *contact, const b2ContactImpulse // Push first fixture. { - Fixture *a = (Fixture *)Memoizer::find(contact->GetFixtureA()); + Fixture *a = (Fixture *)world->findObject(contact->GetFixtureA()); if (a != nullptr) luax_pushtype(L, a); else @@ -66,16 +66,16 @@ void World::ContactCallback::process(b2Contact *contact, const b2ContactImpulse // Push second fixture. { - Fixture *b = (Fixture *)Memoizer::find(contact->GetFixtureB()); + Fixture *b = (Fixture *)world->findObject(contact->GetFixtureB()); if (b != nullptr) luax_pushtype(L, b); else throw love::Exception("A fixture has escaped Memoizer!"); } - Contact *cobj = (Contact *)Memoizer::find(contact); + Contact *cobj = (Contact *)world->findObject(contact); if (!cobj) - cobj = new Contact(contact); + cobj = new Contact(world, contact); else cobj->retain(); @@ -138,8 +138,9 @@ bool World::ContactFilter::process(Fixture *a, Fixture *b) return true; } -World::QueryCallback::QueryCallback(lua_State *L, int idx) - : L(L) +World::QueryCallback::QueryCallback(World *world, lua_State *L, int idx) + : world(world) + , L(L) , funcidx(idx) { luaL_checktype(L, funcidx, LUA_TFUNCTION); @@ -154,7 +155,7 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture) if (L != nullptr) { lua_pushvalue(L, funcidx); - Fixture *f = (Fixture *)Memoizer::find(fixture); + Fixture *f = (Fixture *)world->findObject(fixture); if (!f) throw love::Exception("A fixture has escaped Memoizer!"); luax_pushtype(L, f); @@ -167,8 +168,9 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture) return true; } -World::RayCastCallback::RayCastCallback(lua_State *L, int idx) - : L(L) +World::RayCastCallback::RayCastCallback(World *world, lua_State *L, int idx) + : world(world) + , L(L) , funcidx(idx) { luaL_checktype(L, funcidx, LUA_TFUNCTION); @@ -183,7 +185,7 @@ float32 World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 & if (L != nullptr) { lua_pushvalue(L, funcidx); - Fixture *f = (Fixture *)Memoizer::find(fixture); + Fixture *f = (Fixture *)world->findObject(fixture); if (!f) throw love::Exception("A fixture has escaped Memoizer!"); luax_pushtype(L, f); @@ -206,14 +208,14 @@ float32 World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 & void World::SayGoodbye(b2Fixture *fixture) { - Fixture *f = (Fixture *)Memoizer::find(fixture); + Fixture *f = (Fixture *)findObject(fixture); // Hint implicit destruction with true. if (f) f->destroy(true); } void World::SayGoodbye(b2Joint *joint) { - Joint *j = (Joint *)Memoizer::find(joint); + Joint *j = (Joint *)findObject(joint); // Hint implicit destruction with true. if (j) j->destroyJoint(true); } @@ -221,6 +223,10 @@ void World::SayGoodbye(b2Joint *joint) World::World() : world(nullptr) , destructWorld(false) + , begin(this) + , end(this) + , presolve(this) + , postsolve(this) { world = new b2World(b2Vec2(0,0)); world->SetAllowSleeping(true); @@ -229,12 +235,16 @@ World::World() world->SetDestructionListener(this); b2BodyDef def; groundBody = world->CreateBody(&def); - Memoizer::add(world, this); + registerObject(world, this); } World::World(b2Vec2 gravity, bool sleep) : world(nullptr) , destructWorld(false) + , begin(this) + , end(this) + , presolve(this) + , postsolve(this) { world = new b2World(Physics::scaleDown(gravity)); world->SetAllowSleeping(sleep); @@ -243,7 +253,7 @@ World::World(b2Vec2 gravity, bool sleep) world->SetDestructionListener(this); b2BodyDef def; groundBody = world->CreateBody(&def); - Memoizer::add(world, this); + registerObject(world, this); } World::~World() @@ -297,8 +307,8 @@ void World::EndContact(b2Contact *contact) end.process(contact); // Letting the Contact know that the b2Contact will be destroyed any second. - Contact *c = (Contact *)Memoizer::find(contact); - if (c != NULL) + Contact *c = (Contact *)findObject(contact); + if (c != nullptr) c->invalidate(); } @@ -316,8 +326,8 @@ void World::PostSolve(b2Contact *contact, const b2ContactImpulse *impulse) bool World::ShouldCollide(b2Fixture *fixtureA, b2Fixture *fixtureB) { // Fixtures should be memoized, if we created them - Fixture *a = (Fixture *)Memoizer::find(fixtureA); - Fixture *b = (Fixture *)Memoizer::find(fixtureB); + Fixture *a = (Fixture *)findObject(fixtureA); + Fixture *b = (Fixture *)findObject(fixtureB); if (!a || !b) throw love::Exception("A fixture has escaped Memoizer!"); return filter.process(a, b); @@ -472,7 +482,7 @@ int World::getBodies(lua_State *L) const break; if (b == groundBody) continue; - Body *body = (Body *)Memoizer::find(b); + Body *body = (Body *)findObject(b); if (!body) throw love::Exception("A body has escaped Memoizer!"); luax_pushtype(L, body); @@ -491,7 +501,7 @@ int World::getJoints(lua_State *L) const do { if (!j) break; - Joint *joint = (Joint *)Memoizer::find(j); + Joint *joint = (Joint *)findObject(j); if (!joint) throw love::Exception("A joint has escaped Memoizer!"); luax_pushtype(L, joint); lua_rawseti(L, -2, i); @@ -501,7 +511,7 @@ int World::getJoints(lua_State *L) const return 1; } -int World::getContacts(lua_State *L) const +int World::getContacts(lua_State *L) { lua_newtable(L); b2Contact *c = world->GetContactList(); @@ -509,9 +519,9 @@ int World::getContacts(lua_State *L) const do { if (!c) break; - Contact *contact = (Contact *)Memoizer::find(c); + Contact *contact = (Contact *)findObject(c); if (!contact) - contact = new Contact(c); + contact = new Contact(this, c); else contact->retain(); luax_pushtype(L, contact); @@ -538,7 +548,7 @@ int World::queryBoundingBox(lua_State *L) box.lowerBound = Physics::scaleDown(b2Vec2(lx, ly)); box.upperBound = Physics::scaleDown(b2Vec2(ux, uy)); luaL_checktype(L, 5, LUA_TFUNCTION); - QueryCallback query(L, 5); + QueryCallback query(this, L, 5); world->QueryAABB(&query, box); return 0; } @@ -552,7 +562,7 @@ int World::rayCast(lua_State *L) b2Vec2 v1 = Physics::scaleDown(b2Vec2(x1, y1)); b2Vec2 v2 = Physics::scaleDown(b2Vec2(x2, y2)); luaL_checktype(L, 5, LUA_TFUNCTION); - RayCastCallback raycast(L, 5); + RayCastCallback raycast(this, L, 5); world->RayCast(&raycast, v1, v2); return 0; } @@ -586,19 +596,38 @@ void World::destroy() b = b->GetNext(); if (t == groundBody) continue; - Body *body = (Body *)Memoizer::find(t); + Body *body = (Body *)findObject(t); if (!body) throw love::Exception("A body has escaped Memoizer!"); body->destroy(); } world->DestroyBody(groundBody); - Memoizer::remove(world); + unregisterObject(world); delete world; world = nullptr; } +void World::registerObject(void *b2object, love::Object *object) +{ + box2dObjectMap[b2object] = object; +} + +void World::unregisterObject(void *b2object) +{ + box2dObjectMap.erase(b2object); +} + +love::Object *World::findObject(void *b2object) const +{ + auto it = box2dObjectMap.find(b2object); + if (it != box2dObjectMap.end()) + return it->second; + else + return nullptr; +} + } // box2d } // physics } // love diff --git a/src/modules/physics/box2d/World.h b/src/modules/physics/box2d/World.h index 83a39a777..540161dc5 100644 --- a/src/modules/physics/box2d/World.h +++ b/src/modules/physics/box2d/World.h @@ -28,6 +28,7 @@ // STD #include +#include // Box2D #include @@ -73,7 +74,8 @@ public: public: Reference *ref; lua_State *L; - ContactCallback(); + World *world; + ContactCallback(World *world); ~ContactCallback(); void process(b2Contact *contact, const b2ContactImpulse *impulse = NULL); }; @@ -91,10 +93,11 @@ public: class QueryCallback : public b2QueryCallback { public: - QueryCallback(lua_State *L, int idx); + QueryCallback(World *world, lua_State *L, int idx); ~QueryCallback(); virtual bool ReportFixture(b2Fixture *fixture); private: + World *world; lua_State *L; int funcidx; }; @@ -102,10 +105,11 @@ public: class RayCastCallback : public b2RayCastCallback { public: - RayCastCallback(lua_State *L, int idx); + RayCastCallback(World *world, lua_State *L, int idx); ~RayCastCallback(); virtual float32 ReportFixture(b2Fixture *fixture, const b2Vec2 &point, const b2Vec2 &normal, float32 fraction); private: + World *world; lua_State *L; int funcidx; }; @@ -257,7 +261,7 @@ public: * Get an array of all the Contacts in the World. * @return An array of Contacts. **/ - int getContacts(lua_State *L) const; + int getContacts(lua_State *L); /** * Gets the ground body. @@ -280,6 +284,10 @@ public: **/ void destroy(); + void registerObject(void *b2object, love::Object *object); + void unregisterObject(void *b2object); + love::Object *findObject(void *b2object) const; + private: // Pointer to the Box2D world. @@ -297,7 +305,10 @@ private: // Contact callbacks. ContactCallback begin, end, presolve, postsolve; ContactFilter filter; -}; + + std::unordered_map box2dObjectMap; + +}; // World } // box2d } // physics