Graphics/LightComponent: Replace AttachLight with AddLight

This commit is contained in:
SirLynix
2022-11-26 18:32:47 +01:00
committed by Jérôme Leclercq
parent d7eab778fb
commit ec3bc45544
6 changed files with 58 additions and 49 deletions

View File

@@ -28,12 +28,10 @@ namespace Nz
LightComponent(LightComponent&&) = default;
~LightComponent() = default;
inline void AttachLight(std::shared_ptr<Light> renderable, UInt32 renderMask);
template<typename T, typename... Args> T& AddLight(UInt32 renderMask, Args&&... args);
inline void Clear();
inline void DetachLight(const std::shared_ptr<Light>& renderable);
inline const LightEntry& GetLightEntry(std::size_t lightIndex) const;
inline const std::array<LightEntry, MaxLightCount>& GetLights() const;
@@ -41,6 +39,9 @@ namespace Nz
inline bool IsVisible() const;
inline void RemoveLight(std::size_t lightIndex);
inline void RemoveLight(const Light& renderable);
inline void Show(bool show = true);
LightComponent& operator=(const LightComponent&) = default;
@@ -52,7 +53,7 @@ namespace Nz
struct LightEntry
{
std::shared_ptr<Light> light;
std::unique_ptr<Light> light;
UInt32 renderMask;
};

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/Components/LightComponent.hpp>
#include <stdexcept>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
@@ -12,20 +13,25 @@ namespace Nz
{
}
inline void LightComponent::AttachLight(std::shared_ptr<Light> light, UInt32 renderMask)
template<typename T, typename... Args>
T& LightComponent::AddLight(UInt32 renderMask, Args&&... args)
{
static_assert(std::is_base_of_v<Light, T>, "Type must inherit Light");
for (std::size_t i = 0; i < m_lightEntries.size(); ++i)
{
auto& entry = m_lightEntries[i];
if (entry.light)
continue;
entry.light = std::move(light);
entry.light = std::make_unique<T>(std::forward<Args>(args)...);
entry.renderMask = renderMask;
OnLightAttached(this, i);
break;
return static_cast<T&>(*entry.light);
}
throw std::runtime_error("too many lights attached to the same entity");
}
inline void LightComponent::Clear()
@@ -33,7 +39,7 @@ namespace Nz
for (std::size_t i = 0; i < m_lightEntries.size(); ++i)
{
auto& entry = m_lightEntries[i];
if (entry.light)
if (!entry.light)
continue;
OnLightDetach(this, i);
@@ -41,17 +47,6 @@ namespace Nz
}
}
inline void LightComponent::DetachLight(const std::shared_ptr<Light>& light)
{
auto it = std::find_if(m_lightEntries.begin(), m_lightEntries.end(), [&](const auto& lightEntry) { return lightEntry.light == light; });
if (it != m_lightEntries.end())
{
OnLightDetach(this, std::distance(m_lightEntries.begin(), it));
it->light.reset();
}
}
inline auto LightComponent::GetLightEntry(std::size_t lightIndex) const -> const LightEntry&
{
assert(lightIndex < m_lightEntries.size());
@@ -73,6 +68,24 @@ namespace Nz
return m_isVisible;
}
inline void LightComponent::RemoveLight(std::size_t lightIndex)
{
assert(lightIndex < m_lightEntries.size());
auto& entry = m_lightEntries[lightIndex];
if (!entry.light)
return;
OnLightDetach(this, lightIndex);
entry.light.reset();
}
inline void LightComponent::RemoveLight(const Light& light)
{
auto it = std::find_if(m_lightEntries.begin(), m_lightEntries.end(), [&](const auto& lightEntry) { return lightEntry.light.get() == &light; });
if (it != m_lightEntries.end())
RemoveLight(std::distance(m_lightEntries.begin(), it));
}
inline void LightComponent::Show(bool show)
{
if (m_isVisible != show)