Graphics: Add SlicedSprite class

This commit is contained in:
Jérôme Leclercq
2021-11-28 20:19:59 +01:00
parent 01cd4986cc
commit db88f0ca0d
4 changed files with 326 additions and 0 deletions

View File

@@ -60,6 +60,7 @@
#include <Nazara/Graphics/RenderQueueRegistry.hpp>
#include <Nazara/Graphics/RenderSpriteChain.hpp>
#include <Nazara/Graphics/RenderSubmesh.hpp>
#include <Nazara/Graphics/SlicedSprite.hpp>
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Graphics/SpriteChainRenderer.hpp>
#include <Nazara/Graphics/SubmeshRenderer.hpp>

View File

@@ -0,0 +1,73 @@
// Copyright (C) 2021 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_SLICEDSPRITE_HPP
#define NAZARA_GRAPHICS_SLICEDSPRITE_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 SlicedSprite : public InstancedRenderable
{
public:
struct Corner;
SlicedSprite(std::shared_ptr<Material> material);
SlicedSprite(const SlicedSprite&) = delete;
SlicedSprite(SlicedSprite&&) noexcept = default;
~SlicedSprite() = default;
void BuildElement(std::size_t passIndex, const WorldInstance& worldInstance, std::vector<std::unique_ptr<RenderElement>>& elements) const override;
inline const Color& GetColor() const;
inline const Corner& GetBottomRightCorner() const;
const std::shared_ptr<Material>& GetMaterial(std::size_t i = 0) const override;
std::size_t GetMaterialCount() const override;
inline const Corner& GetTopLeftCorner() const;
inline const Rectf& GetTextureCoords() const;
Vector3ui GetTextureSize() const;
inline void SetColor(const Color& color);
inline void SetCorners(const Corner& topLeftCorner, const Corner& bottomRightCorner);
inline void SetCornersSize(const Vector2f& topLeftSize, const Vector2f& bottomRightSize);
inline void SetCornersTextureCoords(const Vector2f& topLeftTextureCoords, const Vector2f& bottomRightTextureCoords);
inline void SetMaterial(std::shared_ptr<Material> material);
inline void SetSize(const Vector2f& size);
inline void SetTextureCoords(const Rectf& textureCoords);
inline void SetTextureRect(const Rectf& textureRect);
SlicedSprite& operator=(const SlicedSprite&) = delete;
SlicedSprite& operator=(SlicedSprite&&) noexcept = default;
struct Corner
{
Vector2f textureCoords = Vector2f(0.125f, 0.125f);
Vector2f size = Vector2f(16.f, 16.f);
};
private:
void UpdateVertices();
std::array<VertexStruct_XYZ_Color_UV, 4 * 9> m_vertices;
std::shared_ptr<Material> m_material;
Color m_color;
Corner m_topLeftCorner;
Corner m_bottomRightCorner;
Rectf m_textureCoords;
Vector2f m_size;
};
}
#include <Nazara/Graphics/SlicedSprite.inl>
#endif // NAZARA_GRAPHICS_SLICEDSPRITE_HPP

View File

@@ -0,0 +1,91 @@
// Copyright (C) 2021 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/SlicedSprite.hpp>
#include <cassert>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline const Color& SlicedSprite::GetColor() const
{
return m_color;
}
inline auto SlicedSprite::GetBottomRightCorner() const -> const Corner&
{
return m_bottomRightCorner;
}
inline const Rectf& SlicedSprite::GetTextureCoords() const
{
return m_textureCoords;
}
inline void SlicedSprite::SetColor(const Color& color)
{
m_color = color;
UpdateVertices();
}
inline void SlicedSprite::SetCorners(const Corner& topLeftCorner, const Corner& bottomRightCorner)
{
m_topLeftCorner = topLeftCorner;
m_bottomRightCorner = bottomRightCorner;
UpdateVertices();
}
inline void SlicedSprite::SetCornersSize(const Vector2f& topLeftSize, const Vector2f& bottomRightSize)
{
m_topLeftCorner.size = topLeftSize;
m_bottomRightCorner.size = bottomRightSize;
UpdateVertices();
}
inline void SlicedSprite::SetCornersTextureCoords(const Vector2f& topLeftTextureCoords, const Vector2f& bottomRightTextureCoords)
{
m_topLeftCorner.textureCoords = topLeftTextureCoords;
m_bottomRightCorner.textureCoords = bottomRightTextureCoords;
UpdateVertices();
}
inline void SlicedSprite::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 SlicedSprite::SetSize(const Vector2f& size)
{
m_size = size;
UpdateVertices();
}
inline void SlicedSprite::SetTextureCoords(const Rectf& textureCoords)
{
m_textureCoords = textureCoords;
UpdateVertices();
}
inline void SlicedSprite::SetTextureRect(const Rectf& textureRect)
{
Vector2ui textureSize(GetTextureSize());
return SetTextureCoords(textureRect / Vector2f(textureSize));
}
}
#include <Nazara/Graphics/DebugOff.hpp>