////////////////////////////////////////////////////////////////////////////////// // SPARK particle engine // // Copyright (C) 2008-2009 - Julien Fryer - julienfryer@gmail.com // // // // This software is provided 'as-is', without any express or implied // // warranty. In no event will the authors be held liable for any damages // // arising from the use of this software. // // // // Permission is granted to anyone to use this software for any purpose, // // including commercial applications, and to alter it and redistribute it // // freely, subject to the following restrictions: // // // // 1. The origin of this software must not be misrepresented; you must not // // claim that you wrote the original software. If you use this software // // in a product, an acknowledgment in the product documentation would be // // appreciated but is not required. // // 2. Altered source versions must be plainly marked as such, and must not be // // misrepresented as being the original software. // // 3. This notice may not be removed or altered from any source distribution. // ////////////////////////////////////////////////////////////////////////////////// #ifndef H_SPK_ARRAYBUFFER #define H_SPK_ARRAYBUFFER #include "Core/SPK_Buffer.h" namespace SPK { class Group; template class ArrayBufferCreator; /** * @brief A template buffer that holds an array of elements of type T * @since 1.03.02 */ template class ArrayBuffer : public Buffer { friend class ArrayBufferCreator; public : /** * @brief Gets the starting address of the inner array * @return the starting address of the first element of the inner array */ inline T* getData() const; /** * @brief Gets the number of elements for a single particle * @return the number of elements for a single particle */ inline const size_t getParticleSize() const; /** * @brief Gets the total number of T in the inner array * The value is equal to particleSize * nbParticles. * @return the total number of T in the inner array */ inline size_t getDataSize() const; private : T* data; size_t particleSize; size_t dataSize; ArrayBuffer(size_t nbParticles,size_t particleSize); ArrayBuffer(const ArrayBuffer& buffer); virtual ~ArrayBuffer(); virtual void swap(size_t index0,size_t index1); }; /** * @brief Template class to create an ArrayBuffer * @since 1.03.02 */ template class ArrayBufferCreator : public BufferCreator { public : /** * @brief Constructor of ArrayBuffer * @param particleSize : the number of elements per particle in the buffer to be created */ ArrayBufferCreator(size_t particleSize); private : size_t particleSize; virtual ArrayBuffer* createBuffer(size_t nbParticles,const Group& group) const; }; // Typedefs /** @brief A buffer storing an array of floats */ typedef ArrayBuffer FloatBuffer; /** @brief the buffer creator of the float buffer */ typedef ArrayBufferCreator FloatBufferCreator; template ArrayBuffer::ArrayBuffer(size_t nbParticles,size_t particleSize) : Buffer(), dataSize(nbParticles * particleSize), particleSize(particleSize) { data = new T[dataSize]; } template ArrayBuffer::ArrayBuffer(const ArrayBuffer& buffer) : Buffer(buffer), dataSize(buffer.dataSize), particleSize(buffer.particleSize) { data = new T[dataSize]; memcpy(data,buffer.data,dataSize * sizeof(T)); } template ArrayBuffer::~ArrayBuffer() { delete[] data; } template inline T* ArrayBuffer::getData() const { return data; } template inline const size_t ArrayBuffer::getParticleSize() const { return particleSize; } template inline size_t ArrayBuffer::getDataSize() const { return dataSize; } template void ArrayBuffer::swap(size_t index0,size_t index1) { T* address0 = data + index0 * particleSize; T* address1 = data + index1 * particleSize; for (size_t i = 0; i < particleSize; ++i) std::swap(address0[i],address1[i]); } template ArrayBufferCreator::ArrayBufferCreator(size_t particleSize) : BufferCreator(), particleSize(particleSize) {} template ArrayBuffer* ArrayBufferCreator::createBuffer(size_t nbParticles,const Group& group) const { return new ArrayBuffer(nbParticles,particleSize); } } #endif