aboutsummaryrefslogtreecommitdiff
path: root/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxManagerImpl.h
blob: 1f5e51033284cc45db11ca6d13c29d3c9a296fea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2016-2017, NVIDIA CORPORATION.  All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto.  Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/

#ifndef NVBLASTEXTPXMANAGERIMPL_H
#define NVBLASTEXTPXMANAGERIMPL_H

#include "NvBlastExtPxManager.h"
#include "NvBlastExtArray.h"
#include "NvBlastExtHashMap.h"
#include "NvBlastExtPxListener.h"
#include "NvBlastExtPxFamily.h"

#include "PxRigidDynamic.h"


using namespace physx;


namespace Nv
{
namespace Blast
{

// Forward declarations
class TkActor;

class ExtPxManagerImpl final : public ExtPxManager
{
	NV_NOCOPY(ExtPxManagerImpl)

public:
	friend class ExtPxActorImpl;
	friend class ExtPxFamilyImpl;

	ExtPxManagerImpl(PxPhysics& physics, TkFramework&framework, ExtPxCreateJointFunction createFn, bool usePxUserData)
		: m_physics(physics), m_framework(framework), m_createJointFn(createFn), m_usePxUserData(usePxUserData), m_actorCountLimit(0)
	{
	}

	~ExtPxManagerImpl()
	{
	}

	virtual void release() override;


	//////// interface ////////

	virtual ExtPxFamily*	createFamily(const ExtPxFamilyDesc& desc) override;

	virtual bool			createJoint(TkJoint& joint) override;

	virtual void			destroyJoint(TkJoint& joint) override;

	virtual void			setCreateJointFunction(ExtPxCreateJointFunction createFn) override
	{
		m_createJointFn = createFn;
	}

	virtual uint32_t		getFamilyCount() const override
	{
		return m_tkFamiliesMap.size();
	}

	virtual uint32_t		getFamilies(ExtPxFamily** buffer, uint32_t bufferSize) const override
	{
		uint32_t index = 0;
		for (auto it = const_cast<ExtPxManagerImpl*>(this)->m_tkFamiliesMap.getIterator(); !it.done() && index < bufferSize; ++it)
		{
			buffer[index++] = it->second;
		}
		return index;
	}

	virtual ExtPxFamily*	getFamilyFromTkFamily(TkFamily& family) const override
	{
		auto entry = m_tkFamiliesMap.find(&family);
		return entry != nullptr ? entry->second : nullptr;
	}

	virtual ExtPxActor*		getActorFromPhysXActor(const PxRigidDynamic& pxActor) const override
	{
		auto it = m_physXActorsMap.find(&pxActor);
		return it != nullptr ? it->second : nullptr;
	}

	virtual PxPhysics&		getPhysics() const override
	{
		return m_physics;
	}

	virtual TkFramework&	getFramework() const override
	{
		return m_framework;
	}

	virtual bool			isPxUserDataUsed() const override
	{
		return m_usePxUserData;
	}

	virtual void			subscribe(ExtPxListener& listener) override
	{
		m_listeners.pushBack(&listener);
	}

	virtual void			unsubscribe(ExtPxListener& listener) override
	{
		m_listeners.findAndReplaceWithLast(&listener);
	}

	virtual void			setActorCountLimit(uint32_t limit) override
	{
		m_actorCountLimit = limit;
	}

	virtual uint32_t		getActorCountLimit() override
	{
		return m_actorCountLimit;
	}

	virtual uint32_t		getPxActorCount() const override
	{
		return m_physXActorsMap.size();
	}


	//////// internal public methods ////////

	void					registerActor(PxRigidDynamic* pxActor, ExtPxActor* actor)
	{
		if (m_usePxUserData)
		{
			pxActor->userData = actor;
		}
		m_physXActorsMap[pxActor] = actor;
	}

	void					unregisterActor(PxRigidDynamic* pxActor)
	{
		if (m_usePxUserData)
		{
			pxActor->userData = nullptr;
		}
		m_physXActorsMap.erase(pxActor);
	}

	void					registerFamily(ExtPxFamily& family)
	{
		m_tkFamiliesMap[&family.getTkFamily()] = &family;
	}

	void					unregisterFamily(ExtPxFamily& family)
	{
		m_tkFamiliesMap.erase(&family.getTkFamily());
	}

	void					updateJoint(TkJoint& joint);


	//////// events dispatch ////////

	void					dispatchActorCreated(ExtPxFamily& family, ExtPxActor& actor)
	{
		for (ExtPxListener* listener : m_listeners)
			listener->onActorCreated(family, actor);
	}

	void					dispatchActorDestroyed(ExtPxFamily& family, ExtPxActor&actor)
	{
		for (ExtPxListener* listener : m_listeners)
			listener->onActorDestroyed(family, actor);
	}


private:

	//////// data ////////

	PxPhysics&												m_physics;
	TkFramework&											m_framework;
	ExtPxCreateJointFunction								m_createJointFn;
	bool													m_usePxUserData;
	ExtInlineArray<ExtPxListener*, 8>::type					m_listeners;
	ExtHashMap<const PxRigidDynamic*, ExtPxActor*>::type	m_physXActorsMap;
	ExtHashMap<TkFamily*, ExtPxFamily*>::type				m_tkFamiliesMap;
	ExtHashMap<TkActor*, ExtArray<TkJoint*>::type >::type	m_incompleteJointMultiMap;
	uint32_t												m_actorCountLimit;
};

} // namespace Blast
} // namespace Nv


#endif // ifndef NVBLASTEXTPXMANAGERIMPL_H