Merge branch 'NDK' of https://github.com/DigitalPulseSoftware/NazaraEngine into NDK
Former-commit-id: d6b7e05adf8586c40b79de8dc0880ae1a501403c
This commit is contained in:
@@ -8,56 +8,60 @@
|
||||
#define NAZARA_SPRITE_HPP
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/SceneNode.hpp>
|
||||
#include <Nazara/Utility/VertexStruct.hpp>
|
||||
#include <array>
|
||||
|
||||
class NAZARA_GRAPHICS_API NzSprite : public NzSceneNode
|
||||
class NzSprite;
|
||||
|
||||
using NzSpriteConstRef = NzObjectRef<const NzSprite>;
|
||||
using NzSpriteLibrary = NzObjectLibrary<NzSprite>;
|
||||
using NzSpriteRef = NzObjectRef<NzSprite>;
|
||||
|
||||
class NAZARA_GRAPHICS_API NzSprite : public NzInstancedRenderable
|
||||
{
|
||||
public:
|
||||
NzSprite();
|
||||
NzSprite(NzTexture* texture);
|
||||
NzSprite(const NzSprite& sprite);
|
||||
inline NzSprite();
|
||||
inline NzSprite(NzMaterialRef material);
|
||||
inline NzSprite(NzTexture* texture);
|
||||
inline NzSprite(const NzSprite& sprite);
|
||||
~NzSprite() = default;
|
||||
|
||||
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue) const override;
|
||||
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const override;
|
||||
|
||||
NzSprite* Clone() const;
|
||||
NzSprite* Create() const;
|
||||
inline const NzColor& GetColor() const;
|
||||
inline const NzMaterialRef& GetMaterial() const;
|
||||
inline const NzVector2f& GetSize() const;
|
||||
inline const NzRectf& GetTextureCoords() const;
|
||||
|
||||
const NzColor& GetColor() const;
|
||||
NzMaterial* GetMaterial() const;
|
||||
nzSceneNodeType GetSceneNodeType() const override;
|
||||
const NzVector2f& GetSize() const;
|
||||
const NzRectf& GetTextureCoords() const;
|
||||
inline void SetColor(const NzColor& color);
|
||||
inline void SetDefaultMaterial();
|
||||
inline void SetMaterial(NzMaterialRef material, bool resizeSprite = true);
|
||||
inline void SetSize(const NzVector2f& size);
|
||||
inline void SetSize(float sizeX, float sizeY);
|
||||
inline void SetTexture(NzTextureRef texture, bool resizeSprite = true);
|
||||
inline void SetTextureCoords(const NzRectf& coords);
|
||||
inline void SetTextureRect(const NzRectui& rect);
|
||||
|
||||
bool IsDrawable() const;
|
||||
inline NzSprite& operator=(const NzSprite& sprite);
|
||||
|
||||
void SetColor(const NzColor& color);
|
||||
void SetDefaultMaterial();
|
||||
void SetMaterial(NzMaterial* material, bool resizeSprite = true);
|
||||
void SetSize(const NzVector2f& size);
|
||||
void SetSize(float sizeX, float sizeY);
|
||||
void SetTexture(NzTexture* texture, bool resizeSprite = true);
|
||||
void SetTextureCoords(const NzRectf& coords);
|
||||
void SetTextureRect(const NzRectui& rect);
|
||||
|
||||
NzSprite& operator=(const NzSprite& sprite);
|
||||
template<typename... Args> static NzSpriteRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
void InvalidateNode() override;
|
||||
inline void InvalidateVertices();
|
||||
void MakeBoundingVolume() const override;
|
||||
void Register() override;
|
||||
void Unregister() override;
|
||||
void UpdateVertices() const;
|
||||
void UpdateData(InstanceData* instanceData) const override;
|
||||
|
||||
NzColor m_color;
|
||||
NzMaterialRef m_material;
|
||||
NzRectf m_textureCoords;
|
||||
NzVector2f m_size;
|
||||
mutable std::array<NzVertexStruct_XYZ_Color_UV, 4> m_vertices;
|
||||
mutable bool m_verticesUpdated;
|
||||
|
||||
static NzSpriteLibrary::LibraryMap s_library;
|
||||
};
|
||||
|
||||
#include <Nazara/Graphics/Sprite.inl>
|
||||
|
||||
#endif // NAZARA_SPRITE_HPP
|
||||
|
||||
165
include/Nazara/Graphics/Sprite.inl
Normal file
165
include/Nazara/Graphics/Sprite.inl
Normal file
@@ -0,0 +1,165 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <memory>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
inline NzSprite::NzSprite() :
|
||||
m_color(NzColor::White),
|
||||
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
||||
m_size(64.f, 64.f)
|
||||
{
|
||||
SetDefaultMaterial();
|
||||
}
|
||||
|
||||
inline NzSprite::NzSprite(NzMaterialRef material) :
|
||||
m_color(NzColor::White),
|
||||
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
||||
m_size(64.f, 64.f)
|
||||
{
|
||||
SetMaterial(std::move(material), true);
|
||||
}
|
||||
|
||||
inline NzSprite::NzSprite(NzTexture* texture) :
|
||||
m_color(NzColor::White),
|
||||
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
||||
m_size(64.f, 64.f)
|
||||
{
|
||||
SetTexture(texture, true);
|
||||
}
|
||||
|
||||
inline NzSprite::NzSprite(const NzSprite& sprite) :
|
||||
NzInstancedRenderable(sprite),
|
||||
m_color(sprite.m_color),
|
||||
m_material(sprite.m_material),
|
||||
m_textureCoords(sprite.m_textureCoords),
|
||||
m_size(sprite.m_size)
|
||||
{
|
||||
}
|
||||
|
||||
inline const NzColor& NzSprite::GetColor() const
|
||||
{
|
||||
return m_color;
|
||||
}
|
||||
|
||||
inline const NzMaterialRef& NzSprite::GetMaterial() const
|
||||
{
|
||||
return m_material;
|
||||
}
|
||||
|
||||
inline const NzVector2f& NzSprite::GetSize() const
|
||||
{
|
||||
return m_size;
|
||||
}
|
||||
|
||||
inline const NzRectf& NzSprite::GetTextureCoords() const
|
||||
{
|
||||
return m_textureCoords;
|
||||
}
|
||||
|
||||
inline void NzSprite::SetColor(const NzColor& color)
|
||||
{
|
||||
m_color = color;
|
||||
|
||||
InvalidateVertices();
|
||||
}
|
||||
|
||||
inline void NzSprite::SetDefaultMaterial()
|
||||
{
|
||||
NzMaterialRef material = NzMaterial::New();
|
||||
material->Enable(nzRendererParameter_FaceCulling, false);
|
||||
material->EnableLighting(false);
|
||||
|
||||
SetMaterial(material);
|
||||
}
|
||||
|
||||
inline void NzSprite::SetMaterial(NzMaterialRef material, bool resizeSprite)
|
||||
{
|
||||
m_material = std::move(material);
|
||||
if (m_material && resizeSprite)
|
||||
{
|
||||
NzTexture* diffuseMap = m_material->GetDiffuseMap();
|
||||
if (diffuseMap && diffuseMap->IsValid())
|
||||
SetSize(NzVector2f(NzVector2ui(diffuseMap->GetSize())));
|
||||
}
|
||||
}
|
||||
|
||||
inline void NzSprite::SetSize(const NzVector2f& size)
|
||||
{
|
||||
m_size = size;
|
||||
|
||||
// On invalide la bounding box
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateVertices();
|
||||
}
|
||||
|
||||
inline void NzSprite::SetSize(float sizeX, float sizeY)
|
||||
{
|
||||
SetSize(NzVector2f(sizeX, sizeY));
|
||||
}
|
||||
|
||||
inline void NzSprite::SetTexture(NzTextureRef texture, bool resizeSprite)
|
||||
{
|
||||
if (!m_material)
|
||||
SetDefaultMaterial();
|
||||
else if (m_material->GetReferenceCount() > 1)
|
||||
m_material = NzMaterial::New(*m_material); // Copie
|
||||
|
||||
if (resizeSprite && texture && texture->IsValid())
|
||||
SetSize(NzVector2f(NzVector2ui(texture->GetSize())));
|
||||
|
||||
m_material->SetDiffuseMap(std::move(texture));
|
||||
}
|
||||
|
||||
inline void NzSprite::SetTextureCoords(const NzRectf& coords)
|
||||
{
|
||||
m_textureCoords = coords;
|
||||
InvalidateVertices();
|
||||
}
|
||||
|
||||
inline void NzSprite::SetTextureRect(const NzRectui& rect)
|
||||
{
|
||||
NazaraAssert(m_material, "Sprite has no material");
|
||||
NazaraAssert(m_material->HasDiffuseMap(), "Sprite material has no diffuse map");
|
||||
|
||||
NzTexture* diffuseMap = m_material->GetDiffuseMap();
|
||||
|
||||
float invWidth = 1.f/diffuseMap->GetWidth();
|
||||
float invHeight = 1.f/diffuseMap->GetHeight();
|
||||
|
||||
SetTextureCoords(NzRectf(invWidth*rect.x, invHeight*rect.y, invWidth*rect.width, invHeight*rect.height));
|
||||
}
|
||||
|
||||
inline NzSprite& NzSprite::operator=(const NzSprite& sprite)
|
||||
{
|
||||
NzInstancedRenderable::operator=(sprite);
|
||||
|
||||
m_color = sprite.m_color;
|
||||
m_material = sprite.m_material;
|
||||
m_textureCoords = sprite.m_textureCoords;
|
||||
m_size = sprite.m_size;
|
||||
|
||||
// On ne copie pas les sommets finaux car il est très probable que nos paramètres soient modifiés et qu'ils doivent être régénérés de toute façon
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateVertices();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline void NzSprite::InvalidateVertices()
|
||||
{
|
||||
InvalidateInstanceData(0);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzSpriteRef NzSprite::New(Args&&... args)
|
||||
{
|
||||
std::unique_ptr<NzSprite> object(new NzSprite(std::forward<Args>(args)...));
|
||||
object->SetPersistent(false);
|
||||
|
||||
return object.release();
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/DebugOff.hpp>
|
||||
@@ -25,36 +25,31 @@ using NzTextSpriteRef = NzObjectRef<NzTextSprite>;
|
||||
class NAZARA_GRAPHICS_API NzTextSprite : public NzInstancedRenderable
|
||||
{
|
||||
public:
|
||||
NzTextSprite();
|
||||
NzTextSprite(const NzTextSprite& sprite);
|
||||
inline NzTextSprite();
|
||||
inline NzTextSprite(const NzTextSprite& sprite);
|
||||
~NzTextSprite() = default;
|
||||
|
||||
void AddToRenderQueue(NzAbstractRenderQueue* renderQueue, const InstanceData& instanceData) const override;
|
||||
|
||||
void Clear();
|
||||
inline void Clear();
|
||||
|
||||
NzTextSprite* Clone() const;
|
||||
NzTextSprite* Create() const;
|
||||
inline const NzColor& GetColor() const;
|
||||
inline const NzMaterialRef& GetMaterial() const;
|
||||
inline float GetScale() const;
|
||||
|
||||
const NzColor& GetColor() const;
|
||||
NzMaterial* GetMaterial() const;
|
||||
float GetScale() const;
|
||||
|
||||
void InvalidateVertices();
|
||||
bool IsDrawable() const;
|
||||
|
||||
void SetColor(const NzColor& color);
|
||||
void SetDefaultMaterial();
|
||||
void SetMaterial(NzMaterial* material);
|
||||
void SetScale(float scale);
|
||||
inline void SetColor(const NzColor& color);
|
||||
inline void SetDefaultMaterial();
|
||||
inline void SetMaterial(NzMaterialRef material);
|
||||
inline void SetScale(float scale);
|
||||
|
||||
void Update(const NzAbstractTextDrawer& drawer);
|
||||
|
||||
NzTextSprite& operator=(const NzTextSprite& text);
|
||||
inline NzTextSprite& operator=(const NzTextSprite& text);
|
||||
|
||||
template<typename... Args> static NzTextSpriteRef New(Args&&... args);
|
||||
|
||||
private:
|
||||
inline void InvalidateVertices();
|
||||
void MakeBoundingVolume() const override;
|
||||
void OnAtlasInvalidated(const NzAbstractAtlas* atlas);
|
||||
void OnAtlasLayerChange(const NzAbstractAtlas* atlas, NzAbstractImage* oldLayer, NzAbstractImage* newLayer);
|
||||
|
||||
@@ -5,6 +5,123 @@
|
||||
#include <memory>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
inline NzTextSprite::NzTextSprite() :
|
||||
m_color(NzColor::White),
|
||||
m_scale(1.f)
|
||||
{
|
||||
SetDefaultMaterial();
|
||||
}
|
||||
|
||||
inline NzTextSprite::NzTextSprite(const NzTextSprite& sprite) :
|
||||
NzInstancedRenderable(sprite),
|
||||
m_renderInfos(sprite.m_renderInfos),
|
||||
m_localVertices(sprite.m_localVertices),
|
||||
m_color(sprite.m_color),
|
||||
m_material(sprite.m_material),
|
||||
m_localBounds(sprite.m_localBounds),
|
||||
m_scale(sprite.m_scale)
|
||||
{
|
||||
for (auto it = sprite.m_atlases.begin(); it != sprite.m_atlases.end(); ++it)
|
||||
{
|
||||
const NzAbstractAtlas* atlas = it->first;
|
||||
AtlasSlots& slots = m_atlases[atlas];
|
||||
|
||||
slots.clearSlot.Connect(atlas->OnAtlasCleared, this, &NzTextSprite::OnAtlasInvalidated);
|
||||
slots.layerChangeSlot.Connect(atlas->OnAtlasLayerChange, this, &NzTextSprite::OnAtlasLayerChange);
|
||||
slots.releaseSlot.Connect(atlas->OnAtlasRelease, this, &NzTextSprite::OnAtlasInvalidated);
|
||||
}
|
||||
}
|
||||
|
||||
inline void NzTextSprite::Clear()
|
||||
{
|
||||
m_atlases.clear();
|
||||
m_boundingVolume.MakeNull();
|
||||
m_localVertices.clear();
|
||||
m_renderInfos.clear();
|
||||
}
|
||||
|
||||
inline const NzColor& NzTextSprite::GetColor() const
|
||||
{
|
||||
return m_color;
|
||||
}
|
||||
|
||||
inline const NzMaterialRef& NzTextSprite::GetMaterial() const
|
||||
{
|
||||
return m_material;
|
||||
}
|
||||
|
||||
inline float NzTextSprite::GetScale() const
|
||||
{
|
||||
return m_scale;
|
||||
}
|
||||
|
||||
inline void NzTextSprite::SetColor(const NzColor& color)
|
||||
{
|
||||
m_color = color;
|
||||
|
||||
InvalidateVertices();
|
||||
}
|
||||
|
||||
inline void NzTextSprite::SetDefaultMaterial()
|
||||
{
|
||||
NzMaterialRef material = NzMaterial::New();
|
||||
material->Enable(nzRendererParameter_Blend, true);
|
||||
material->Enable(nzRendererParameter_DepthWrite, false);
|
||||
material->Enable(nzRendererParameter_FaceCulling, false);
|
||||
material->EnableLighting(false);
|
||||
material->SetDstBlend(nzBlendFunc_InvSrcAlpha);
|
||||
material->SetSrcBlend(nzBlendFunc_SrcAlpha);
|
||||
|
||||
SetMaterial(material);
|
||||
}
|
||||
|
||||
inline void NzTextSprite::SetMaterial(NzMaterialRef material)
|
||||
{
|
||||
m_material = std::move(material);
|
||||
}
|
||||
|
||||
inline void NzTextSprite::SetScale(float scale)
|
||||
{
|
||||
m_scale = scale;
|
||||
|
||||
InvalidateVertices();
|
||||
}
|
||||
|
||||
inline void NzTextSprite::InvalidateVertices()
|
||||
{
|
||||
InvalidateInstanceData(0);
|
||||
}
|
||||
|
||||
inline NzTextSprite& NzTextSprite::operator=(const NzTextSprite& text)
|
||||
{
|
||||
NzInstancedRenderable::operator=(text);
|
||||
|
||||
m_atlases.clear();
|
||||
|
||||
m_color = text.m_color;
|
||||
m_material = text.m_material;
|
||||
m_renderInfos = text.m_renderInfos;
|
||||
m_localBounds = text.m_localBounds;
|
||||
m_localVertices = text.m_localVertices;
|
||||
m_scale = text.m_scale;
|
||||
|
||||
// Connect to the slots of the new atlases
|
||||
for (auto it = text.m_atlases.begin(); it != text.m_atlases.end(); ++it)
|
||||
{
|
||||
const NzAbstractAtlas* atlas = it->first;
|
||||
AtlasSlots& slots = m_atlases[atlas];
|
||||
|
||||
slots.clearSlot.Connect(atlas->OnAtlasCleared, this, &NzTextSprite::OnAtlasInvalidated);
|
||||
slots.layerChangeSlot.Connect(atlas->OnAtlasLayerChange, this, &NzTextSprite::OnAtlasLayerChange);
|
||||
slots.releaseSlot.Connect(atlas->OnAtlasRelease, this, &NzTextSprite::OnAtlasInvalidated);
|
||||
}
|
||||
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateVertices();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
NzTextSpriteRef NzTextSprite::New(Args&&... args)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user