Files
XCEngine/engine/src/Components/LightComponent.cpp

65 lines
1.9 KiB
C++

#include "Components/LightComponent.h"
#include <algorithm>
#include <sstream>
namespace XCEngine {
namespace Components {
void LightComponent::SetIntensity(float value) {
m_intensity = std::max(0.0f, value);
}
void LightComponent::SetRange(float value) {
m_range = std::max(0.001f, value);
}
void LightComponent::SetSpotAngle(float value) {
m_spotAngle = std::clamp(value, 1.0f, 179.0f);
}
void LightComponent::Serialize(std::ostream& os) const {
os << "type=" << static_cast<int>(m_lightType) << ";";
os << "color=" << m_color.r << "," << m_color.g << "," << m_color.b << "," << m_color.a << ";";
os << "intensity=" << m_intensity << ";";
os << "range=" << m_range << ";";
os << "spotAngle=" << m_spotAngle << ";";
os << "shadows=" << (m_castsShadows ? 1 : 0) << ";";
}
void LightComponent::Deserialize(std::istream& is) {
std::string token;
while (std::getline(is, token, ';')) {
if (token.empty()) {
continue;
}
const size_t eqPos = token.find('=');
if (eqPos == std::string::npos) {
continue;
}
const std::string key = token.substr(0, eqPos);
std::string value = token.substr(eqPos + 1);
if (key == "type") {
m_lightType = static_cast<LightType>(std::stoi(value));
} else if (key == "color") {
std::replace(value.begin(), value.end(), ',', ' ');
std::istringstream ss(value);
ss >> m_color.r >> m_color.g >> m_color.b >> m_color.a;
} else if (key == "intensity") {
SetIntensity(std::stof(value));
} else if (key == "range") {
SetRange(std::stof(value));
} else if (key == "spotAngle") {
SetSpotAngle(std::stof(value));
} else if (key == "shadows") {
m_castsShadows = (std::stoi(value) != 0);
}
}
}
} // namespace Components
} // namespace XCEngine