Преглед изворни кода

Do not use global Physics object to initialize physics system(s). Use init() and shutdown() in a dedicated namespace instead

Daniele Bartolini пре 12 година
родитељ
комит
dda9f6ea90

+ 0 - 1
engine/CMakeLists.txt

@@ -400,7 +400,6 @@ set (PHYSICS_SRC
 set (PHYSICS_HEADERS
 	physics/Actor.h
 	physics/Controller.cpp
-	physics/Physics.h
 	physics/PhysicsTypes.h
 	physics/PhysicsWorld.h
 	physics/Trigger.h

+ 3 - 6
engine/Device.cpp

@@ -100,7 +100,6 @@ Device::Device()
 	, m_resource_manager(NULL)
 	, m_resource_bundle(NULL)
 
-	, m_physx(NULL)
 	, m_world_manager(NULL)
 
 	, m_renderer_init_request(false)
@@ -180,7 +179,7 @@ void Device::init()
 	m_lua_environment->init();
 
 	Log::d("Creating physics...");
-	m_physx = CE_NEW(m_allocator, Physics)();
+	physics_system::init();
 
 	Log::d("Creating audio...");
 	audio_system::init();
@@ -215,13 +214,11 @@ void Device::shutdown()
 	// Shutdowns the game
 	m_lua_environment->call_global("shutdown", 0);
 
+	Log::d("Releasing audio...");
 	audio_system::shutdown();
 
 	Log::d("Releasing Physics...");
-	if (m_physx)
-	{
-		CE_DELETE(m_allocator, m_physx);
-	}
+	physics_system::shutdown();
 
 	Log::d("Releasing LuaEnvironment...");
 	if (m_lua_environment)

+ 0 - 3
engine/Device.h

@@ -31,7 +31,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "OS.h"
 #include "LinearAllocator.h"
 #include "Resource.h"
-#include "Physics.h"
 #include "WorldTypes.h"
 
 namespace crown
@@ -149,7 +148,6 @@ public:
 	Touch*					touch();
 	Accelerometer*			accelerometer();
 	ConsoleServer*			console() { return m_console; }
-	physx::PxPhysics*		physx() { return m_physx->m_physics; };
 	WorldManager*			world_manager() { return m_world_manager; }
 
 protected:
@@ -198,7 +196,6 @@ protected:
 	ResourceManager*		m_resource_manager;
 	Bundle*					m_resource_bundle;
 
-	Physics*				m_physx;
 	WorldManager*			m_world_manager;
 
 	bool 					m_renderer_init_request;

+ 8 - 9
engine/physics/Actor.cpp

@@ -29,8 +29,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Quaternion.h"
 #include "Matrix4x4.h"
 #include "Unit.h"
-#include "Device.h"
-#include "Physics.h"
 #include "Log.h"
 #include "PhysicsResource.h"
 #include "SceneGraph.h"
@@ -64,7 +62,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Actor::Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot)
+Actor::Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot)
 	: m_resource(res)
 	, m_index(i)
 	, m_scene(scene)
@@ -81,28 +79,29 @@ Actor::Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph&
 	{
 		case ActorType::STATIC:
 		{
-			m_actor = device()->physx()->createRigidStatic(PxTransform(pose));
+			m_actor = physics->createRigidStatic(PxTransform(pose));
 			break;
 		}
 		case ActorType::DYNAMIC_PHYSICAL:
 		case ActorType::DYNAMIC_KINEMATIC:
 		{
-			m_actor = device()->physx()->createRigidDynamic(PxTransform(pose));
+			m_actor = physics->createRigidDynamic(PxTransform(pose));
 
 			if (a.type == ActorType::DYNAMIC_KINEMATIC)
 			{
 				static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
 			}
-			break;
+			//break;
 
-			PxRigidBodyExt::setMassAndUpdateInertia(*static_cast<PxRigidDynamic*>(m_actor), 500.0f);
+			//PxRigidBodyExt::setMassAndUpdateInertia(*static_cast<PxRigidDynamic*>(m_actor), 500.0f);
 
-			PxD6Joint* joint = PxD6JointCreate(*device()->physx(), m_actor, PxTransform(pose), NULL, PxTransform(pose));
+			PxD6Joint* joint = PxD6JointCreate(*physics, m_actor, PxTransform(pose), NULL, PxTransform(pose));
 			joint->setMotion(PxD6Axis::eX, PxD6Motion::eFREE);
 			joint->setMotion(PxD6Axis::eY, PxD6Motion::eFREE);
 			//joint->setMotion(PxD6Axis::eZ, PxD6Motion::eFREE);
 			//joint->setMotion(PxD6Axis::eSWING1, PxD6Motion::eFREE);
 			joint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eFREE);
+			break;
 		}
 		default:
 		{
@@ -114,7 +113,7 @@ Actor::Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph&
 	m_actor->userData = this;
 
 	// Creates material
-	m_mat = device()->physx()->createMaterial(a.static_friction, a.dynamic_friction, a.restitution);
+	m_mat = physics->createMaterial(a.static_friction, a.dynamic_friction, a.restitution);
 
 	// Creates shapes
 	uint32_t index = m_resource->shape_index(m_index);

+ 2 - 2
engine/physics/Actor.h

@@ -27,7 +27,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "Types.h"
-#include "IdTable.h"
 #include "IdArray.h"
 #include "PhysicsTypes.h"
 #include "Vector3.h"
@@ -41,6 +40,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 using physx::PxRigidActor;
 using physx::PxMaterial;
 using physx::PxScene;
+using physx::PxPhysics;
 
 namespace crown
 {
@@ -53,7 +53,7 @@ class SceneGraph;
 
 struct Actor
 {
-						Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot);
+						Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot);
 						~Actor();
 
 	void				enable_gravity();

+ 4 - 4
engine/physics/Controller.cpp

@@ -25,7 +25,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Controller.h"
-#include "Device.h"
 #include "MathUtils.h"
 #include "PhysicsResource.h"
 #include "SceneGraph.h"
@@ -33,6 +32,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "PhysicsCallback.h"
 
 #include "PxCapsuleController.h"
+#include "PxPhysicsAPI.h"
 using physx::PxCapsuleClimbingMode;
 using physx::PxCapsuleController;
 using physx::PxCapsuleControllerDesc;
@@ -46,7 +46,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxScene* scene, PxControllerManager* manager)
+Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxScene* scene, PxControllerManager* manager)
 	: m_resource(pr)
 	, m_scene_graph(sg)
 	, m_node(node)
@@ -65,14 +65,14 @@ Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node,
 	desc.stepOffset = contr.step_offset;
 	desc.contactOffset = contr.contact_offset;
 	desc.upDirection = PxVec3(0.0, 1.0, 0.0);
-	desc.material = device()->physx()->createMaterial(0.5f, 0.5f, 0.5f);
+	desc.material = physics->createMaterial(0.5f, 0.5f, 0.5f);
 	desc.position = PxExtendedVec3(0, 0, 0);
 
 	CE_ASSERT(desc.isValid(), "Capsule is not valid");
 	m_callback = CE_NEW(default_allocator(), PhysicsControllerCallback)();
 	desc.callback = m_callback;
 
-	m_controller = manager->createController(*device()->physx(), scene, desc);
+	m_controller = manager->createController(*physics, scene, desc);
 	CE_ASSERT(m_controller, "Failed to create controller");
 }
 

+ 2 - 1
engine/physics/Controller.h

@@ -31,6 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 using physx::PxController;
 using physx::PxControllerManager;
+using physx::PxPhysics;
 using physx::PxScene;
 using physx::PxU32;
 
@@ -44,7 +45,7 @@ class PhysicsControllerCallback;
 
 struct Controller
 {
-							Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxScene* scene, PxControllerManager* manager);
+							Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxScene* scene, PxControllerManager* manager);
 							~Controller();
 
 	void					move(const Vector3& pos);

+ 0 - 146
engine/physics/Physics.h

@@ -1,146 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "ProxyAllocator.h"
-
-#include "PxFoundation.h"
-#include "PxPhysics.h"
-#include "PxCooking.h"
-#include "PxDefaultAllocator.h"
-#include "PxDefaultErrorCallback.h"
-#include "PxExtensionsAPI.h"
-
-#include "Log.h"
-
-using physx::PxAllocatorCallback;
-using physx::PxErrorCallback;
-using physx::PxErrorCode;
-
-namespace crown
-{
-
-class PhysXAllocator : public PxAllocatorCallback
-{
-public:
-
-	PhysXAllocator(Allocator& a)
-		: m_backing(a)
-	{
-	}
-
-	void* allocate(size_t size, const char*, const char*, int)
-	{
-		return m_backing.allocate(size, 16);
-	}
-
-	void deallocate(void* p)
-	{
-		m_backing.deallocate(p);
-	}
-
-private:
-
-	Allocator& m_backing;
-};
-
-class PhysXError : public PxErrorCallback
-{
-public:
-
-	void reportError(PxErrorCode::Enum code, const char* message, const char* file, int line)
-	{
-		switch (code)
-		{
-			case PxErrorCode::eDEBUG_INFO:
-			{
-				Log::i("In %s:%d: %s", file, line, message);
-				break;
-			}
-			case PxErrorCode::eDEBUG_WARNING: 
-			case PxErrorCode::ePERF_WARNING:
-			{
-				Log::w("In %s:%d: %s", file, line, message);
-				break;
-			}
-			case PxErrorCode::eINVALID_PARAMETER:
-			case PxErrorCode::eINVALID_OPERATION:
-			case PxErrorCode::eOUT_OF_MEMORY:
-			case PxErrorCode::eINTERNAL_ERROR:
-			case PxErrorCode::eABORT:
-			{
-				Log::e("In %s:%d: %s", file, line, message);
-				break;
-			}
-			default:
-			{
-				break;
-			}
-		}
-	}
-};
-
-struct Physics
-{
-	Physics();
-	~Physics();
-
-public:
-
-	ProxyAllocator m_allocator;
-	PhysXAllocator m_px_allocator;
-	PhysXError m_error;
-	physx::PxFoundation* m_foundation;
-	physx::PxPhysics* m_physics;
-};
-
-//-----------------------------------------------------------------------------
-inline Physics::Physics()
-	: m_allocator("physics", default_allocator())
-	, m_px_allocator(m_allocator)
-	, m_foundation(NULL)
-	, m_physics(NULL)
-{
-	m_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, m_px_allocator, m_error);
-	CE_ASSERT(m_foundation, "Unable to create PhysX Foundation");
-
-	m_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *m_foundation, physx::PxTolerancesScale());
-	CE_ASSERT(m_physics, "Unable to create PhysX Physics");
-
-	bool extension = PxInitExtensions(*m_physics);
-	CE_ASSERT(extension, "Unable to initialize PhysX Extensions");
-}
-
-//-----------------------------------------------------------------------------
-inline Physics::~Physics()
-{
-	PxCloseExtensions();
-	m_physics->release();
-	m_foundation->release();
-}
-
-} // namespace crown

+ 109 - 7
engine/physics/PhysicsWorld.cpp

@@ -28,12 +28,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Vector3.h"
 #include "Actor.h"
 #include "Device.h"
-#include "Physics.h"
 #include "Quaternion.h"
 #include "SceneGraph.h"
 #include "Controller.h"
 #include "Trigger.h"
 #include "PhysicsCallback.h"
+#include "ProxyAllocator.h"
 
 #include "PxPhysicsAPI.h"
 
@@ -61,6 +61,108 @@ using physx::PxFilterFlag;
 namespace crown
 {
 
+namespace physics_system
+{
+	using physx::PxFoundation;
+	using physx::PxPhysics;
+	using physx::PxAllocatorCallback;
+	using physx::PxErrorCallback;
+	using physx::PxErrorCode;
+
+	//-----------------------------------------------------------------------------
+	class PhysXAllocator : public PxAllocatorCallback
+	{
+	public:
+
+		PhysXAllocator()
+			: m_backing("physics", default_allocator())
+		{
+		}
+
+		void* allocate(size_t size, const char*, const char*, int)
+		{
+			return m_backing.allocate(size, 16);
+		}
+
+		void deallocate(void* p)
+		{
+			m_backing.deallocate(p);
+		}
+
+	private:
+
+		ProxyAllocator m_backing;
+	};
+
+	//-----------------------------------------------------------------------------
+	class PhysXError : public PxErrorCallback
+	{
+	public:
+
+		void reportError(PxErrorCode::Enum code, const char* message, const char* file, int line)
+		{
+			switch (code)
+			{
+				case PxErrorCode::eDEBUG_INFO:
+				{
+					Log::i("In %s:%d: %s", file, line, message);
+					break;
+				}
+				case PxErrorCode::eDEBUG_WARNING: 
+				case PxErrorCode::ePERF_WARNING:
+				{
+					Log::w("In %s:%d: %s", file, line, message);
+					break;
+				}
+				case PxErrorCode::eINVALID_PARAMETER:
+				case PxErrorCode::eINVALID_OPERATION:
+				case PxErrorCode::eOUT_OF_MEMORY:
+				case PxErrorCode::eINTERNAL_ERROR:
+				case PxErrorCode::eABORT:
+				{
+					CE_ASSERT(false, "In %s:%d: %s", file, line, message);
+					break;
+				}
+				default:
+				{
+					CE_FATAL("Oops, unknown physx error");
+					break;
+				}
+			}
+		}
+	};
+
+	static PhysXAllocator* s_px_allocator;
+	static PhysXError* s_px_error;
+	static PxFoundation* s_foundation;
+	static PxPhysics* s_physics;
+
+	void init()
+	{
+		s_px_allocator = CE_NEW(default_allocator(), PhysXAllocator)();
+		s_px_error = CE_NEW(default_allocator(), PhysXError)();
+
+		s_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, *s_px_allocator, *s_px_error);
+		CE_ASSERT(s_foundation, "Unable to create PhysX Foundation");
+
+		s_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *s_foundation, physx::PxTolerancesScale());
+		CE_ASSERT(s_physics, "Unable to create PhysX Physics");
+
+		bool extension = PxInitExtensions(*s_physics);
+		CE_ASSERT(extension, "Unable to initialize PhysX Extensions");
+	}
+
+	void shutdown()
+	{
+		PxCloseExtensions();
+		s_physics->release();
+		s_foundation->release();
+
+		CE_DELETE(default_allocator(), s_px_error);
+		CE_DELETE(default_allocator(), s_px_allocator);
+	}
+} // namespace physics_system
+
 PxFilterFlags PhysicsFilterShader(PxFilterObjectAttributes attributes0, PxFilterData filterData0, 
 								PxFilterObjectAttributes attributes1, PxFilterData filterData1,
 								PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
@@ -94,7 +196,7 @@ PhysicsWorld::PhysicsWorld()
 	, m_controllers_pool(default_allocator(), MAX_CONTROLLERS, sizeof(Controller), CE_ALIGNOF(Controller))
 	, m_triggers_pool(default_allocator(), MAX_TRIGGERS, sizeof(Trigger), CE_ALIGNOF(Trigger))
 {
-	PxSceneDesc scene_desc(device()->physx()->getTolerancesScale());
+	PxSceneDesc scene_desc(physics_system::s_physics->getTolerancesScale());
 	scene_desc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
 
 	if(!scene_desc.cpuDispatcher)
@@ -115,12 +217,12 @@ PhysicsWorld::PhysicsWorld()
 	scene_desc.simulationEventCallback = m_callback;
 
 	// Create scene
-	m_scene = device()->physx()->createScene(scene_desc);
+	m_scene = physics_system::s_physics->createScene(scene_desc);
 
 	m_scene->setFlag(PxSceneFlag::eENABLE_KINEMATIC_STATIC_PAIRS, true);
 
 	// Create controller manager
-	m_controller_manager = PxCreateControllerManager(device()->physx()->getFoundation());
+	m_controller_manager = PxCreateControllerManager(*physics_system::s_foundation);
 	CE_ASSERT(m_controller_manager != NULL, "Failed to create PhysX controller manager");
 }
 
@@ -136,7 +238,7 @@ PhysicsWorld::~PhysicsWorld()
 //-----------------------------------------------------------------------------
 ActorId	PhysicsWorld::create_actor(const PhysicsResource* res, const uint32_t index, SceneGraph& sg, int32_t node)
 {
-	Actor* actor = CE_NEW(m_actors_pool, Actor)(res, index, m_scene, sg, node, Vector3::ZERO, Quaternion::IDENTITY);
+	Actor* actor = CE_NEW(m_actors_pool, Actor)(res, index, physics_system::s_physics, m_scene, sg, node, Vector3::ZERO, Quaternion::IDENTITY);
 	return m_actors.create(actor);
 }
 
@@ -152,7 +254,7 @@ void PhysicsWorld::destroy_actor(ActorId id)
 //-----------------------------------------------------------------------------
 ControllerId PhysicsWorld::create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node)
 {
-	Controller* controller = CE_NEW(m_controllers_pool, Controller)(pr, sg, node, m_scene, m_controller_manager);
+	Controller* controller = CE_NEW(m_controllers_pool, Controller)(pr, sg, node, physics_system::s_physics, m_scene, m_controller_manager);
 	return m_controllers.create(controller);
 }
 
@@ -168,7 +270,7 @@ void PhysicsWorld::destroy_controller(ControllerId id)
 //-----------------------------------------------------------------------------
 TriggerId PhysicsWorld::create_trigger(const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
 {
-	Trigger* trigger = CE_NEW(m_triggers_pool, Trigger)(m_scene, half_extents, pos, rot);
+	Trigger* trigger = CE_NEW(m_triggers_pool, Trigger)(physics_system::s_physics, m_scene, half_extents, pos, rot);
 	return m_triggers.create(trigger);
 }
 

+ 11 - 0
engine/physics/PhysicsWorld.h

@@ -48,6 +48,17 @@ using physx::PxActor;
 namespace crown
 {
 
+/// Global physics-related functions
+namespace physics_system
+{
+	/// Initializes the physics system.
+	/// This is the place where to create and initialize per-application objects.
+	void init();
+
+	/// It should reverse the actions performed by audio_system::init().
+	void shutdown();
+} // namespace physics_system
+
 //-----------------------------------------------------------------------------
 struct PhysicsResource;
 struct PhysicsActor;

+ 3 - 5
engine/physics/Trigger.cpp

@@ -24,9 +24,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "Device.h"
 #include "Matrix4x4.h"
-#include "Physics.h"
 #include "PxPhysicsAPI.h"
 #include "Quaternion.h"
 #include "Trigger.h"
@@ -51,17 +49,17 @@ namespace crown
 {
 	
 //-----------------------------------------------------------------------------
-Trigger::Trigger(PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
+Trigger::Trigger(PxPhysics* physics, PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
 	: m_scene(scene)
 {
 	Matrix4x4 m(rot, pos);
 	m.transpose();
 	PxMat44 pose((PxReal*)(m.to_float_ptr()));
 
-	m_actor = device()->physx()->createRigidStatic(PxTransform(pose));
+	m_actor = physics->createRigidStatic(PxTransform(pose));
 	m_actor->userData = NULL;
 
-	m_mat = device()->physx()->createMaterial(0.5f, 0.5f, 0.5f);
+	m_mat = physics->createMaterial(0.5f, 0.5f, 0.5f);
 	PxShape* shape = m_actor->createShape(PxBoxGeometry(half_extents.x, half_extents.y, half_extents.z), *m_mat);
 	shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
 	shape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);

+ 2 - 1
engine/physics/Trigger.h

@@ -39,6 +39,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 using physx::PxRigidActor;
 using physx::PxMaterial;
 using physx::PxScene;
+using physx::PxPhysics;
 
 namespace crown
 {
@@ -50,7 +51,7 @@ class SceneGraph;
 
 struct Trigger
 {
-						Trigger(PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot);
+						Trigger(PxPhysics* physics, PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot);
 						~Trigger();
 
 public: