diff options
Diffstat (limited to 'thirdparty/recastnavigation/Recast/Source/Recast.cpp')
-rw-r--r-- | thirdparty/recastnavigation/Recast/Source/Recast.cpp | 165 |
1 files changed, 118 insertions, 47 deletions
diff --git a/thirdparty/recastnavigation/Recast/Source/Recast.cpp b/thirdparty/recastnavigation/Recast/Source/Recast.cpp index 8308d1973e..1b71710cdc 100644 --- a/thirdparty/recastnavigation/Recast/Source/Recast.cpp +++ b/thirdparty/recastnavigation/Recast/Source/Recast.cpp @@ -23,11 +23,34 @@ #include <stdlib.h> #include <stdio.h> #include <stdarg.h> -#include <new> #include "Recast.h" #include "RecastAlloc.h" #include "RecastAssert.h" +namespace +{ +/// Allocates and constructs an object of the given type, returning a pointer. +/// TODO: Support constructor args. +/// @param[in] hint Hint to the allocator. +template <typename T> +T* rcNew(rcAllocHint hint) { + T* ptr = (T*)rcAlloc(sizeof(T), hint); + ::new(rcNewTag(), (void*)ptr) T(); + return ptr; +} + +/// Destroys and frees an object allocated with rcNew. +/// @param[in] ptr The object pointer to delete. +template <typename T> +void rcDelete(T* ptr) { + if (ptr) { + ptr->~T(); + rcFree((void*)ptr); + } +} +} // namespace + + float rcSqrt(float x) { return sqrtf(x); @@ -73,9 +96,8 @@ void rcContext::log(const rcLogCategory category, const char* format, ...) rcHeightfield* rcAllocHeightfield() { - return new (rcAlloc(sizeof(rcHeightfield), RC_ALLOC_PERM)) rcHeightfield; + return rcNew<rcHeightfield>(RC_ALLOC_PERM); } - rcHeightfield::rcHeightfield() : width() , height() @@ -104,84 +126,133 @@ rcHeightfield::~rcHeightfield() void rcFreeHeightField(rcHeightfield* hf) { - if (!hf) return; - hf->~rcHeightfield(); - rcFree(hf); + rcDelete(hf); } rcCompactHeightfield* rcAllocCompactHeightfield() { - rcCompactHeightfield* chf = (rcCompactHeightfield*)rcAlloc(sizeof(rcCompactHeightfield), RC_ALLOC_PERM); - memset(chf, 0, sizeof(rcCompactHeightfield)); - return chf; + return rcNew<rcCompactHeightfield>(RC_ALLOC_PERM); } void rcFreeCompactHeightfield(rcCompactHeightfield* chf) { - if (!chf) return; - rcFree(chf->cells); - rcFree(chf->spans); - rcFree(chf->dist); - rcFree(chf->areas); - rcFree(chf); + rcDelete(chf); } -rcHeightfieldLayerSet* rcAllocHeightfieldLayerSet() +rcCompactHeightfield::rcCompactHeightfield() + : width(), + height(), + spanCount(), + walkableHeight(), + walkableClimb(), + borderSize(), + maxDistance(), + maxRegions(), + bmin(), + bmax(), + cs(), + ch(), + cells(), + spans(), + dist(), + areas() { - rcHeightfieldLayerSet* lset = (rcHeightfieldLayerSet*)rcAlloc(sizeof(rcHeightfieldLayerSet), RC_ALLOC_PERM); - memset(lset, 0, sizeof(rcHeightfieldLayerSet)); - return lset; +} +rcCompactHeightfield::~rcCompactHeightfield() +{ + rcFree(cells); + rcFree(spans); + rcFree(dist); + rcFree(areas); } +rcHeightfieldLayerSet* rcAllocHeightfieldLayerSet() +{ + return rcNew<rcHeightfieldLayerSet>(RC_ALLOC_PERM); +} void rcFreeHeightfieldLayerSet(rcHeightfieldLayerSet* lset) { - if (!lset) return; - for (int i = 0; i < lset->nlayers; ++i) + rcDelete(lset); +} + +rcHeightfieldLayerSet::rcHeightfieldLayerSet() + : layers(), nlayers() {} +rcHeightfieldLayerSet::~rcHeightfieldLayerSet() +{ + for (int i = 0; i < nlayers; ++i) { - rcFree(lset->layers[i].heights); - rcFree(lset->layers[i].areas); - rcFree(lset->layers[i].cons); + rcFree(layers[i].heights); + rcFree(layers[i].areas); + rcFree(layers[i].cons); } - rcFree(lset->layers); - rcFree(lset); + rcFree(layers); } rcContourSet* rcAllocContourSet() { - rcContourSet* cset = (rcContourSet*)rcAlloc(sizeof(rcContourSet), RC_ALLOC_PERM); - memset(cset, 0, sizeof(rcContourSet)); - return cset; + return rcNew<rcContourSet>(RC_ALLOC_PERM); } - void rcFreeContourSet(rcContourSet* cset) { - if (!cset) return; - for (int i = 0; i < cset->nconts; ++i) + rcDelete(cset); +} + +rcContourSet::rcContourSet() + : conts(), + nconts(), + bmin(), + bmax(), + cs(), + ch(), + width(), + height(), + borderSize(), + maxError() {} +rcContourSet::~rcContourSet() +{ + for (int i = 0; i < nconts; ++i) { - rcFree(cset->conts[i].verts); - rcFree(cset->conts[i].rverts); + rcFree(conts[i].verts); + rcFree(conts[i].rverts); } - rcFree(cset->conts); - rcFree(cset); + rcFree(conts); } + rcPolyMesh* rcAllocPolyMesh() { - rcPolyMesh* pmesh = (rcPolyMesh*)rcAlloc(sizeof(rcPolyMesh), RC_ALLOC_PERM); - memset(pmesh, 0, sizeof(rcPolyMesh)); - return pmesh; + return rcNew<rcPolyMesh>(RC_ALLOC_PERM); } - void rcFreePolyMesh(rcPolyMesh* pmesh) { - if (!pmesh) return; - rcFree(pmesh->verts); - rcFree(pmesh->polys); - rcFree(pmesh->regs); - rcFree(pmesh->flags); - rcFree(pmesh->areas); - rcFree(pmesh); + rcDelete(pmesh); +} + +rcPolyMesh::rcPolyMesh() + : verts(), + polys(), + regs(), + flags(), + areas(), + nverts(), + npolys(), + maxpolys(), + nvp(), + bmin(), + bmax(), + cs(), + ch(), + borderSize(), + maxEdgeError() {} + +rcPolyMesh::~rcPolyMesh() +{ + rcFree(verts); + rcFree(polys); + rcFree(regs); + rcFree(flags); + rcFree(areas); } rcPolyMeshDetail* rcAllocPolyMeshDetail() |