diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h index 751169c6..be383e98 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Array2D.h @@ -26,22 +26,23 @@ distribution. namespace PolyVox { - template + template class Array2D { public: Array2D(uint32_t width, uint32_t height) - :m_uWidth(width) - , m_uHeight(height) - , m_pData(0) + :m_pData(0) { - m_pData = new ElementType[m_uWidth * m_uHeight]; + m_uDimensions[0] = width; + m_uDimensions[1] = height; + + initialize(); } // These are deleted to avoid accidental copying. - Array2D(const Array2D&) = delete; - Array2D& operator=(const Array2D&) = delete; + Array2D(const Array2D&) = delete; + Array2D& operator=(const Array2D&) = delete; ~Array2D() { @@ -50,14 +51,14 @@ namespace PolyVox ElementType operator()(uint32_t x, uint32_t y) const { - POLYVOX_ASSERT((x < m_uWidth && y < m_uHeight), "Array access is out-of-range."); - return m_pData[y * m_uWidth + x]; + POLYVOX_ASSERT((x < m_uDimensions[0] && y < m_uDimensions[1]), "Array access is out-of-range."); + return m_pData[y * m_uDimensions[0] + x]; } ElementType& operator()(uint32_t x, uint32_t y) { - POLYVOX_ASSERT((x < m_uWidth && y < m_uHeight), "Array access is out-of-range."); - return m_pData[y * m_uWidth + x]; + POLYVOX_ASSERT((x < m_uDimensions[0] && y < m_uDimensions[1]), "Array access is out-of-range."); + return m_pData[y * m_uDimensions[0] + x]; } ElementType* getRawData() @@ -67,7 +68,7 @@ namespace PolyVox size_t getNoOfElements() { - return m_uWidth * m_uHeight; + return m_uNoOfElements; } void swap(Array2D& other) @@ -79,27 +80,38 @@ namespace PolyVox private: - uint32_t m_uWidth; - uint32_t m_uHeight; + void initialize(void) + { + // Calculate the total number of elements in the array. + m_uNoOfElements = 1; + for (uint32_t i = 0; i < noOfDims; i++) + { + m_uNoOfElements *= m_uDimensions[i]; + } + m_pData = new ElementType[m_uNoOfElements]; + } + + uint32_t m_uDimensions[noOfDims]; + uint32_t m_uNoOfElements; ElementType* m_pData; }; ///A 2D Array of floats. - typedef Array2D Array2DFloat; + typedef Array2D<2, float> Array2DFloat; ///A 2D Array of doubles. - typedef Array2D Array2DDouble; + typedef Array2D<2, double> Array2DDouble; ///A 2D Array of signed 8-bit values. - typedef Array2D Array2DInt8; + typedef Array2D<2, int8_t> Array2DInt8; ///A 2D Array of unsigned 8-bit values. - typedef Array2D Array2DUint8; + typedef Array2D<2, uint8_t> Array2DUint8; ///A 2D Array of signed 16-bit values. - typedef Array2D Array2DInt16; + typedef Array2D<2, int16_t> Array2DInt16; ///A 2D Array of unsigned 16-bit values. - typedef Array2D Array2DUint16; + typedef Array2D<2, uint16_t> Array2DUint16; ///A 2D Array of signed 32-bit values. - typedef Array2D Array2DInt32; + typedef Array2D<2, int32_t> Array2DInt32; ///A 2D Array of unsigned 32-bit values. - typedef Array2D Array2DUint32; + typedef Array2D<2, uint32_t> Array2DUint32; } #endif //__PolyVox_Array2D_H__ \ No newline at end of file