Graphics: Add LinearSlicedSprite class
This commit is contained in:
91
include/Nazara/Graphics/LinearSlicedSprite.hpp
Normal file
91
include/Nazara/Graphics/LinearSlicedSprite.hpp
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_GRAPHICS_LINEARSLICEDSPRITE_HPP
|
||||
#define NAZARA_GRAPHICS_LINEARSLICEDSPRITE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||
#include <Nazara/Renderer/RenderPipeline.hpp>
|
||||
#include <Nazara/Utility/VertexDeclaration.hpp>
|
||||
#include <Nazara/Utility/VertexStruct.hpp>
|
||||
#include <array>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_GRAPHICS_API LinearSlicedSprite : public InstancedRenderable
|
||||
{
|
||||
public:
|
||||
enum class Orientation;
|
||||
struct Section;
|
||||
|
||||
LinearSlicedSprite(std::shared_ptr<Material> material, Orientation orientation);
|
||||
LinearSlicedSprite(const LinearSlicedSprite&) = delete;
|
||||
LinearSlicedSprite(LinearSlicedSprite&&) noexcept = default;
|
||||
~LinearSlicedSprite() = default;
|
||||
|
||||
inline void AddSection(float size, float textureCoord);
|
||||
|
||||
void BuildElement(std::size_t passIndex, const WorldInstance& worldInstance, std::vector<std::unique_ptr<RenderElement>>& elements, const Recti& scissorBox) const override;
|
||||
|
||||
inline void Clear();
|
||||
|
||||
inline const Color& GetColor() const;
|
||||
const std::shared_ptr<Material>& GetMaterial(std::size_t i = 0) const override;
|
||||
std::size_t GetMaterialCount() const override;
|
||||
inline Orientation GetOrientation() const;
|
||||
inline const Section& GetSection(std::size_t sectionIndex) const;
|
||||
std::size_t GetSectionCount() const;
|
||||
inline const Rectf& GetTextureCoords() const;
|
||||
Vector3ui GetTextureSize() const;
|
||||
|
||||
inline void RemoveSection(std::size_t sectionIndex);
|
||||
|
||||
inline void SetColor(const Color& color);
|
||||
inline void SetMaterial(std::shared_ptr<Material> material);
|
||||
inline void SetSection(std::size_t sectionIndex, float size, float textureCoord);
|
||||
inline void SetSectionSize(std::size_t sectionIndex, float size);
|
||||
inline void SetSectionTextureCoord(std::size_t sectionIndex, float textureCoord);
|
||||
inline void SetSize(const Vector2f& size);
|
||||
inline void SetTextureCoords(const Rectf& textureCoords);
|
||||
inline void SetTextureRect(const Rectf& textureRect);
|
||||
|
||||
LinearSlicedSprite& operator=(const LinearSlicedSprite&) = delete;
|
||||
LinearSlicedSprite& operator=(LinearSlicedSprite&&) noexcept = default;
|
||||
|
||||
enum class Orientation
|
||||
{
|
||||
Horizontal,
|
||||
Vertical
|
||||
};
|
||||
|
||||
struct Section
|
||||
{
|
||||
float size;
|
||||
float textureCoord;
|
||||
};
|
||||
|
||||
static constexpr std::size_t MaxSection = 5;
|
||||
|
||||
private:
|
||||
void UpdateVertices();
|
||||
|
||||
std::array<Section, MaxSection> m_sections;
|
||||
std::array<VertexStruct_XYZ_Color_UV, 4 * MaxSection> m_vertices;
|
||||
std::shared_ptr<Material> m_material;
|
||||
std::size_t m_sectionCount;
|
||||
std::size_t m_spriteCount;
|
||||
Color m_color;
|
||||
Orientation m_orientation;
|
||||
Rectf m_textureCoords;
|
||||
Vector2f m_size;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Graphics/LinearSlicedSprite.inl>
|
||||
|
||||
#endif // NAZARA_GRAPHICS_LINEARSLICEDSPRITE_HPP
|
||||
142
include/Nazara/Graphics/LinearSlicedSprite.inl
Normal file
142
include/Nazara/Graphics/LinearSlicedSprite.inl
Normal file
@@ -0,0 +1,142 @@
|
||||
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/LinearSlicedSprite.hpp>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline void LinearSlicedSprite::AddSection(float size, float textureCoord)
|
||||
{
|
||||
NazaraAssert(m_sectionCount < m_sections.size(), "too many sections");
|
||||
|
||||
auto& section = m_sections[m_sectionCount++];
|
||||
section.size = size;
|
||||
section.textureCoord = textureCoord;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::Clear()
|
||||
{
|
||||
m_sectionCount = 0;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline const Color& LinearSlicedSprite::GetColor() const
|
||||
{
|
||||
return m_color;
|
||||
}
|
||||
|
||||
inline auto LinearSlicedSprite::GetOrientation() const -> Orientation
|
||||
{
|
||||
return m_orientation;
|
||||
}
|
||||
|
||||
inline auto LinearSlicedSprite::GetSection(std::size_t sectionIndex) const -> const Section&
|
||||
{
|
||||
NazaraAssert(sectionIndex < m_sectionCount, "out of range section");
|
||||
return m_sections[sectionIndex];
|
||||
}
|
||||
|
||||
inline std::size_t LinearSlicedSprite::GetSectionCount() const
|
||||
{
|
||||
return m_sectionCount;
|
||||
}
|
||||
|
||||
inline const Rectf& LinearSlicedSprite::GetTextureCoords() const
|
||||
{
|
||||
return m_textureCoords;
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetColor(const Color& color)
|
||||
{
|
||||
m_color = color;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::RemoveSection(std::size_t sectionIndex)
|
||||
{
|
||||
NazaraAssert(sectionIndex < m_sectionCount, "out of range section");
|
||||
if (m_sectionCount >= sectionIndex + 1)
|
||||
std::move(m_sections.begin() + sectionIndex + 1, m_sections.begin() + m_sectionCount, m_sections.begin() + sectionIndex);
|
||||
|
||||
m_sectionCount--;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetMaterial(std::shared_ptr<Material> material)
|
||||
{
|
||||
assert(material);
|
||||
|
||||
if (m_material != material)
|
||||
{
|
||||
OnMaterialInvalidated(this, 0, material);
|
||||
m_material = std::move(material);
|
||||
|
||||
OnElementInvalidated(this);
|
||||
}
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetSection(std::size_t sectionIndex, float size, float textureCoord)
|
||||
{
|
||||
NazaraAssert(sectionIndex < m_sectionCount, "out of range section");
|
||||
|
||||
auto& section = m_sections[sectionIndex];
|
||||
section.size = size;
|
||||
section.textureCoord = textureCoord;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetSectionSize(std::size_t sectionIndex, float size)
|
||||
{
|
||||
NazaraAssert(sectionIndex < m_sectionCount, "out of range section");
|
||||
|
||||
auto& section = m_sections[sectionIndex];
|
||||
section.size = size;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetSectionTextureCoord(std::size_t sectionIndex, float textureCoord)
|
||||
{
|
||||
NazaraAssert(sectionIndex < m_sectionCount, "out of range section");
|
||||
|
||||
auto& section = m_sections[sectionIndex];
|
||||
section.textureCoord = textureCoord;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetSize(const Vector2f& size)
|
||||
{
|
||||
NazaraAssert(size.x >= 0.f, "width must be positive");
|
||||
NazaraAssert(size.y >= 0.f, "height must be positive");
|
||||
|
||||
m_size = size;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetTextureCoords(const Rectf& textureCoords)
|
||||
{
|
||||
m_textureCoords = textureCoords;
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
inline void LinearSlicedSprite::SetTextureRect(const Rectf& textureRect)
|
||||
{
|
||||
Vector2ui textureSize(GetTextureSize());
|
||||
return SetTextureCoords(textureRect / Vector2f(textureSize));
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Graphics/DebugOff.hpp>
|
||||
Reference in New Issue
Block a user