diff --git a/include/Nazara/Graphics/Material.hpp b/include/Nazara/Graphics/Material.hpp index 27a1f7137..4bc433c14 100644 --- a/include/Nazara/Graphics/Material.hpp +++ b/include/Nazara/Graphics/Material.hpp @@ -22,6 +22,8 @@ namespace Nz inline void AddPass(std::size_t passIndex, std::shared_ptr pass); inline void AddPass(std::string passName, std::shared_ptr pass); + inline const std::shared_ptr& FindPass(const std::string& passName) const; + inline const std::shared_ptr& GetPass(std::size_t passIndex) const; inline bool HasPass(std::size_t passIndex) const; diff --git a/include/Nazara/Graphics/Material.inl b/include/Nazara/Graphics/Material.inl index ebec3f1c9..9b9569dfd 100644 --- a/include/Nazara/Graphics/Material.inl +++ b/include/Nazara/Graphics/Material.inl @@ -21,6 +21,12 @@ namespace Nz return AddPass(registry.GetPassIndex(passName), std::move(pass)); } + inline const std::shared_ptr& Material::FindPass(const std::string& passName) const + { + auto& registry = Graphics::Instance()->GetMaterialPassRegistry(); + return GetPass(registry.GetPassIndex(passName)); + } + inline const std::shared_ptr& Material::GetPass(std::size_t passIndex) const { if (passIndex >= m_passes.size()) diff --git a/include/Nazara/Graphics/Sprite.hpp b/include/Nazara/Graphics/Sprite.hpp index 3491b8f7a..6de65e408 100644 --- a/include/Nazara/Graphics/Sprite.hpp +++ b/include/Nazara/Graphics/Sprite.hpp @@ -29,13 +29,17 @@ namespace Nz inline const Color& GetColor() const; inline const Color& GetCornerColor(RectCorner corner) const; - const std::shared_ptr& GetMaterial(std::size_t i) const; + const std::shared_ptr& GetMaterial(std::size_t i = 0) const; std::size_t GetMaterialCount() const; + inline const Rectf& GetTextureCoords() const; + Vector3ui GetTextureSize() const; inline void SetColor(const Color& color); inline void SetCornerColor(RectCorner corner, const Color& color); inline void SetMaterial(std::shared_ptr material); inline void SetSize(const Vector2f& size); + inline void SetTextureCoords(const Rectf& textureCoords); + inline void SetTextureRect(const Rectf& textureRect); Sprite& operator=(const Sprite&) = delete; Sprite& operator=(Sprite&&) noexcept = default; diff --git a/include/Nazara/Graphics/Sprite.inl b/include/Nazara/Graphics/Sprite.inl index 4300067d3..bc19deeea 100644 --- a/include/Nazara/Graphics/Sprite.inl +++ b/include/Nazara/Graphics/Sprite.inl @@ -8,6 +8,21 @@ namespace Nz { + inline const Color& Sprite::GetColor() const + { + return m_color; + } + + inline const Color& Sprite::GetCornerColor(RectCorner corner) const + { + return m_cornerColor[UnderlyingCast(corner)]; + } + + inline const Rectf& Sprite::GetTextureCoords() const + { + return m_textureCoords; + } + inline void Sprite::SetColor(const Color& color) { m_color = color; @@ -24,6 +39,8 @@ namespace Nz inline void Sprite::SetMaterial(std::shared_ptr material) { + assert(material); + m_material = std::move(material); } @@ -34,6 +51,19 @@ namespace Nz UpdateVertices(); } + inline void Sprite::SetTextureCoords(const Rectf& textureCoords) + { + m_textureCoords = textureCoords; + + UpdateVertices(); + } + + inline void Sprite::SetTextureRect(const Rectf& textureRect) + { + Vector2ui textureSize(GetTextureSize()); + return SetTextureCoords(textureRect / Vector2f(textureSize)); + } + inline void Sprite::UpdateVertices() { VertexStruct_XYZ_Color_UV* vertices = m_vertices.data(); diff --git a/src/Nazara/Graphics/Sprite.cpp b/src/Nazara/Graphics/Sprite.cpp index 627e92c21..49ab71e9d 100644 --- a/src/Nazara/Graphics/Sprite.cpp +++ b/src/Nazara/Graphics/Sprite.cpp @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include @@ -44,16 +45,6 @@ namespace Nz elements.emplace_back(std::make_unique(0, materialPass, renderPipeline, worldInstance, vertexDeclaration, whiteTexture, 1, m_vertices.data())); } - inline const Color& Sprite::GetColor() const - { - return m_color; - } - - inline const Color& Sprite::GetCornerColor(RectCorner corner) const - { - return m_cornerColor[UnderlyingCast(corner)]; - } - const std::shared_ptr& Sprite::GetMaterial(std::size_t i) const { assert(i == 0); @@ -66,4 +57,24 @@ namespace Nz { return 1; } + + Vector3ui Sprite::GetTextureSize() const + { + assert(m_material); + + //TODO: Cache index in registry? + if (const auto& material = m_material->FindPass("ForwardPass")) + { + BasicMaterial mat(*material); + if (mat.HasDiffuseMap()) + { + // Material should always have textures but we're better safe than sorry + if (const auto& texture = mat.GetDiffuseMap()) + return mat.GetDiffuseMap()->GetSize(); + } + } + + // Couldn't get material pass or texture + return Vector3ui::Zero(); + } }