Fix for issue 150

This commit is contained in:
Mikko Mononen 2011-02-03 09:48:16 +00:00
parent 98e7390ba0
commit 9bb9abad33
4 changed files with 20 additions and 18 deletions

View File

@ -270,7 +270,7 @@ void duDebugDrawNavMeshNodes(struct duDebugDraw* dd, const dtNavMeshQuery& query
dd->begin(DU_DRAW_POINTS, 4.0f); dd->begin(DU_DRAW_POINTS, 4.0f);
for (int i = 0; i < pool->getHashSize(); ++i) for (int i = 0; i < pool->getHashSize(); ++i)
{ {
for (unsigned short j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j)) for (dtNodeIndex j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j))
{ {
const dtNode* node = pool->getNodeAtIdx(j+1); const dtNode* node = pool->getNodeAtIdx(j+1);
if (!node) continue; if (!node) continue;
@ -282,7 +282,7 @@ void duDebugDrawNavMeshNodes(struct duDebugDraw* dd, const dtNavMeshQuery& query
dd->begin(DU_DRAW_LINES, 2.0f); dd->begin(DU_DRAW_LINES, 2.0f);
for (int i = 0; i < pool->getHashSize(); ++i) for (int i = 0; i < pool->getHashSize(); ++i)
{ {
for (unsigned short j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j)) for (dtNodeIndex j = pool->getFirst(i); j != DT_NULL_IDX; j = pool->getNext(j))
{ {
const dtNode* node = pool->getNodeAtIdx(j+1); const dtNode* node = pool->getNodeAtIdx(j+1);
if (!node) continue; if (!node) continue;

View File

@ -27,7 +27,8 @@ enum dtNodeFlags
DT_NODE_CLOSED = 0x02, DT_NODE_CLOSED = 0x02,
}; };
static const unsigned short DT_NULL_IDX = 0xffff; typedef unsigned short dtNodeIndex;
static const dtNodeIndex DT_NULL_IDX = ~0;
struct dtNode struct dtNode
{ {
@ -39,6 +40,7 @@ struct dtNode
dtPolyRef id; // Polygon ref the node corresponds to. dtPolyRef id; // Polygon ref the node corresponds to.
}; };
class dtNodePool class dtNodePool
{ {
public: public:
@ -70,22 +72,22 @@ public:
inline int getMemUsed() const inline int getMemUsed() const
{ {
return sizeof(*this) + return sizeof(*this) +
sizeof(dtNode)*m_maxNodes + sizeof(dtNode)*m_maxNodes +
sizeof(unsigned short)*m_maxNodes + sizeof(dtNodeIndex)*m_maxNodes +
sizeof(unsigned short)*m_hashSize; sizeof(dtNodeIndex)*m_hashSize;
} }
inline int getMaxNodes() const { return m_maxNodes; } inline int getMaxNodes() const { return m_maxNodes; }
inline int getHashSize() const { return m_hashSize; } inline int getHashSize() const { return m_hashSize; }
inline unsigned short getFirst(int bucket) const { return m_first[bucket]; } inline dtNodeIndex getFirst(int bucket) const { return m_first[bucket]; }
inline unsigned short getNext(int i) const { return m_next[i]; } inline dtNodeIndex getNext(int i) const { return m_next[i]; }
private: private:
dtNode* m_nodes; dtNode* m_nodes;
unsigned short* m_first; dtNodeIndex* m_first;
unsigned short* m_next; dtNodeIndex* m_next;
const int m_maxNodes; const int m_maxNodes;
const int m_hashSize; const int m_hashSize;
int m_nodeCount; int m_nodeCount;

View File

@ -46,15 +46,15 @@ dtNodePool::dtNodePool(int maxNodes, int hashSize) :
dtAssert(m_maxNodes > 0); dtAssert(m_maxNodes > 0);
m_nodes = (dtNode*)dtAlloc(sizeof(dtNode)*m_maxNodes, DT_ALLOC_PERM); m_nodes = (dtNode*)dtAlloc(sizeof(dtNode)*m_maxNodes, DT_ALLOC_PERM);
m_next = (unsigned short*)dtAlloc(sizeof(unsigned short)*m_maxNodes, DT_ALLOC_PERM); m_next = (dtNodeIndex*)dtAlloc(sizeof(dtNodeIndex)*m_maxNodes, DT_ALLOC_PERM);
m_first = (unsigned short*)dtAlloc(sizeof(unsigned short)*hashSize, DT_ALLOC_PERM); m_first = (dtNodeIndex*)dtAlloc(sizeof(dtNodeIndex)*hashSize, DT_ALLOC_PERM);
dtAssert(m_nodes); dtAssert(m_nodes);
dtAssert(m_next); dtAssert(m_next);
dtAssert(m_first); dtAssert(m_first);
memset(m_first, 0xff, sizeof(unsigned short)*m_hashSize); memset(m_first, 0xff, sizeof(dtNodeIndex)*m_hashSize);
memset(m_next, 0xff, sizeof(unsigned short)*m_maxNodes); memset(m_next, 0xff, sizeof(dtNodeIndex)*m_maxNodes);
} }
dtNodePool::~dtNodePool() dtNodePool::~dtNodePool()
@ -66,14 +66,14 @@ dtNodePool::~dtNodePool()
void dtNodePool::clear() void dtNodePool::clear()
{ {
memset(m_first, 0xff, sizeof(unsigned short)*m_hashSize); memset(m_first, 0xff, sizeof(dtNodeIndex)*m_hashSize);
m_nodeCount = 0; m_nodeCount = 0;
} }
dtNode* dtNodePool::findNode(dtPolyRef id) dtNode* dtNodePool::findNode(dtPolyRef id)
{ {
unsigned int bucket = dtHashRef(id) & (m_hashSize-1); unsigned int bucket = dtHashRef(id) & (m_hashSize-1);
unsigned short i = m_first[bucket]; dtNodeIndex i = m_first[bucket];
while (i != DT_NULL_IDX) while (i != DT_NULL_IDX)
{ {
if (m_nodes[i].id == id) if (m_nodes[i].id == id)
@ -86,7 +86,7 @@ dtNode* dtNodePool::findNode(dtPolyRef id)
dtNode* dtNodePool::getNode(dtPolyRef id) dtNode* dtNodePool::getNode(dtPolyRef id)
{ {
unsigned int bucket = dtHashRef(id) & (m_hashSize-1); unsigned int bucket = dtHashRef(id) & (m_hashSize-1);
unsigned short i = m_first[bucket]; dtNodeIndex i = m_first[bucket];
dtNode* node = 0; dtNode* node = 0;
while (i != DT_NULL_IDX) while (i != DT_NULL_IDX)
{ {
@ -98,7 +98,7 @@ dtNode* dtNodePool::getNode(dtPolyRef id)
if (m_nodeCount >= m_maxNodes) if (m_nodeCount >= m_maxNodes)
return 0; return 0;
i = (unsigned short)m_nodeCount; i = (dtNodeIndex)m_nodeCount;
m_nodeCount++; m_nodeCount++;
// Init node // Init node