From eb069a9fef5ed668088136695ebf72547f11b54a Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 30 Oct 2019 23:12:18 +0800 Subject: [PATCH] Update physics --- src/kiwano-physics/Body.cpp | 166 +++++++++++------- src/kiwano-physics/Body.h | 102 +++++++---- src/kiwano-physics/Fixture.cpp | 20 +-- src/kiwano-physics/Fixture.h | 14 +- src/kiwano-physics/Joint.cpp | 226 ++++++++++++------------ src/kiwano-physics/Joint.h | 245 +++++++++++++-------------- src/kiwano-physics/Shape.cpp | 80 ++++----- src/kiwano-physics/Shape.h | 60 +++---- src/kiwano-physics/World.cpp | 53 +++--- src/kiwano-physics/World.h | 28 ++- src/kiwano/2d/Actor.h | 40 ++--- src/kiwano/2d/Text.h | 6 +- src/kiwano/2d/action/Action.h | 4 +- src/kiwano/2d/action/ActionGroup.h | 2 +- src/kiwano/2d/action/ActionManager.h | 2 +- src/kiwano/base/EventDispatcher.h | 2 +- src/kiwano/base/EventListener.h | 4 +- src/kiwano/base/ObjectBase.h | 2 +- src/kiwano/base/RefCounter.hpp | 2 +- src/kiwano/base/SmartPtr.hpp | 2 +- src/kiwano/base/Timer.h | 4 +- src/kiwano/base/TimerManager.h | 2 +- src/kiwano/base/win32/ComPtr.hpp | 2 +- src/kiwano/core/any.hpp | 2 +- src/kiwano/core/basic_json.hpp | 4 +- src/kiwano/core/core.h | 9 + src/kiwano/core/function.hpp | 18 +- src/kiwano/core/intrusive_list.hpp | 8 +- src/kiwano/core/intrusive_ptr.hpp | 4 +- src/kiwano/core/noncopyable.hpp | 4 +- src/kiwano/core/singleton.hpp | 18 +- src/kiwano/core/string.hpp | 26 +-- src/kiwano/core/vector.hpp | 4 +- src/kiwano/math/scalar.h | 32 ++-- src/kiwano/platform/Application.h | 2 +- src/kiwano/renderer/Geometry.h | 2 +- src/kiwano/renderer/RenderTarget.h | 2 +- src/kiwano/utils/ResourceCache.cpp | 4 +- 38 files changed, 653 insertions(+), 554 deletions(-) diff --git a/src/kiwano-physics/Body.cpp b/src/kiwano-physics/Body.cpp index 15e5e899..44dd0783 100644 --- a/src/kiwano-physics/Body.cpp +++ b/src/kiwano-physics/Body.cpp @@ -26,84 +26,83 @@ namespace kiwano namespace physics { - Body::Body() - : ignore_rotation_(false) - , body_(nullptr) + PhysicBody::PhysicBody() + : body_(nullptr) , actor_(nullptr) , world_(nullptr) { } - Body::Body(b2Body* body, Actor* actor) - : Body() + PhysicBody::PhysicBody(b2Body* body, Actor* actor) + : PhysicBody() { SetB2Body(body); SetActor(actor); } - Body::~Body() + PhysicBody::PhysicBody(PhysicWorld* world, Actor* actor) + : PhysicBody() { - if (world_) - { - world_->RemoveBody(this); - } + Init(world, actor); } - BodyPtr Body::Create(World* world, Actor* actor) + PhysicBody::~PhysicBody() { - BodyPtr body = new Body; - if (body) - { - body->world_ = world; - if (world) - { - b2BodyDef def; - b2Body* b2body = world->GetB2World()->CreateBody(&def); - body->SetB2Body(b2body); - } - body->SetActor(actor); - body->UpdateFromActor(); - } - return body; + Destroy(); } - Fixture Body::AddShape(Shape* shape, float density, float friction, float restitution) + void PhysicBody::Init(PhysicWorld* world, Actor* actor) + { + KGE_ASSERT(world); + + Destroy(); + + world_ = world; + b2BodyDef def; + b2Body* b2body = world->GetB2World()->CreateBody(&def); + + SetB2Body(b2body); + SetActor(actor); + UpdateFromActor(); + } + + PhysicFixture PhysicBody::AddShape(PhysicShape* shape, float density, float friction, float restitution) { KGE_ASSERT(body_ && world_); if (shape) { - return Fixture::Create(this, shape, density, friction, restitution); + return PhysicFixture::Create(this, shape, density, friction, restitution); } - return Fixture(); + return PhysicFixture(); } - Fixture Body::AddCircleShape(float radius, float density) + PhysicFixture PhysicBody::AddCircleShape(float radius, float density) { - return AddShape(&CircleShape(radius), density); + return AddShape(&PhysicCircleShape(radius), density); } - Fixture Body::AddBoxShape(Vec2 const& size, float density) + PhysicFixture PhysicBody::AddBoxShape(Vec2 const& size, float density) { - return AddShape(&BoxShape(size), density); + return AddShape(&PhysicBoxShape(size), density); } - Fixture Body::AddPolygonShape(Vector const& vertexs, float density) + PhysicFixture PhysicBody::AddPolygonShape(Vector const& vertexs, float density) { - return AddShape(&PolygonShape(vertexs), density); + return AddShape(&PhysicPolygonShape(vertexs), density); } - Fixture Body::AddEdgeShape(Point const& p1, Point const& p2, float density) + PhysicFixture PhysicBody::AddEdgeShape(Point const& p1, Point const& p2, float density) { - return AddShape(&EdgeShape(p1, p2), density); + return AddShape(&PhysicEdgeShape(p1, p2), density); } - Fixture Body::AddChainShape(Vector const& vertexs, bool loop, float density) + PhysicFixture PhysicBody::AddChainShape(Vector const& vertexs, bool loop, float density) { - return AddShape(&ChainShape(vertexs, loop), density); + return AddShape(&PhysicChainShape(vertexs, loop), density); } - void Body::RemoveFixture(Fixture const& fixture) + void PhysicBody::RemoveFixture(PhysicFixture const& fixture) { if (fixture.GetB2Fixture()) { @@ -112,43 +111,90 @@ namespace kiwano } } - Point Body::GetPosition() const + void PhysicBody::GetMassData(float* mass, Point* center, float* inertia) + { + KGE_ASSERT(body_ && world_); + + b2MassData data; + body_->GetMassData(&data); + + if (mass) *mass = data.mass; + if (center) *center = world_->World2Stage(data.center); + if (inertia) *inertia = data.I; + } + + void PhysicBody::SetMassData(float mass, Point const& center, float inertia) + { + KGE_ASSERT(body_ && world_); + + b2MassData data; + data.mass = mass; + data.center = world_->Stage2World(center); + data.I = inertia; + body_->SetMassData(&data); + } + + void PhysicBody::ResetMassData() + { + KGE_ASSERT(body_); + body_->ResetMassData(); + } + + Point PhysicBody::GetBodyPosition() const { KGE_ASSERT(body_ && world_); return world_->World2Stage(body_->GetPosition()); } - Point Body::GetLocalPoint(Point const& world) const + void PhysicBody::SetBodyTransform(Point const& pos, float angle) + { + KGE_ASSERT(body_ && world_); + body_->SetTransform(world_->Stage2World(pos), math::Degree2Radian(angle)); + } + + Point PhysicBody::GetLocalPoint(Point const& world) const { KGE_ASSERT(body_ && world_); return world_->World2Stage(body_->GetLocalPoint(world_->Stage2World(world))); } - Point Body::GetWorldPoint(Point const& local) const + Point PhysicBody::GetWorldPoint(Point const& local) const { KGE_ASSERT(body_ && world_); return world_->World2Stage(body_->GetWorldPoint(world_->Stage2World(local))); } - void Body::ApplyForce(Vec2 const& force, Point const& point, bool wake) + Point PhysicBody::GetLocalCenter() const + { + KGE_ASSERT(body_ && world_); + return world_->World2Stage(body_->GetLocalCenter()); + } + + Point PhysicBody::GetWorldCenter() const + { + KGE_ASSERT(body_ && world_); + return world_->World2Stage(body_->GetWorldCenter()); + } + + void PhysicBody::ApplyForce(Vec2 const& force, Point const& point, bool wake) { KGE_ASSERT(body_ && world_); body_->ApplyForce(b2Vec2(force.x, force.y), world_->Stage2World(point), wake); } - void Body::ApplyForceToCenter(Vec2 const& force, bool wake) + void PhysicBody::ApplyForceToCenter(Vec2 const& force, bool wake) { KGE_ASSERT(body_ && world_); body_->ApplyForceToCenter(b2Vec2(force.x, force.y), wake); } - void Body::ApplyTorque(float torque, bool wake) + void PhysicBody::ApplyTorque(float torque, bool wake) { KGE_ASSERT(body_ && world_); body_->ApplyTorque(torque, wake); } - void Body::SetB2Body(b2Body* body) + void PhysicBody::SetB2Body(b2Body* body) { body_ = body; if (body_) @@ -157,7 +203,19 @@ namespace kiwano } } - void Body::UpdateActor() + void PhysicBody::Destroy() + { + if (world_) + { + world_->RemoveBody(this); + } + + body_ = nullptr; + world_ = nullptr; + actor_ = nullptr; + } + + void PhysicBody::UpdateActor() { if (actor_ && body_) { @@ -169,32 +227,26 @@ namespace kiwano { actor_->SetPosition(World2Stage(body_->GetPosition())); } - - if (!ignore_rotation_) - { - actor_->SetRotation(math::Radian2Angle(body_->GetAngle())); - } + actor_->SetRotation(math::Radian2Degree(body_->GetAngle())); } } - void Body::UpdateFromActor() + void PhysicBody::UpdateFromActor() { if (actor_ && body_) { - float rotation = ignore_rotation_ ? body_->GetAngle() : math::Angle2Radian(actor_->GetRotation()); - if (world_) { body_->SetTransform( world_->Stage2World(actor_->GetPosition()), - rotation + math::Degree2Radian(actor_->GetRotation()) ); } else { body_->SetTransform( Stage2World(actor_->GetPosition()), - rotation + math::Degree2Radian(actor_->GetRotation()) ); } } diff --git a/src/kiwano-physics/Body.h b/src/kiwano-physics/Body.h index 3f1aca0d..079b900c 100644 --- a/src/kiwano-physics/Body.h +++ b/src/kiwano-physics/Body.h @@ -27,12 +27,12 @@ namespace kiwano { namespace physics { - class World; + class PhysicWorld; - // 刚体 - KGE_DECLARE_SMART_PTR(Body); - class KGE_API Body - : public ObjectBase + // 物体 + KGE_DECLARE_SMART_PTR(PhysicBody); + class KGE_API PhysicBody + : public virtual RefCounter { public: enum class Type @@ -42,48 +42,91 @@ namespace kiwano Dynamic, }; - Body(); - Body(b2Body* body, Actor* actor); - virtual ~Body(); + PhysicBody(); + PhysicBody(b2Body* body, Actor* actor); + PhysicBody(PhysicWorld* world, Actor* actor); + PhysicBody(PhysicWorld* world, ActorPtr actor) : PhysicBody(world, actor.get()) {} + virtual ~PhysicBody(); - static BodyPtr Create(World* world, Actor* actor); + // 初始化 + void Init(PhysicWorld* world, Actor* actor); // 添加形状 - Fixture AddShape(Shape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); - Fixture AddCircleShape(float radius, float density = 0.f); - Fixture AddBoxShape(Vec2 const& size, float density = 0.f); - Fixture AddPolygonShape(Vector const& vertexs, float density = 0.f); - Fixture AddEdgeShape(Point const& p1, Point const& p2, float density = 0.f); - Fixture AddChainShape(Vector const& vertexs, bool loop, float density = 0.f); + PhysicFixture AddShape(PhysicShape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); + PhysicFixture AddCircleShape(float radius, float density = 0.f); + PhysicFixture AddBoxShape(Vec2 const& size, float density = 0.f); + PhysicFixture AddPolygonShape(Vector const& vertexs, float density = 0.f); + PhysicFixture AddEdgeShape(Point const& p1, Point const& p2, float density = 0.f); + PhysicFixture AddChainShape(Vector const& vertexs, bool loop, float density = 0.f); - // 获取夹具列表 - Fixture GetFixture() const { KGE_ASSERT(body_); Fixture(body_->GetFixtureList()); } + // 获取夹具 + PhysicFixture GetFixture() const { KGE_ASSERT(body_); PhysicFixture(body_->GetFixtureList()); } // 移除夹具 - void RemoveFixture(Fixture const& fixture); + void RemoveFixture(PhysicFixture const& fixture); - // 获取质量 + // 旋转角度 + float GetBodyRotation() const { KGE_ASSERT(body_); return math::Radian2Degree(body_->GetAngle()); } + void SetBodyRotation(float angle) { SetBodyTransform(GetBodyPosition(), angle); } + + // 位置 + Point GetBodyPosition() const; + void SetBodyPosition(Point const& pos) { SetBodyTransform(pos, GetBodyRotation()); } + + // 位置和旋转变换 + void SetBodyTransform(Point const& pos, float angle); + + // 质量 float GetMass() const { KGE_ASSERT(body_); return body_->GetMass(); } - // 获取旋转角度 - float GetRotation() const { KGE_ASSERT(body_); return math::Radian2Angle(body_->GetAngle()); } + // 惯性 + float GetInertia() const { KGE_ASSERT(body_); return body_->GetInertia(); } - // 获取位置 - Point GetPosition() const; + // 质量数据 + void GetMassData(float* mass, Point* center, float* inertia); + void SetMassData(float mass, Point const& center, float inertia); + void ResetMassData(); + // 坐标转换 Point GetLocalPoint(Point const& world) const; Point GetWorldPoint(Point const& local) const; + // 质心坐标 + Point GetLocalCenter() const; + Point GetWorldCenter() const; + + // 物体类型 Type GetType() const { KGE_ASSERT(body_); return Type(body_->GetType()); } void SetType(Type type) { KGE_ASSERT(body_); body_->SetType(static_cast(type)); } + // 重力因子 + float GetGravityScale() const { KGE_ASSERT(body_); return body_->GetGravityScale(); } + void SetGravityScale(float scale) { KGE_ASSERT(body_); body_->SetGravityScale(scale); } + + // 施力 void ApplyForce(Vec2 const& force, Point const& point, bool wake = true); void ApplyForceToCenter(Vec2 const& force, bool wake = true); + // 施加扭矩 void ApplyTorque(float torque, bool wake = true); - bool IsIgnoreRotation() const { return ignore_rotation_; } - void SetIgnoreRotation(bool ignore_rotation) { ignore_rotation_ = ignore_rotation; } + // 固定旋转 + bool IsIgnoreRotation() const { KGE_ASSERT(body_); return body_->IsFixedRotation(); } + void SetIgnoreRotation(bool flag) { KGE_ASSERT(body_); body_->SetFixedRotation(flag); } + + // 子弹 + bool IsBullet() const { KGE_ASSERT(body_); return body_->IsBullet(); } + void SetBullet(bool flag) { KGE_ASSERT(body_); body_->SetBullet(flag); } + + // 休眠 + bool IsAwake() const { KGE_ASSERT(body_); return body_->IsAwake(); } + void SetAwake(bool flag) { KGE_ASSERT(body_); body_->SetAwake(flag); } + bool IsSleepingAllowed() const { KGE_ASSERT(body_); return body_->IsSleepingAllowed(); } + void SetSleepingAllowed(bool flag) { KGE_ASSERT(body_); body_->SetSleepingAllowed(flag); } + + // 活动状态 + bool IsActive() const { KGE_ASSERT(body_); return body_->IsActive(); } + void SetActive(bool flag) { KGE_ASSERT(body_); body_->SetActive(flag); } Actor* GetActor() const { return actor_; } void SetActor(Actor* actor) { actor_ = actor; } @@ -92,16 +135,17 @@ namespace kiwano const b2Body* GetB2Body() const { return body_; } void SetB2Body(b2Body* body); - World* GetWorld() { return world_; } - const World* GetWorld() const { return world_; } + PhysicWorld* GetWorld() { return world_; } + const PhysicWorld* GetWorld() const { return world_; } + + void Destroy(); void UpdateActor(); void UpdateFromActor(); protected: - bool ignore_rotation_; Actor* actor_; - World* world_; + PhysicWorld* world_; b2Body* body_; }; } diff --git a/src/kiwano-physics/Fixture.cpp b/src/kiwano-physics/Fixture.cpp index a549f144..c238b5ff 100644 --- a/src/kiwano-physics/Fixture.cpp +++ b/src/kiwano-physics/Fixture.cpp @@ -27,18 +27,18 @@ namespace kiwano namespace physics { - Fixture::Fixture() + PhysicFixture::PhysicFixture() : fixture_(nullptr) { } - Fixture::Fixture(b2Fixture* fixture) - : Fixture() + PhysicFixture::PhysicFixture(b2Fixture* fixture) + : PhysicFixture() { SetB2Fixture(fixture); } - Fixture Fixture::Create(Body* body, Shape* shape, float density, float friction, float restitution) + PhysicFixture PhysicFixture::Create(PhysicBody* body, PhysicShape* shape, float density, float friction, float restitution) { KGE_ASSERT(body); @@ -53,21 +53,21 @@ namespace kiwano fd.restitution = restitution; fd.shape = shape->GetB2Shape(); auto fixture = b2body->CreateFixture(&fd); - return Fixture(fixture); + return PhysicFixture(fixture); } - return Fixture(); + return PhysicFixture(); } - Shape Fixture::GetShape() const + PhysicShape PhysicFixture::GetShape() const { KGE_ASSERT(fixture_); - return Shape(fixture_->GetShape()); + return PhysicShape(fixture_->GetShape()); } - Fixture Fixture::GetNext() const + PhysicFixture PhysicFixture::GetNext() const { KGE_ASSERT(fixture_); - return Fixture(fixture_->GetNext()); + return PhysicFixture(fixture_->GetNext()); } } diff --git a/src/kiwano-physics/Fixture.h b/src/kiwano-physics/Fixture.h index 6ea5a85f..7e5e5911 100644 --- a/src/kiwano-physics/Fixture.h +++ b/src/kiwano-physics/Fixture.h @@ -26,19 +26,19 @@ namespace kiwano { namespace physics { - class Body; + class PhysicBody; // 夹具 - class Fixture + class PhysicFixture { public: - Fixture(); - Fixture(b2Fixture* fixture); + PhysicFixture(); + PhysicFixture(b2Fixture* fixture); - static Fixture Create(Body* body, Shape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); + static PhysicFixture Create(PhysicBody* body, PhysicShape* shape, float density = 0.f, float friction = 0.2f, float restitution = 0.f); - Shape GetShape() const; - Fixture GetNext() const; + PhysicShape GetShape() const; + PhysicFixture GetNext() const; bool IsValid() const { return !!fixture_; } diff --git a/src/kiwano-physics/Joint.cpp b/src/kiwano-physics/Joint.cpp index 8a91bfbb..e32a2598 100644 --- a/src/kiwano-physics/Joint.cpp +++ b/src/kiwano-physics/Joint.cpp @@ -26,24 +26,37 @@ namespace kiwano namespace physics { // - // Joint + // PhysicJoint // - Joint::Joint() + PhysicJoint::PhysicJoint() : joint_(nullptr) , world_(nullptr) , type_(Type::Unknown) { } - Joint::Joint(b2Joint* joint) - : Joint() + PhysicJoint::PhysicJoint(b2Joint* joint) + : PhysicJoint() { SetB2Joint(joint); } - Joint::Joint(World* world, b2JointDef* joint_def) - : Joint() + PhysicJoint::PhysicJoint(PhysicWorld* world, b2JointDef* joint_def) + : PhysicJoint() + { + Init(world, joint_def); + } + + PhysicJoint::~PhysicJoint() + { + if (world_) + { + world_->RemoveJoint(this); + } + } + + void PhysicJoint::Init(PhysicWorld* world, b2JointDef* joint_def) { world_ = world; if (world_) @@ -55,36 +68,28 @@ namespace kiwano } } - Joint::~Joint() - { - if (world_) - { - world_->RemoveJoint(this); - } - } - - BodyPtr Joint::GetBodyA() const + PhysicBodyPtr PhysicJoint::GetBodyA() const { KGE_ASSERT(joint_); b2Body* body = joint_->GetBodyA(); - return BodyPtr(static_cast(body->GetUserData())); + return PhysicBodyPtr(static_cast(body->GetUserData())); } - BodyPtr Joint::GetBodyB() const + PhysicBodyPtr PhysicJoint::GetBodyB() const { KGE_ASSERT(joint_); b2Body* body = joint_->GetBodyB(); - return BodyPtr(static_cast(body->GetUserData())); + return PhysicBodyPtr(static_cast(body->GetUserData())); } - void Joint::SetB2Joint(b2Joint* joint) + void PhysicJoint::SetB2Joint(b2Joint* joint) { joint_ = joint; if (joint_) { - type_ = Joint::Type(joint_->GetType()); + type_ = PhysicJoint::Type(joint_->GetType()); } } @@ -93,18 +98,20 @@ namespace kiwano // DistanceJoint::DistanceJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - DistanceJoint::DistanceJoint(World* world, b2DistanceJointDef* def) - : Joint(world, def) + DistanceJoint::DistanceJoint(PhysicWorld* world, b2DistanceJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - DistanceJointPtr DistanceJoint::Create(World* world, DistanceJoint::Param const& param) + DistanceJoint::DistanceJoint(PhysicWorld* world, DistanceJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -113,9 +120,8 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - DistanceJointPtr joint = new DistanceJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } void DistanceJoint::SetLength(float length) @@ -135,18 +141,20 @@ namespace kiwano // FrictionJoint::FrictionJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - FrictionJoint::FrictionJoint(World* world, b2FrictionJointDef* def) - : Joint(world, def) + FrictionJoint::FrictionJoint(PhysicWorld* world, b2FrictionJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - FrictionJointPtr FrictionJoint::Create(World* world, FrictionJoint::Param const& param) + FrictionJoint::FrictionJoint(PhysicWorld* world, FrictionJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -155,9 +163,8 @@ namespace kiwano def.maxForce = param.max_force; def.maxTorque = world->Stage2World(param.max_torque); - FrictionJointPtr joint = new FrictionJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } void FrictionJoint::SetMaxForce(float length) @@ -189,18 +196,20 @@ namespace kiwano // GearJoint::GearJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - GearJoint::GearJoint(World* world, b2GearJointDef* def) - : Joint(world, def) + GearJoint::GearJoint(PhysicWorld* world, b2GearJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - GearJointPtr GearJoint::Create(World* world, GearJoint::Param const& param) + GearJoint::GearJoint(PhysicWorld* world, GearJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.joint_a && param.joint_b); @@ -209,9 +218,8 @@ namespace kiwano def.joint2 = param.joint_b->GetB2Joint(); def.ratio = param.ratio; - GearJointPtr joint = new GearJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } void GearJoint::SetRatio(float ratio) @@ -231,18 +239,20 @@ namespace kiwano // MotorJoint::MotorJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - MotorJoint::MotorJoint(World* world, b2MotorJointDef* def) - : Joint(world, def) + MotorJoint::MotorJoint(PhysicWorld* world, b2MotorJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - MotorJointPtr MotorJoint::Create(World* world, MotorJoint::Param const& param) + MotorJoint::MotorJoint(PhysicWorld* world, MotorJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -251,9 +261,8 @@ namespace kiwano def.maxForce = param.max_force; def.maxTorque = world->Stage2World(param.max_torque); - MotorJointPtr joint = new MotorJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } void MotorJoint::SetMaxForce(float length) @@ -285,18 +294,20 @@ namespace kiwano // PrismaticJoint::PrismaticJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - PrismaticJoint::PrismaticJoint(World* world, b2PrismaticJointDef* def) - : Joint(world, def) + PrismaticJoint::PrismaticJoint(PhysicWorld* world, b2PrismaticJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - PrismaticJointPtr PrismaticJoint::Create(World* world, PrismaticJoint::Param const& param) + PrismaticJoint::PrismaticJoint(PhysicWorld* world, PrismaticJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -309,9 +320,8 @@ namespace kiwano def.maxMotorForce = param.max_motor_force; def.motorSpeed = world->Stage2World(param.motor_speed); - PrismaticJointPtr joint = new PrismaticJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } float PrismaticJoint::GetJointTranslation() const @@ -349,18 +359,20 @@ namespace kiwano // PulleyJoint::PulleyJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - PulleyJoint::PulleyJoint(World* world, b2PulleyJointDef* def) - : Joint(world, def) + PulleyJoint::PulleyJoint(PhysicWorld* world, b2PulleyJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - PulleyJointPtr PulleyJoint::Create(World* world, PulleyJoint::Param const& param) + PulleyJoint::PulleyJoint(PhysicWorld* world, PulleyJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -368,9 +380,8 @@ namespace kiwano def.Initialize(param.body_a->GetB2Body(), param.body_b->GetB2Body(), world->Stage2World(param.ground_anchor_a), world->Stage2World(param.ground_anchor_b), world->Stage2World(param.anchor_a), world->Stage2World(param.anchor_b), param.ratio); - PulleyJointPtr joint = new PulleyJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } Point PulleyJoint::GetGroundAnchorA() const @@ -420,63 +431,64 @@ namespace kiwano // RevoluteJoint::RevoluteJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - RevoluteJoint::RevoluteJoint(World* world, b2RevoluteJointDef* def) - : Joint(world, def) + RevoluteJoint::RevoluteJoint(PhysicWorld* world, b2RevoluteJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - RevoluteJointPtr RevoluteJoint::Create(World* world, RevoluteJoint::Param const& param) + RevoluteJoint::RevoluteJoint(PhysicWorld* world, RevoluteJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); b2RevoluteJointDef def; def.Initialize(param.body_a->GetB2Body(), param.body_b->GetB2Body(), world->Stage2World(param.anchor)); def.enableLimit = param.enable_limit; - def.lowerAngle = math::Angle2Radian(param.lower_angle); - def.upperAngle = math::Angle2Radian(param.upper_angle); + def.lowerAngle = math::Degree2Radian(param.lower_angle); + def.upperAngle = math::Degree2Radian(param.upper_angle); def.enableMotor = param.enable_motor; def.maxMotorTorque = world->Stage2World(param.max_motor_torque); - def.motorSpeed = math::Angle2Radian(param.motor_speed); + def.motorSpeed = math::Degree2Radian(param.motor_speed); - RevoluteJointPtr joint = new RevoluteJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } float RevoluteJoint::GetJointAngle() const { KGE_ASSERT(raw_joint_ && world_); - return math::Radian2Angle(raw_joint_->GetJointAngle()); + return math::Radian2Degree(raw_joint_->GetJointAngle()); } float RevoluteJoint::GetJointSpeed() const { KGE_ASSERT(raw_joint_ && world_); - return math::Radian2Angle(raw_joint_->GetJointSpeed()); + return math::Radian2Degree(raw_joint_->GetJointSpeed()); } float RevoluteJoint::GetLowerLimit() const { KGE_ASSERT(raw_joint_ && world_); - return math::Radian2Angle(raw_joint_->GetLowerLimit()); + return math::Radian2Degree(raw_joint_->GetLowerLimit()); } float RevoluteJoint::GetUpperLimit() const { KGE_ASSERT(raw_joint_ && world_); - return math::Radian2Angle(raw_joint_->GetUpperLimit()); + return math::Radian2Degree(raw_joint_->GetUpperLimit()); } void RevoluteJoint::SetLimits(float lower, float upper) { KGE_ASSERT(raw_joint_ && world_); - raw_joint_->SetLimits(math::Angle2Radian(lower), math::Angle2Radian(upper)); + raw_joint_->SetLimits(math::Degree2Radian(lower), math::Degree2Radian(upper)); } void RevoluteJoint::SetMaxMotorTorque(float torque) @@ -496,18 +508,20 @@ namespace kiwano // RopeJoint::RopeJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - RopeJoint::RopeJoint(World* world, b2RopeJointDef* def) - : Joint(world, def) + RopeJoint::RopeJoint(PhysicWorld* world, b2RopeJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - RopeJointPtr RopeJoint::Create(World* world, RopeJoint::Param const& param) + RopeJoint::RopeJoint(PhysicWorld* world, RopeJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -518,9 +532,8 @@ namespace kiwano def.localAnchorB = world->Stage2World(param.local_anchor_b); def.maxLength = world->Stage2World(param.max_length); - RopeJointPtr joint = new RopeJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } void RopeJoint::SetMaxLength(float length) @@ -540,18 +553,20 @@ namespace kiwano // WeldJoint::WeldJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - WeldJoint::WeldJoint(World* world, b2WeldJointDef* def) - : Joint(world, def) + WeldJoint::WeldJoint(PhysicWorld* world, b2WeldJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - WeldJointPtr WeldJoint::Create(World* world, WeldJoint::Param const& param) + WeldJoint::WeldJoint(PhysicWorld* world, WeldJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -560,9 +575,8 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - WeldJointPtr joint = new WeldJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } // @@ -570,18 +584,20 @@ namespace kiwano // WheelJoint::WheelJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - WheelJoint::WheelJoint(World* world, b2WheelJointDef* def) - : Joint(world, def) + WheelJoint::WheelJoint(PhysicWorld* world, b2WheelJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - WheelJointPtr WheelJoint::Create(World* world, WheelJoint::Param const& param) + WheelJoint::WheelJoint(PhysicWorld* world, WheelJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -593,9 +609,8 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - WheelJointPtr joint = new WheelJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } float WheelJoint::GetJointTranslation() const @@ -627,18 +642,20 @@ namespace kiwano // MouseJoint::MouseJoint() - : Joint() + : PhysicJoint() , raw_joint_(nullptr) { } - MouseJoint::MouseJoint(World* world, b2MouseJointDef* def) - : Joint(world, def) + MouseJoint::MouseJoint(PhysicWorld* world, b2MouseJointDef* def) + : PhysicJoint(world, def) , raw_joint_(nullptr) { } - MouseJointPtr MouseJoint::Create(World* world, MouseJoint::Param const& param) + MouseJoint::MouseJoint(PhysicWorld* world, MouseJoint::Param const& param) + : PhysicJoint() + , raw_joint_(nullptr) { KGE_ASSERT(param.body_a && param.body_b); @@ -650,9 +667,8 @@ namespace kiwano def.frequencyHz = param.frequency_hz; def.dampingRatio = param.damping_ratio; - MouseJointPtr joint = new MouseJoint(world, &def); - joint->raw_joint_ = static_cast(joint->GetB2Joint()); - return joint; + Init(world, &def); + raw_joint_ = static_cast(GetB2Joint()); } void MouseJoint::SetMaxForce(float length) diff --git a/src/kiwano-physics/Joint.h b/src/kiwano-physics/Joint.h index 861cd6ad..e2a8b0df 100644 --- a/src/kiwano-physics/Joint.h +++ b/src/kiwano-physics/Joint.h @@ -26,7 +26,7 @@ namespace kiwano { namespace physics { - KGE_DECLARE_SMART_PTR(Joint); + KGE_DECLARE_SMART_PTR(PhysicJoint); KGE_DECLARE_SMART_PTR(DistanceJoint); KGE_DECLARE_SMART_PTR(FrictionJoint); KGE_DECLARE_SMART_PTR(GearJoint); @@ -40,8 +40,8 @@ namespace kiwano KGE_DECLARE_SMART_PTR(WheelJoint); // 关节 - class KGE_API Joint - : public ObjectBase + class KGE_API PhysicJoint + : public virtual RefCounter { public: enum class Type @@ -62,41 +62,43 @@ namespace kiwano struct ParamBase { - Body* body_a; - Body* body_b; + PhysicBody* body_a; + PhysicBody* body_b; - ParamBase(Body* body_a, Body* body_b) : body_a(body_a), body_b(body_b) {} - ParamBase(BodyPtr body_a, BodyPtr body_b) : body_a(body_a.get()), body_b(body_b.get()) {} + ParamBase(PhysicBody* body_a, PhysicBody* body_b) : body_a(body_a), body_b(body_b) {} + ParamBase(PhysicBodyPtr body_a, PhysicBodyPtr body_b) : body_a(body_a.get()), body_b(body_b.get()) {} }; - Joint(); - Joint(b2Joint* joint); - Joint(World* world, b2JointDef* joint_def); - virtual ~Joint(); + PhysicJoint(); + PhysicJoint(b2Joint* joint); + PhysicJoint(PhysicWorld* world, b2JointDef* joint_def); + virtual ~PhysicJoint(); - BodyPtr GetBodyA() const; - BodyPtr GetBodyB() const; + void Init(PhysicWorld* world, b2JointDef* joint_def); + + PhysicBodyPtr GetBodyA() const; + PhysicBodyPtr GetBodyB() const; b2Joint* GetB2Joint() { return joint_; } const b2Joint* GetB2Joint() const { return joint_; } void SetB2Joint(b2Joint* joint); - World* GetWorld() { return world_; } - const World* GetWorld() const { return world_; } + PhysicWorld* GetWorld() { return world_; } + const PhysicWorld* GetWorld() const { return world_; } protected: b2Joint* joint_; - World* world_; + PhysicWorld* world_; Type type_; }; // 固定距离关节 class KGE_API DistanceJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point anchor_a; Point anchor_b; @@ -104,8 +106,8 @@ namespace kiwano float damping_ratio; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& anchor_a, Point const& anchor_b, float frequency_hz = 0.f, @@ -119,8 +121,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& anchor_a, Point const& anchor_b, float frequency_hz = 0.f, @@ -131,9 +133,8 @@ namespace kiwano }; DistanceJoint(); - DistanceJoint(World* world, b2DistanceJointDef* def); - - static DistanceJointPtr Create(World* world, Param const& param); + DistanceJoint(PhysicWorld* world, b2DistanceJointDef* def); + DistanceJoint(PhysicWorld* world, Param const& param); void SetLength(float length); float GetLength() const; @@ -153,18 +154,18 @@ namespace kiwano // 摩擦关节 class KGE_API FrictionJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point anchor; float max_force; float max_torque; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& anchor, float max_force = 0.f, float max_torque = 0.f @@ -176,8 +177,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& anchor, float max_force = 0.f, float max_torque = 0.f @@ -187,9 +188,8 @@ namespace kiwano }; FrictionJoint(); - FrictionJoint(World* world, b2FrictionJointDef* def); - - static FrictionJointPtr Create(World* world, Param const& param); + FrictionJoint(PhysicWorld* world, b2FrictionJointDef* def); + FrictionJoint(PhysicWorld* world, Param const& param); // 设定最大摩擦力 void SetMaxForce(float force); @@ -206,18 +206,18 @@ namespace kiwano // 齿轮关节 class KGE_API GearJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { - JointPtr joint_a; - JointPtr joint_b; + PhysicJointPtr joint_a; + PhysicJointPtr joint_b; float ratio; Param( - Joint* joint_a, - Joint* joint_b, + PhysicJoint* joint_a, + PhysicJoint* joint_b, float ratio = 1.f ) : ParamBase(nullptr, nullptr) @@ -227,8 +227,8 @@ namespace kiwano {} Param( - JointPtr joint_a, - JointPtr joint_b, + PhysicJointPtr joint_a, + PhysicJointPtr joint_b, float ratio = 1.f ) : Param(joint_a.get(), joint_b.get(), ratio) @@ -236,9 +236,8 @@ namespace kiwano }; GearJoint(); - GearJoint(World* world, b2GearJointDef* def); - - static GearJointPtr Create(World* world, Param const& param); + GearJoint(PhysicWorld* world, b2GearJointDef* def); + GearJoint(PhysicWorld* world, Param const& param); // 设定齿轮传动比 void SetRatio(float ratio); @@ -251,18 +250,18 @@ namespace kiwano // 马达关节 class KGE_API MotorJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { float max_force; float max_torque; float correction_factor; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, float max_force = 1.f, float max_torque = 100.f, float correction_factor = 0.3f @@ -274,8 +273,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, float max_force = 0.f, float max_torque = 0.f, float correction_factor = 0.3f @@ -285,9 +284,8 @@ namespace kiwano }; MotorJoint(); - MotorJoint(World* world, b2MotorJointDef* def); - - static MotorJointPtr Create(World* world, Param const& param); + MotorJoint(PhysicWorld* world, b2MotorJointDef* def); + MotorJoint(PhysicWorld* world, Param const& param); // 设定最大摩擦力 void SetMaxForce(float force); @@ -304,10 +302,10 @@ namespace kiwano // 平移关节 class KGE_API PrismaticJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point anchor; Vec2 axis; @@ -319,8 +317,8 @@ namespace kiwano float motor_speed; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& anchor, Vec2 const& axis, bool enable_limit = false, @@ -342,8 +340,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& anchor, Vec2 const& axis, bool enable_limit = false, @@ -358,11 +356,10 @@ namespace kiwano }; PrismaticJoint(); - PrismaticJoint(World* world, b2PrismaticJointDef* def); + PrismaticJoint(PhysicWorld* world, b2PrismaticJointDef* def); + PrismaticJoint(PhysicWorld* world, Param const& param); - static PrismaticJointPtr Create(World* world, Param const& param); - - float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetReferenceAngle()); } + float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetReferenceAngle()); } float GetJointTranslation() const; float GetJointSpeed() const; @@ -377,8 +374,8 @@ namespace kiwano void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); } // 设置马达转速 [degree/s] - void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Angle2Radian(speed)); } - float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetMotorSpeed()); } + void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Degree2Radian(speed)); } + float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetMotorSpeed()); } // 设定最大马达力 [N] void SetMaxMotorForce(float force) { KGE_ASSERT(raw_joint_); raw_joint_->SetMaxMotorForce(force); } @@ -391,10 +388,10 @@ namespace kiwano // 滑轮关节 class KGE_API PulleyJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point anchor_a; Point anchor_b; @@ -403,8 +400,8 @@ namespace kiwano float ratio; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& anchor_a, Point const& anchor_b, Point const& ground_anchor_a, @@ -420,8 +417,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& anchor_a, Point const& anchor_b, Point const& ground_anchor_a, @@ -433,9 +430,8 @@ namespace kiwano }; PulleyJoint(); - PulleyJoint(World* world, b2PulleyJointDef* def); - - static PulleyJointPtr Create(World* world, Param const& param); + PulleyJoint(PhysicWorld* world, b2PulleyJointDef* def); + PulleyJoint(PhysicWorld* world, Param const& param); Point GetGroundAnchorA() const; Point GetGroundAnchorB() const; @@ -455,10 +451,10 @@ namespace kiwano // 旋转关节 class KGE_API RevoluteJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point anchor; bool enable_limit; @@ -469,8 +465,8 @@ namespace kiwano float motor_speed; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& anchor, bool enable_limit = false, float lower_angle = 0.0f, @@ -490,8 +486,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& anchor, bool enable_limit = false, float lower_angle = 0.0f, @@ -505,11 +501,10 @@ namespace kiwano }; RevoluteJoint(); - RevoluteJoint(World* world, b2RevoluteJointDef* def); + RevoluteJoint(PhysicWorld* world, b2RevoluteJointDef* def); + RevoluteJoint(PhysicWorld* world, Param const& param); - static RevoluteJointPtr Create(World* world, Param const& param); - - float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetReferenceAngle()); } + float GetReferenceAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetReferenceAngle()); } float GetJointAngle() const; float GetJointSpeed() const; @@ -524,8 +519,8 @@ namespace kiwano void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); } // 设置马达转速 [degree/s] - void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Angle2Radian(speed)); } - float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetMotorSpeed()); } + void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Degree2Radian(speed)); } + float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetMotorSpeed()); } // 设定最大马达转矩 [N/m] void SetMaxMotorTorque(float torque); @@ -538,18 +533,18 @@ namespace kiwano // 绳关节 class KGE_API RopeJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point local_anchor_a; Point local_anchor_b; float max_length; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& local_anchor_a, Point const& local_anchor_b, float max_length = 0.f @@ -561,8 +556,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& local_anchor_a, Point const& local_anchor_b, float max_length = 0.f @@ -572,9 +567,8 @@ namespace kiwano }; RopeJoint(); - RopeJoint(World* world, b2RopeJointDef* def); - - static RopeJointPtr Create(World* world, Param const& param); + RopeJoint(PhysicWorld* world, b2RopeJointDef* def); + RopeJoint(PhysicWorld* world, Param const& param); void SetMaxLength(float length); float GetMaxLength() const; @@ -586,18 +580,18 @@ namespace kiwano // 焊接关节 class KGE_API WeldJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point anchor; float frequency_hz; float damping_ratio; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& anchor, float frequency_hz = 0.f, float damping_ratio = 0.f @@ -609,8 +603,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& anchor, float frequency_hz = 0.f, float damping_ratio = 0.f @@ -620,9 +614,8 @@ namespace kiwano }; WeldJoint(); - WeldJoint(World* world, b2WeldJointDef* def); - - static WeldJointPtr Create(World* world, Param const& param); + WeldJoint(PhysicWorld* world, b2WeldJointDef* def); + WeldJoint(PhysicWorld* world, Param const& param); // 设置弹簧阻尼器频率 [赫兹] void SetFrequency(float hz) { KGE_ASSERT(raw_joint_); raw_joint_->SetFrequency(hz); } @@ -639,10 +632,10 @@ namespace kiwano // 轮关节 class KGE_API WheelJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point anchor; Vec2 axis; @@ -653,8 +646,8 @@ namespace kiwano float damping_ratio; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& anchor, Vec2 const& axis, float frequency_hz = 2.0f, @@ -674,8 +667,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& anchor, Vec2 const& axis, float frequency_hz = 2.0f, @@ -689,21 +682,20 @@ namespace kiwano }; WheelJoint(); - WheelJoint(World* world, b2WheelJointDef* def); - - static WheelJointPtr Create(World* world, Param const& param); + WheelJoint(PhysicWorld* world, b2WheelJointDef* def); + WheelJoint(PhysicWorld* world, Param const& param); float GetJointTranslation() const; float GetJointLinearSpeed() const; - float GetJointAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetJointAngle()); } - float GetJointAngularSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetJointAngularSpeed()); } + float GetJointAngle() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetJointAngle()); } + float GetJointAngularSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetJointAngularSpeed()); } bool IsMotorEnabled() const { KGE_ASSERT(raw_joint_); return raw_joint_->IsMotorEnabled(); } void EnableMotor(bool flag) { KGE_ASSERT(raw_joint_); raw_joint_->EnableMotor(flag); } // 设置马达转速 [degree/s] - void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Angle2Radian(speed)); } - float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Angle(raw_joint_->GetMotorSpeed()); } + void SetMotorSpeed(float speed) { KGE_ASSERT(raw_joint_); raw_joint_->SetMotorSpeed(math::Degree2Radian(speed)); } + float GetMotorSpeed() const { KGE_ASSERT(raw_joint_); return math::Radian2Degree(raw_joint_->GetMotorSpeed()); } // 设定最大马达转矩 [N/m] void SetMaxMotorTorque(float torque); @@ -723,10 +715,10 @@ namespace kiwano // 鼠标关节 // 用于使身体的某个点追踪世界上的指定点,例如让物体追踪鼠标位置 class KGE_API MouseJoint - : public Joint + : public PhysicJoint { public: - struct Param : public Joint::ParamBase + struct Param : public PhysicJoint::ParamBase { Point target; float max_force; @@ -734,8 +726,8 @@ namespace kiwano float damping_ratio; Param( - Body* body_a, - Body* body_b, + PhysicBody* body_a, + PhysicBody* body_b, Point const& target, float max_force, float frequency_hz = 5.0f, @@ -749,8 +741,8 @@ namespace kiwano {} Param( - BodyPtr body_a, - BodyPtr body_b, + PhysicBodyPtr body_a, + PhysicBodyPtr body_b, Point const& target, float max_force, float frequency_hz = 5.0f, @@ -761,9 +753,8 @@ namespace kiwano }; MouseJoint(); - MouseJoint(World* world, b2MouseJointDef* def); - - static MouseJointPtr Create(World* world, Param const& param); + MouseJoint(PhysicWorld* world, b2MouseJointDef* def); + MouseJoint(PhysicWorld* world, Param const& param); // 设定最大摩擦力 [N] void SetMaxForce(float force); diff --git a/src/kiwano-physics/Shape.cpp b/src/kiwano-physics/Shape.cpp index 47d6dc0d..e380fa3b 100644 --- a/src/kiwano-physics/Shape.cpp +++ b/src/kiwano-physics/Shape.cpp @@ -25,55 +25,55 @@ namespace kiwano { namespace physics { - Shape::Shape() + PhysicShape::PhysicShape() : shape_(nullptr) { } - Shape::Shape(b2Shape* shape) + PhysicShape::PhysicShape(b2Shape* shape) : shape_(shape) { } - b2Shape* Shape::GetB2Shape() + b2Shape* PhysicShape::GetB2Shape() { return shape_; } - const b2Shape* Shape::GetB2Shape() const + const b2Shape* PhysicShape::GetB2Shape() const { return shape_; } - void Shape::SetB2Shape(b2Shape* shape) + void PhysicShape::SetB2Shape(b2Shape* shape) { shape_ = shape; } // - // CircleShape + // PhysicCircleShape // - CircleShape::CircleShape() - : Shape(&circle_) + PhysicCircleShape::PhysicCircleShape() + : PhysicShape(&circle_) , circle_() , radius_(0.f) { } - CircleShape::CircleShape(float radius, Point const& offset) - : CircleShape() + PhysicCircleShape::PhysicCircleShape(float radius, Point const& offset) + : PhysicCircleShape() { Set(radius, offset); } - void CircleShape::Set(float radius, Point const& offset) + void PhysicCircleShape::Set(float radius, Point const& offset) { radius_ = radius; offset_ = offset; } - void CircleShape::FitWorld(World* world) + void PhysicCircleShape::FitWorld(PhysicWorld* world) { KGE_ASSERT(world); circle_.m_radius = world->Stage2World(radius_); @@ -81,30 +81,30 @@ namespace kiwano } // - // BoxShape + // PhysicBoxShape // - BoxShape::BoxShape() - : Shape(&polygon_) + PhysicBoxShape::PhysicBoxShape() + : PhysicShape(&polygon_) , polygon_() , rotation_(0.f) { } - BoxShape::BoxShape(Vec2 const& size, Point const& offset, float rotation) - : BoxShape() + PhysicBoxShape::PhysicBoxShape(Vec2 const& size, Point const& offset, float rotation) + : PhysicBoxShape() { Set(size, offset, rotation); } - void BoxShape::Set(Vec2 const& size, Point const& offset, float rotation) + void PhysicBoxShape::Set(Vec2 const& size, Point const& offset, float rotation) { box_size_ = size; offset_ = offset; rotation_ = rotation; } - void BoxShape::FitWorld(World* world) + void PhysicBoxShape::FitWorld(PhysicWorld* world) { KGE_ASSERT(world); @@ -114,27 +114,27 @@ namespace kiwano } // - // PolygonShape + // PhysicPolygonShape // - PolygonShape::PolygonShape() - : Shape(&polygon_) + PhysicPolygonShape::PhysicPolygonShape() + : PhysicShape(&polygon_) , polygon_() { } - PolygonShape::PolygonShape(Vector const& vertexs) - : PolygonShape() + PhysicPolygonShape::PhysicPolygonShape(Vector const& vertexs) + : PhysicPolygonShape() { Set(vertexs); } - void PolygonShape::Set(Vector const& vertexs) + void PhysicPolygonShape::Set(Vector const& vertexs) { vertexs_ = vertexs; } - void PolygonShape::FitWorld(World* world) + void PhysicPolygonShape::FitWorld(PhysicWorld* world) { KGE_ASSERT(world); @@ -149,28 +149,28 @@ namespace kiwano } // - // EdgeShape + // PhysicEdgeShape // - EdgeShape::EdgeShape() - : Shape(&edge_) + PhysicEdgeShape::PhysicEdgeShape() + : PhysicShape(&edge_) , edge_() { } - EdgeShape::EdgeShape(Point const& p1, Point const& p2) - : EdgeShape() + PhysicEdgeShape::PhysicEdgeShape(Point const& p1, Point const& p2) + : PhysicEdgeShape() { Set(p1, p2); } - void EdgeShape::Set(Point const& p1, Point const& p2) + void PhysicEdgeShape::Set(Point const& p1, Point const& p2) { p_[0] = p1; p_[1] = p2; } - void EdgeShape::FitWorld(World* world) + void PhysicEdgeShape::FitWorld(PhysicWorld* world) { KGE_ASSERT(world); @@ -180,29 +180,29 @@ namespace kiwano } // - // ChainShape + // PhysicChainShape // - ChainShape::ChainShape() - : Shape(&chain_) + PhysicChainShape::PhysicChainShape() + : PhysicShape(&chain_) , chain_() , loop_(false) { } - ChainShape::ChainShape(Vector const& vertexs, bool loop) - : ChainShape() + PhysicChainShape::PhysicChainShape(Vector const& vertexs, bool loop) + : PhysicChainShape() { Set(vertexs, loop); } - void ChainShape::Set(Vector const& vertexs, bool loop) + void PhysicChainShape::Set(Vector const& vertexs, bool loop) { vertexs_ = vertexs; loop_ = loop; } - void ChainShape::FitWorld(World* world) + void PhysicChainShape::FitWorld(PhysicWorld* world) { KGE_ASSERT(world); diff --git a/src/kiwano-physics/Shape.h b/src/kiwano-physics/Shape.h index 0d891f62..1d51a5df 100644 --- a/src/kiwano-physics/Shape.h +++ b/src/kiwano-physics/Shape.h @@ -25,37 +25,37 @@ namespace kiwano { namespace physics { - class World; + class PhysicWorld; // 形状基类 - class KGE_API Shape + class KGE_API PhysicShape { public: - Shape(); - Shape(b2Shape* shape); + PhysicShape(); + PhysicShape(b2Shape* shape); b2Shape* GetB2Shape(); const b2Shape* GetB2Shape() const; void SetB2Shape(b2Shape* shape); - virtual void FitWorld(World* world) {} + virtual void FitWorld(PhysicWorld* world) {} protected: b2Shape* shape_; }; // 圆形形状 - class KGE_API CircleShape - : public Shape + class KGE_API PhysicCircleShape + : public PhysicShape { public: - CircleShape(); + PhysicCircleShape(); - CircleShape(float radius, Point const& offset = Point()); + PhysicCircleShape(float radius, Point const& offset = Point()); void Set(float radius, Point const& offset = Point()); - void FitWorld(World* world) override; + void FitWorld(PhysicWorld* world) override; protected: float radius_; @@ -64,17 +64,17 @@ namespace kiwano }; // 盒子形状 - class KGE_API BoxShape - : public Shape + class KGE_API PhysicBoxShape + : public PhysicShape { public: - BoxShape(); + PhysicBoxShape(); - BoxShape(Vec2 const& size, Point const& offset = Point(), float rotation = 0.f); + PhysicBoxShape(Vec2 const& size, Point const& offset = Point(), float rotation = 0.f); void Set(Vec2 const& size, Point const& offset = Point(), float rotation = 0.f); - void FitWorld(World* world) override; + void FitWorld(PhysicWorld* world) override; protected: float rotation_; @@ -84,17 +84,17 @@ namespace kiwano }; // 多边形形状 - class KGE_API PolygonShape - : public Shape + class KGE_API PhysicPolygonShape + : public PhysicShape { public: - PolygonShape(); + PhysicPolygonShape(); - PolygonShape(Vector const& vertexs); + PhysicPolygonShape(Vector const& vertexs); void Set(Vector const& vertexs); - void FitWorld(World* world) override; + void FitWorld(PhysicWorld* world) override; protected: Vector vertexs_; @@ -102,17 +102,17 @@ namespace kiwano }; // 线段形状, 用于表示一条边 - class KGE_API EdgeShape - : public Shape + class KGE_API PhysicEdgeShape + : public PhysicShape { public: - EdgeShape(); + PhysicEdgeShape(); - EdgeShape(Point const& p1, Point const& p2); + PhysicEdgeShape(Point const& p1, Point const& p2); void Set(Point const& p1, Point const& p2); - void FitWorld(World* world) override; + void FitWorld(PhysicWorld* world) override; protected: Point p_[2]; @@ -120,17 +120,17 @@ namespace kiwano }; // 链式形状 - class KGE_API ChainShape - : public Shape + class KGE_API PhysicChainShape + : public PhysicShape { public: - ChainShape(); + PhysicChainShape(); - ChainShape(Vector const& vertexs, bool loop = false); + PhysicChainShape(Vector const& vertexs, bool loop = false); void Set(Vector const& vertexs, bool loop = false); - void FitWorld(World* world) override; + void FitWorld(PhysicWorld* world) override; protected: bool loop_; diff --git a/src/kiwano-physics/World.cpp b/src/kiwano-physics/World.cpp index 8bbe52e2..b786b69a 100644 --- a/src/kiwano-physics/World.cpp +++ b/src/kiwano-physics/World.cpp @@ -29,12 +29,12 @@ namespace kiwano const float DefaultGlobalScale = 100.f; // 100 pixels per meters } - class World::DestructionListener : public b2DestructionListener + class PhysicWorld::DestructionListener : public b2DestructionListener { - World* world_; + PhysicWorld* world_; public: - DestructionListener(World* world) + DestructionListener(PhysicWorld* world) : world_(world) { } @@ -53,7 +53,7 @@ namespace kiwano } }; - World::World() + PhysicWorld::PhysicWorld() : world_(b2Vec2(0, 10.0f)) , vel_iter_(6) , pos_iter_(2) @@ -65,7 +65,7 @@ namespace kiwano world_.SetDestructionListener(destruction_listener_); } - World::~World() + PhysicWorld::~PhysicWorld() { world_.SetDestructionListener(nullptr); if (destruction_listener_) @@ -80,18 +80,7 @@ namespace kiwano RemoveAllJoints(); } - BodyPtr World::CreateBody(ActorPtr actor) - { - return CreateBody(actor.get()); - } - - BodyPtr World::CreateBody(Actor* actor) - { - BodyPtr body = Body::Create(this, actor); - return body; - } - - void World::RemoveBody(Body* body) + void PhysicWorld::RemoveBody(PhysicBody* body) { if (body) { @@ -102,7 +91,7 @@ namespace kiwano } } - void World::RemoveAllBodies() + void PhysicWorld::RemoveAllBodies() { if (world_.GetBodyCount()) { @@ -116,7 +105,7 @@ namespace kiwano } } - void World::AddJoint(Joint* joint) + void PhysicWorld::AddJoint(PhysicJoint* joint) { if (joint) { @@ -124,7 +113,7 @@ namespace kiwano } } - void World::RemoveJoint(Joint* joint) + void PhysicWorld::RemoveJoint(PhysicJoint* joint) { if (joint) { @@ -143,7 +132,7 @@ namespace kiwano } } - void World::RemoveAllJoints() + void PhysicWorld::RemoveAllJoints() { if (world_.GetJointCount()) { @@ -162,14 +151,14 @@ namespace kiwano joints_.clear(); } - void World::JointRemoved(b2Joint* joint) + void PhysicWorld::JointRemoved(b2Joint* joint) { if (!removing_joint_ && joint) { auto iter = std::find_if( joints_.begin(), joints_.end(), - [joint](Joint* j) -> bool { return j->GetB2Joint() == joint; } + [joint](PhysicJoint* j) -> bool { return j->GetB2Joint() == joint; } ); if (iter != joints_.end()) @@ -179,36 +168,36 @@ namespace kiwano } } - b2World* World::GetB2World() + b2World* PhysicWorld::GetB2World() { return &world_; } - const b2World* World::GetB2World() const + const b2World* PhysicWorld::GetB2World() const { return &world_; } - Vec2 World::GetGravity() const + Vec2 PhysicWorld::GetGravity() const { b2Vec2 g = world_.GetGravity(); return Vec2(g.x, g.y); } - void World::SetGravity(Vec2 gravity) + void PhysicWorld::SetGravity(Vec2 gravity) { world_.SetGravity(b2Vec2(gravity.x, gravity.y)); } - void World::Update(Duration dt) + void PhysicWorld::Update(Duration dt) { Stage::Update(dt); b2Body* b2body = world_.GetBodyList(); while (b2body) { - Body* body = static_cast(b2body->GetUserData()); - if (body && body->GetType() != Body::Type::Static) + PhysicBody* body = static_cast(b2body->GetUserData()); + if (body && body->GetType() != PhysicBody::Type::Static) { body->UpdateFromActor(); } @@ -221,8 +210,8 @@ namespace kiwano b2body = world_.GetBodyList(); while (b2body) { - Body* body = static_cast(b2body->GetUserData()); - if (body && body->GetType() != Body::Type::Static) + PhysicBody* body = static_cast(b2body->GetUserData()); + if (body && body->GetType() != PhysicBody::Type::Static) { body->UpdateActor(); } diff --git a/src/kiwano-physics/World.h b/src/kiwano-physics/World.h index 6fbb887d..a0b98ec8 100644 --- a/src/kiwano-physics/World.h +++ b/src/kiwano-physics/World.h @@ -27,20 +27,16 @@ namespace kiwano namespace physics { // 物理世界 - class KGE_API World + class KGE_API PhysicWorld : public Stage { - friend class Body; - friend class Joint; + friend class PhysicBody; + friend class PhysicJoint; public: - World(); + PhysicWorld(); - virtual ~World(); - - // 创建刚体 - BodyPtr CreateBody(ActorPtr actor); - BodyPtr CreateBody(Actor* actor); + virtual ~PhysicWorld(); // 获取重力 Vec2 GetGravity() const; @@ -75,17 +71,17 @@ namespace kiwano const b2World* GetB2World() const; protected: - // 移除刚体 - void RemoveBody(Body* body); + // 移除物体 + void RemoveBody(PhysicBody* body); - // 移除所有刚体 + // 移除所有物体 void RemoveAllBodies(); // 添加关节 - void AddJoint(Joint* joint); + void AddJoint(PhysicJoint* joint); // 移除关节 - void RemoveJoint(Joint* joint); + void RemoveJoint(PhysicJoint* joint); // 移除所有关节 void RemoveAllJoints(); @@ -107,9 +103,9 @@ namespace kiwano DestructionListener* destruction_listener_; bool removing_joint_; - Vector joints_; + Vector joints_; }; - KGE_DECLARE_SMART_PTR(World); + KGE_DECLARE_SMART_PTR(PhysicWorld); } } diff --git a/src/kiwano/2d/Actor.h b/src/kiwano/2d/Actor.h index 9795edef..b0727211 100644 --- a/src/kiwano/2d/Actor.h +++ b/src/kiwano/2d/Actor.h @@ -36,14 +36,14 @@ namespace kiwano , public TimerManager , public ActionManager , public EventDispatcher - , public intrusive_list_item + , public IntrusiveListItem { friend class Director; friend class Transition; - friend class intrusive_list; + friend IntrusiveList; public: - using Children = intrusive_list; + using Children = IntrusiveList; using UpdateCallback = Function; Actor(); @@ -70,61 +70,61 @@ namespace kiwano int GetZOrder() const { return z_order_; } // 获取坐标 - Point GetPosition() const { return transform_.position; } + Point const& GetPosition() const { return transform_.position; } // 获取 x 坐标 - float GetPositionX() const { return transform_.position.x; } + float GetPositionX() const { return GetPosition().x; } // 获取 y 坐标 - float GetPositionY() const { return transform_.position.y; } + float GetPositionY() const { return GetPosition().y; } // 获取缩放比例 - Point GetScale() const { return transform_.scale; } + Point const& GetScale() const { return transform_.scale; } // 获取横向缩放比例 - float GetScaleX() const { return transform_.scale.x; } + float GetScaleX() const { return GetScale().x; } // 获取纵向缩放比例 - float GetScaleY() const { return transform_.scale.y; } + float GetScaleY() const { return GetScale().y; } // 获取错切角度 - Point GetSkew() const { return transform_.skew; } + Point const& GetSkew() const { return transform_.skew; } // 获取横向错切角度 - float GetSkewX() const { return transform_.skew.x; } + float GetSkewX() const { return GetSkew().x; } // 获取纵向错切角度 - float GetSkewY() const { return transform_.skew.y; } + float GetSkewY() const { return GetSkew().y; } // 获取旋转角度 float GetRotation() const { return transform_.rotation; } // 获取宽度 - float GetWidth() const { return size_.x; } + float GetWidth() const { return GetSize().x; } // 获取高度 - float GetHeight() const { return size_.y; } + float GetHeight() const { return GetSize().y; } // 获取大小 - Size GetSize() const { return size_; } + Size const& GetSize() const { return size_; } // 获取缩放后的宽度 - float GetScaledWidth() const { return size_.x * transform_.scale.x; } + float GetScaledWidth() const { return GetWidth() * GetScaleX(); } // 获取缩放后的高度 - float GetScaledHeight() const { return size_.y * transform_.scale.y; } + float GetScaledHeight() const { return GetHeight() * GetScaleY(); } // 获取缩放后的大小 Size GetScaledSize() const { return Size{ GetScaledWidth(), GetScaledHeight() }; } // 获取锚点 - Point GetAnchor() const { return anchor_; } + Point const& GetAnchor() const { return anchor_; } // 获取 x 方向锚点 - float GetAnchorX() const { return anchor_.x; } + float GetAnchorX() const { return GetAnchor().x; } // 获取 y 方向锚点 - float GetAnchorY() const { return anchor_.y; } + float GetAnchorY() const { return GetAnchor().y; } // 获取透明度 float GetOpacity() const { return opacity_; } diff --git a/src/kiwano/2d/Text.h b/src/kiwano/2d/Text.h index 4ee9df11..c4cc6586 100644 --- a/src/kiwano/2d/Text.h +++ b/src/kiwano/2d/Text.h @@ -150,6 +150,9 @@ namespace kiwano StrokeStyle outline_stroke ); + // 更新文本布局 + void UpdateLayout(); + // 设置默认字体 static void SetDefaultFont( Font const& font @@ -162,9 +165,6 @@ namespace kiwano void OnRender(RenderTarget* rt) override; - protected: - void UpdateLayout(); - protected: bool format_dirty_; bool layout_dirty_; diff --git a/src/kiwano/2d/action/Action.h b/src/kiwano/2d/action/Action.h index 5ef5fe21..2b222214 100644 --- a/src/kiwano/2d/action/Action.h +++ b/src/kiwano/2d/action/Action.h @@ -29,11 +29,11 @@ namespace kiwano class KGE_API Action : public ObjectBase - , protected intrusive_list_item + , protected IntrusiveListItem { friend class ActionManager; friend class ActionGroup; - friend class intrusive_list; + friend IntrusiveList; public: enum class Status diff --git a/src/kiwano/2d/action/ActionGroup.h b/src/kiwano/2d/action/ActionGroup.h index 058608d8..4ad51b62 100644 --- a/src/kiwano/2d/action/ActionGroup.h +++ b/src/kiwano/2d/action/ActionGroup.h @@ -28,7 +28,7 @@ namespace kiwano : public Action { public: - using ActionList = intrusive_list; + using ActionList = IntrusiveList; ActionGroup(); diff --git a/src/kiwano/2d/action/ActionManager.h b/src/kiwano/2d/action/ActionManager.h index e0ae6282..98f7c2b7 100644 --- a/src/kiwano/2d/action/ActionManager.h +++ b/src/kiwano/2d/action/ActionManager.h @@ -25,7 +25,7 @@ namespace kiwano { class KGE_API ActionManager { - using Actions = intrusive_list; + using Actions = IntrusiveList; public: // 添加动作 diff --git a/src/kiwano/base/EventDispatcher.h b/src/kiwano/base/EventDispatcher.h index 75065de8..31f83298 100644 --- a/src/kiwano/base/EventDispatcher.h +++ b/src/kiwano/base/EventDispatcher.h @@ -25,7 +25,7 @@ namespace kiwano { class KGE_API EventDispatcher { - using Listeners = intrusive_list; + using Listeners = IntrusiveList; public: // 添加监听器 diff --git a/src/kiwano/base/EventListener.h b/src/kiwano/base/EventListener.h index ee0fc59a..42317aae 100644 --- a/src/kiwano/base/EventListener.h +++ b/src/kiwano/base/EventListener.h @@ -33,10 +33,10 @@ namespace kiwano // 事件监听器 class KGE_API EventListener : public ObjectBase - , protected intrusive_list_item + , protected IntrusiveListItem { friend class EventDispatcher; - friend class intrusive_list; + friend IntrusiveList; public: using Callback = Function; diff --git a/src/kiwano/base/ObjectBase.h b/src/kiwano/base/ObjectBase.h index fbf61547..8f4d9b94 100644 --- a/src/kiwano/base/ObjectBase.h +++ b/src/kiwano/base/ObjectBase.h @@ -29,7 +29,7 @@ namespace kiwano KGE_DECLARE_SMART_PTR(ObjectBase); class KGE_API ObjectBase - : public RefCounter + : public virtual RefCounter { public: ObjectBase(); diff --git a/src/kiwano/base/RefCounter.hpp b/src/kiwano/base/RefCounter.hpp index b3bc7443..e4c7eb6e 100644 --- a/src/kiwano/base/RefCounter.hpp +++ b/src/kiwano/base/RefCounter.hpp @@ -25,7 +25,7 @@ namespace kiwano { class KGE_API RefCounter - : protected noncopyable + : protected core::noncopyable { public: // 增加引用计数 diff --git a/src/kiwano/base/SmartPtr.hpp b/src/kiwano/base/SmartPtr.hpp index dd40bae7..09a60358 100644 --- a/src/kiwano/base/SmartPtr.hpp +++ b/src/kiwano/base/SmartPtr.hpp @@ -38,7 +38,7 @@ namespace kiwano }; template - using SmartPtr = intrusive_ptr<_Ty, DefaultIntrusivePtrManager>; + using SmartPtr = core::intrusive_ptr<_Ty, DefaultIntrusivePtrManager>; } diff --git a/src/kiwano/base/Timer.h b/src/kiwano/base/Timer.h index c87b1578..2ab574a8 100644 --- a/src/kiwano/base/Timer.h +++ b/src/kiwano/base/Timer.h @@ -33,10 +33,10 @@ namespace kiwano // 定时任务 class KGE_API Timer : public ObjectBase - , protected intrusive_list_item + , protected IntrusiveListItem { friend class TimerManager; - friend class intrusive_list; + friend IntrusiveList; using Callback = Function; diff --git a/src/kiwano/base/TimerManager.h b/src/kiwano/base/TimerManager.h index b0a86fae..c1b989ae 100644 --- a/src/kiwano/base/TimerManager.h +++ b/src/kiwano/base/TimerManager.h @@ -25,7 +25,7 @@ namespace kiwano { class KGE_API TimerManager { - using Timers = intrusive_list; + using Timers = IntrusiveList; public: // 添加定时器 diff --git a/src/kiwano/base/win32/ComPtr.hpp b/src/kiwano/base/win32/ComPtr.hpp index c6d59f90..6d705861 100644 --- a/src/kiwano/base/win32/ComPtr.hpp +++ b/src/kiwano/base/win32/ComPtr.hpp @@ -42,6 +42,6 @@ namespace kiwano template< typename _Ty, typename = typename std::enable_if::value, int>::type> - using ComPtr = intrusive_ptr<_Ty, ComPtrManager>; + using ComPtr = core::intrusive_ptr<_Ty, ComPtrManager>; } diff --git a/src/kiwano/core/any.hpp b/src/kiwano/core/any.hpp index ddb3f63a..81d66390 100644 --- a/src/kiwano/core/any.hpp +++ b/src/kiwano/core/any.hpp @@ -26,7 +26,7 @@ namespace kiwano { -inline namespace core +namespace core { class bad_any_cast : public std::exception diff --git a/src/kiwano/core/basic_json.hpp b/src/kiwano/core/basic_json.hpp index 5ef5e514..05080c47 100644 --- a/src/kiwano/core/basic_json.hpp +++ b/src/kiwano/core/basic_json.hpp @@ -28,7 +28,7 @@ namespace kiwano { -inline namespace core +namespace core { // @@ -2728,7 +2728,7 @@ private: __json_detail::json_value value_; }; -} // inline namespace core +} // namespace core } // namespace kiwano diff --git a/src/kiwano/core/core.h b/src/kiwano/core/core.h index 00b9a851..be374b02 100644 --- a/src/kiwano/core/core.h +++ b/src/kiwano/core/core.h @@ -78,6 +78,15 @@ namespace kiwano using Json = kiwano::core::basic_json; + + template + using Singleton = core::singleton<_Ty>; + + template + using IntrusiveList = core::intrusive_list<_Ty>; + + template + using IntrusiveListItem = core::intrusive_list_item<_Ty>; } namespace std diff --git a/src/kiwano/core/function.hpp b/src/kiwano/core/function.hpp index 261affff..3a686226 100644 --- a/src/kiwano/core/function.hpp +++ b/src/kiwano/core/function.hpp @@ -25,7 +25,7 @@ namespace kiwano { -inline namespace core +namespace core { // // function is a light weight ::std::function<>-like class @@ -321,7 +321,7 @@ private: __function_detail::callable<_Ret, _Args...>* callable_; }; -} // inline namespace core +} // namespace core } // namespace kiwano @@ -333,10 +333,11 @@ namespace kiwano std::is_same<_Ty, _Uty>::value || std::is_base_of<_Ty, _Uty>::value, int >::type, typename _Ret, - typename... _Args> - inline function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...)) + typename... _Args + > + inline core::function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...)) { - return function<_Ret(_Args...)>(ptr, func); + return core::function<_Ret(_Args...)>(ptr, func); } template::value || std::is_base_of<_Ty, _Uty>::value, int >::type, typename _Ret, - typename... _Args> - inline function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...) const) + typename... _Args + > + inline core::function<_Ret(_Args...)> Closure(_Uty* ptr, _Ret(_Ty::* func)(_Args...) const) { - return function<_Ret(_Args...)>(ptr, func); + return core::function<_Ret(_Args...)>(ptr, func); } } diff --git a/src/kiwano/core/intrusive_list.hpp b/src/kiwano/core/intrusive_list.hpp index 1b70840d..4ce55f9f 100644 --- a/src/kiwano/core/intrusive_list.hpp +++ b/src/kiwano/core/intrusive_list.hpp @@ -32,7 +32,7 @@ namespace kiwano { -inline namespace core +namespace core { template @@ -285,8 +285,8 @@ public: using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; - inline iterator begin() { return iterator(first_item()); } - inline const_iterator begin() const { return const_iterator(first_item()); } + inline iterator begin() { return iterator(first_item(), first_item() == nullptr); } + inline const_iterator begin() const { return const_iterator(first_item(), first_item() == nullptr); } inline const_iterator cbegin() const { return begin(); } inline iterator end() { return iterator(last_item(), true); } inline const_iterator end() const { return const_iterator(last_item(), true); } @@ -307,7 +307,7 @@ private: value_type last_; }; -} // inline namespace core +} // namespace core } // namespace kiwano diff --git a/src/kiwano/core/intrusive_ptr.hpp b/src/kiwano/core/intrusive_ptr.hpp index 82cf8cde..8a84e987 100644 --- a/src/kiwano/core/intrusive_ptr.hpp +++ b/src/kiwano/core/intrusive_ptr.hpp @@ -26,7 +26,7 @@ namespace kiwano { -inline namespace core +namespace core { template @@ -141,6 +141,6 @@ inline void swap(intrusive_ptr<_Ty, manager_type>& lhs, intrusive_ptr<_Ty, manag lhs.swap(rhs); } -} // inline namespace core +} // namespace core } // namespace kiwano diff --git a/src/kiwano/core/noncopyable.hpp b/src/kiwano/core/noncopyable.hpp index dff4e653..741cf806 100644 --- a/src/kiwano/core/noncopyable.hpp +++ b/src/kiwano/core/noncopyable.hpp @@ -22,7 +22,7 @@ namespace kiwano { -inline namespace core +namespace core { class noncopyable @@ -36,5 +36,5 @@ private: noncopyable& operator=(const noncopyable&) = delete; }; -} // inline namespace core +} // namespace core } // namespace kiwano diff --git a/src/kiwano/core/singleton.hpp b/src/kiwano/core/singleton.hpp index 43819e02..be4aabd7 100644 --- a/src/kiwano/core/singleton.hpp +++ b/src/kiwano/core/singleton.hpp @@ -27,17 +27,17 @@ #ifndef KGE_DECLARE_SINGLETON #define KGE_DECLARE_SINGLETON( CLASS ) \ - friend ::kiwano::Singleton< CLASS >; \ + friend ::kiwano::core::singleton< CLASS >; \ friend typename std::unique_ptr< CLASS >::deleter_type #endif namespace kiwano { -inline namespace core +namespace core { template -struct Singleton +struct singleton { public: static inline _Ty* GetInstance() @@ -55,12 +55,12 @@ public: } protected: - Singleton() = default; + singleton() = default; private: - Singleton(const Singleton&) = delete; + singleton(const singleton&) = delete; - Singleton& operator=(const Singleton&) = delete; + singleton& operator=(const singleton&) = delete; static inline void InitInstance() { @@ -76,10 +76,10 @@ private: }; template -std::once_flag Singleton<_Ty>::once_; +std::once_flag singleton<_Ty>::once_; template -std::unique_ptr<_Ty> Singleton<_Ty>::instance_; +std::unique_ptr<_Ty> singleton<_Ty>::instance_; -} // inline namespace core +} // namespace core } // namespace kiwano diff --git a/src/kiwano/core/string.hpp b/src/kiwano/core/string.hpp index d5aef04c..e210387b 100644 --- a/src/kiwano/core/string.hpp +++ b/src/kiwano/core/string.hpp @@ -29,7 +29,7 @@ namespace kiwano { -inline namespace core +namespace core { // @@ -450,7 +450,7 @@ inline wstring to_wstring(double val) { return to_basic_string(val); inline wstring to_wstring(long double val) { return to_basic_string(val); } -} // inline namespace core +} // namespace core } // namespace kiwano namespace kiwano @@ -515,7 +515,7 @@ namespace __string_details } } -inline namespace core +namespace core { template @@ -1159,31 +1159,31 @@ inline namespace core // template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(int val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(int val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned int val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned int val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(long val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(long val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(long long val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(long long val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long long val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(unsigned long long val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(float val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(float val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(double val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(double val) { return ::kiwano::core::to_basic_string(val); } template - inline basic_string<_CharTy> basic_string<_CharTy>::parse(long double val) { return ::kiwano::to_basic_string(val); } + inline basic_string<_CharTy> basic_string<_CharTy>::parse(long double val) { return ::kiwano::core::to_basic_string(val); } // // details of basic_string::format @@ -1193,7 +1193,7 @@ inline namespace core template inline basic_string<_CharTy> basic_string<_CharTy>::format(const char_type* fmt, _Args&& ... args) { - return ::kiwano::format_string(fmt, std::forward<_Args>(args)...); + return ::kiwano::core::format_string(fmt, std::forward<_Args>(args)...); } // diff --git a/src/kiwano/core/vector.hpp b/src/kiwano/core/vector.hpp index d099f729..0194c248 100644 --- a/src/kiwano/core/vector.hpp +++ b/src/kiwano/core/vector.hpp @@ -25,7 +25,7 @@ namespace kiwano { -inline namespace core +namespace core { @@ -284,5 +284,5 @@ namespace __vector_details }; } -} // inline namespace core +} // namespace core } // namespace kiwano diff --git a/src/kiwano/math/scalar.h b/src/kiwano/math/scalar.h index 7b41fe8b..90a02471 100644 --- a/src/kiwano/math/scalar.h +++ b/src/kiwano/math/scalar.h @@ -40,29 +40,29 @@ namespace kiwano inline float Sign(float val) { return val < 0 ? -1.f : 1.f; } inline double Sign(double val) { return val < 0 ? -1.0 : 1.0; } - inline float Angle2Radian(float angle) { return angle * constants::PI_F / 180.f; } - inline double Angle2Radian(double angle) { return angle * constants::PI_D / 180.0; } + inline float Degree2Radian(float angle) { return angle * constants::PI_F / 180.f; } + inline double Degree2Radian(double angle) { return angle * constants::PI_D / 180.0; } - inline float Radian2Angle(float radian) { return radian * 180.f / math::constants::PI_F; } - inline double Radian2Angle(double radian) { return radian * 180.0 / math::constants::PI_D; } + inline float Radian2Degree(float radian) { return radian * 180.f / math::constants::PI_F; } + inline double Radian2Degree(double radian) { return radian * 180.0 / math::constants::PI_D; } - inline float Sin(float val) { return ::sinf(Angle2Radian(val)); } - inline double Sin(double val) { return ::sin(Angle2Radian(val)); } + inline float Sin(float val) { return ::sinf(Degree2Radian(val)); } + inline double Sin(double val) { return ::sin(Degree2Radian(val)); } - inline float Cos(float val) { return ::cosf(Angle2Radian(val)); } - inline double Cos(double val) { return ::cos(Angle2Radian(val)); } + inline float Cos(float val) { return ::cosf(Degree2Radian(val)); } + inline double Cos(double val) { return ::cos(Degree2Radian(val)); } - inline float Tan(float val) { return ::tanf(Angle2Radian(val)); } - inline double Tan(double val) { return ::tan(Angle2Radian(val)); } + inline float Tan(float val) { return ::tanf(Degree2Radian(val)); } + inline double Tan(double val) { return ::tan(Degree2Radian(val)); } - inline float Asin(float val) { return Radian2Angle(::asinf(val)); } - inline double Asin(double val) { return Radian2Angle(::asin(val)); } + inline float Asin(float val) { return Radian2Degree(::asinf(val)); } + inline double Asin(double val) { return Radian2Degree(::asin(val)); } - inline float Acos(float val) { return Radian2Angle(::acosf(val)); } - inline double Acos(double val) { return Radian2Angle(::acos(val)); } + inline float Acos(float val) { return Radian2Degree(::acosf(val)); } + inline double Acos(double val) { return Radian2Degree(::acos(val)); } - inline float Atan(float val) { return Radian2Angle(::atanf(val)); } - inline double Atan(double val) { return Radian2Angle(::atan(val)); } + inline float Atan(float val) { return Radian2Degree(::atanf(val)); } + inline double Atan(double val) { return Radian2Degree(::atan(val)); } inline float Ceil(float val) { return ::ceil(val); } inline double Ceil(double val) { return ::ceil(val); } diff --git a/src/kiwano/platform/Application.h b/src/kiwano/platform/Application.h index 8322aac9..f1224b39 100644 --- a/src/kiwano/platform/Application.h +++ b/src/kiwano/platform/Application.h @@ -50,7 +50,7 @@ namespace kiwano // 应用 class KGE_API Application - : protected noncopyable + : protected core::noncopyable { public: Application(); diff --git a/src/kiwano/renderer/Geometry.h b/src/kiwano/renderer/Geometry.h index d166c297..e522ee7a 100644 --- a/src/kiwano/renderer/Geometry.h +++ b/src/kiwano/renderer/Geometry.h @@ -129,7 +129,7 @@ namespace kiwano // 几何体生成器 class KGE_API GeometrySink - : protected noncopyable + : protected core::noncopyable { public: GeometrySink(); diff --git a/src/kiwano/renderer/RenderTarget.h b/src/kiwano/renderer/RenderTarget.h index 1ba9d8c1..2f2ae489 100644 --- a/src/kiwano/renderer/RenderTarget.h +++ b/src/kiwano/renderer/RenderTarget.h @@ -41,7 +41,7 @@ namespace kiwano // 渲染目标 class KGE_API RenderTarget - : public noncopyable + : public core::noncopyable { public: bool IsValid() const; diff --git a/src/kiwano/utils/ResourceCache.cpp b/src/kiwano/utils/ResourceCache.cpp index 2c75a649..05d657a7 100644 --- a/src/kiwano/utils/ResourceCache.cpp +++ b/src/kiwano/utils/ResourceCache.cpp @@ -83,7 +83,7 @@ namespace kiwano KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot open file. (%s)", string_to_wide(e.what()).c_str()); return false; } - catch (json_exception& e) + catch (core::json_exception& e) { KGE_WARNING_LOG(L"ResourceCache::LoadFromJsonFile failed: Cannot parse to JSON. (%s)", string_to_wide(e.what()).c_str()); return false; @@ -163,7 +163,7 @@ namespace kiwano { if (auto root = doc->FirstChildElement(L"resources")) { - kiwano::wstring version; + String version; if (auto ver = root->FirstChildElement(L"version")) version = ver->GetText(); auto load = load_xml_funcs.find(version);