diff --git a/include/Nazara/Utility/VertexDeclaration.hpp b/include/Nazara/Utility/VertexDeclaration.hpp index 0bb4f7f18..f4db3bae2 100644 --- a/include/Nazara/Utility/VertexDeclaration.hpp +++ b/include/Nazara/Utility/VertexDeclaration.hpp @@ -2,6 +2,8 @@ // This file is part of the "Nazara Engine - Utility module" // For conditions of distribution and use, see copyright notice in Config.hpp +#pragma once + #ifndef NAZARA_VERTEXDECLARATION_HPP #define NAZARA_VERTEXDECLARATION_HPP @@ -39,6 +41,9 @@ class NAZARA_API NzVertexDeclaration : public NzResource unsigned int GetElementCount(nzElementStream stream) const; unsigned int GetStride(nzElementStream stream) const; + bool HasElement(unsigned int i) const; + bool HasElement(nzElementStream stream, unsigned int i) const; + bool HasElement(nzElementStream stream, nzElementUsage usage, unsigned int usageIndex = 0) const; bool HasStream(nzElementStream stream) const; bool IsValid() const; diff --git a/src/Nazara/Utility/VertexDeclaration.cpp b/src/Nazara/Utility/VertexDeclaration.cpp index e4d24b0ec..6b5ec7e2f 100644 --- a/src/Nazara/Utility/VertexDeclaration.cpp +++ b/src/Nazara/Utility/VertexDeclaration.cpp @@ -244,16 +244,34 @@ const NzVertexElement* NzVertexDeclaration::GetElement(nzElementStream stream, n #endif int elementPos = m_sharedImpl->elementPos[stream][usage]; + + #if NAZARA_UTILITY_SAFE if (elementPos == -1) + { + NazaraError("Element not found"); return nullptr; + } + #endif elementPos += usageIndex; + + #if NAZARA_UTILITY_SAFE if (static_cast(elementPos) >= m_sharedImpl->elements.size()) + { + NazaraError("Element not found"); return nullptr; + } + #endif NzVertexElement& element = m_sharedImpl->elements[elementPos]; + + #if NAZARA_UTILITY_SAFE if (element.stream != stream || element.usage != usage || element.usageIndex != usageIndex) + { + NazaraError("Element not found"); return nullptr; + } + #endif return &element; } @@ -318,6 +336,33 @@ unsigned int NzVertexDeclaration::GetStride(nzElementStream stream) const return m_sharedImpl->stride[stream]; } +bool NzVertexDeclaration::HasElement(unsigned int i) const +{ + return i < m_sharedImpl->elements.size(); +} + +bool NzVertexDeclaration::HasElement(nzElementStream stream, unsigned int i) const +{ + return i < GetElementCount(stream); +} + +bool NzVertexDeclaration::HasElement(nzElementStream stream, nzElementUsage usage, unsigned int usageIndex) const +{ + int elementPos = m_sharedImpl->elementPos[stream][usage]; + if (elementPos == -1) + return false; + + elementPos += usageIndex; + if (static_cast(elementPos) >= m_sharedImpl->elements.size()) + return false; + + NzVertexElement& element = m_sharedImpl->elements[elementPos]; + if (element.stream != stream || element.usage != usage || element.usageIndex != usageIndex) + return false; + + return true; +} + bool NzVertexDeclaration::HasStream(nzElementStream stream) const { #if NAZARA_UTILITY_SAFE