diff --git a/src/framework/graphics/indexbuffer.cpp b/src/framework/graphics/indexbuffer.cpp index b372b79..91a891f 100644 --- a/src/framework/graphics/indexbuffer.cpp +++ b/src/framework/graphics/indexbuffer.cpp @@ -4,25 +4,10 @@ #include -IndexBuffer::IndexBuffer(uint32_t numIndices, BOOL isStatic) - : BufferObject(BUFFEROBJECT_TYPE_INDEX, isStatic ? BUFFEROBJECT_USAGE_STATIC : BUFFEROBJECT_USAGE_DYNAMIC) +IndexBuffer::IndexBuffer() { STACK_TRACE; m_currentIndex = 0; - Resize(numIndices); -} - -IndexBuffer::IndexBuffer(const IndexBuffer *source) - : BufferObject(BUFFEROBJECT_TYPE_INDEX, source->GetUsage()) -{ - STACK_TRACE; - ASSERT(source != NULL); - ASSERT(source->GetNumElements() > 0); - - m_currentIndex = 0; - Resize(source->GetNumElements()); - - memcpy(&m_buffer[0], source->GetBuffer(), GetNumElements() * GetElementWidthInBytes()); } IndexBuffer::~IndexBuffer() @@ -30,6 +15,57 @@ IndexBuffer::~IndexBuffer() STACK_TRACE; } +BOOL IndexBuffer::Initialize(uint32_t numIndices, BUFFEROBJECT_USAGE usage) +{ + STACK_TRACE; + return Initialize(NULL, numIndices, usage); +} + +BOOL IndexBuffer::Initialize(GraphicsDevice *graphicsDevice, uint32_t numIndices, BUFFEROBJECT_USAGE usage) +{ + STACK_TRACE; + ASSERT(m_buffer.size() == 0); + if (m_buffer.size() > 0) + return FALSE; + + ASSERT(numIndices > 0); + if (numIndices == 0) + return FALSE; + + if (!BufferObject::Initialize(graphicsDevice, BUFFEROBJECT_TYPE_INDEX, usage)) + return FALSE; + + Resize(numIndices); + + return TRUE; +} + +BOOL IndexBuffer::Initialize(const IndexBuffer *source) +{ + STACK_TRACE; + return Initialize(NULL, source); +} + +BOOL IndexBuffer::Initialize(GraphicsDevice *graphicsDevice, const IndexBuffer *source) +{ + STACK_TRACE; + ASSERT(m_buffer.size() == 0); + if (m_buffer.size() > 0) + return FALSE; + + ASSERT(source != NULL); + if (source == NULL) + return FALSE; + + ASSERT(source->GetNumElements() > 0); + if (source->GetNumElements() == 0) + return FALSE; + + Resize(source->GetNumElements()); + + memcpy(&m_buffer[0], source->GetBuffer(), GetNumElements() * GetElementWidthInBytes()); +} + void IndexBuffer::Set(const uint16_t *indices, uint32_t numIndices) { STACK_TRACE; @@ -39,6 +75,10 @@ void IndexBuffer::Set(const uint16_t *indices, uint32_t numIndices) void IndexBuffer::Resize(uint32_t numIndices) { STACK_TRACE; + ASSERT(numIndices > 0); + if (numIndices == 0) + return; + m_buffer.resize(numIndices, 0); if (!IsClientSideBuffer()) diff --git a/src/framework/graphics/indexbuffer.h b/src/framework/graphics/indexbuffer.h index 1cab64a..2f270c7 100644 --- a/src/framework/graphics/indexbuffer.h +++ b/src/framework/graphics/indexbuffer.h @@ -7,6 +7,8 @@ #include // for abs() #include +class GraphicsDevice; + /** * Wraps management of an array of vertex indexes to be used for * optimized rendering of vertices. @@ -14,21 +16,42 @@ class IndexBuffer : public BufferObject { public: - /** - * Creates an index buffer. - * @param numIndices the initial number of indices the buffer should hold - * @param isStatic whether the index data is static or dynamic (used as - * a hint for index data stored in video memory) - */ - IndexBuffer(uint32_t numIndices, BOOL isStatic); - - /** - * Creates an index buffer. - * @param source the source buffer to copy during creation of this buffer - */ - IndexBuffer(const IndexBuffer *source); - + IndexBuffer(); virtual ~IndexBuffer(); + + /** + * Initializes the index buffer. + * @param numIndices the initial number of indices the buffer should hold + * @param usage the expected usage pattern of this index buffer + * @return TRUE if successful, FALSE if not + */ + BOOL Initialize(uint32_t numIndices, BUFFEROBJECT_USAGE usage); + + /** + * Initializes the index buffer. + * @param graphicsDevice the graphics device to use to create this buffer + * on the GPU + * @param numIndices the initial number of indices the buffer should hold + * @param usage the expected usage pattern of this index buffer + * @return TRUE if successful, FALSE if not + */ + BOOL Initialize(GraphicsDevice *graphicsDevice, uint32_t numIndices, BUFFEROBJECT_USAGE usage); + + /** + * Initializes the index buffer. + * @param source the source buffer to copy during creation of this buffer + * @return TRUE if successful, FALSE if not + */ + BOOL Initialize(const IndexBuffer *source); + + /** + * Initializes the index buffer. + * @param graphicsDevice the graphics device to use to create this buffer + * on the GPU + * @param source the source buffer to copy during creation of this buffer + * @return TRUE if successful, FALSE if not + */ + BOOL Initialize(GraphicsDevice *graphicsDevice, const IndexBuffer *source); /** * Copies the source indices over top of this buffer's existing indices.