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

78 lines
2.5 KiB
C++

#include "Components/CameraComponent.h"
#include <algorithm>
#include <sstream>
namespace XCEngine {
namespace Components {
void CameraComponent::SetFieldOfView(float value) {
m_fieldOfView = std::clamp(value, 1.0f, 179.0f);
}
void CameraComponent::SetOrthographicSize(float value) {
m_orthographicSize = std::max(0.001f, value);
}
void CameraComponent::SetNearClipPlane(float value) {
m_nearClipPlane = std::max(0.001f, value);
if (m_farClipPlane <= m_nearClipPlane) {
m_farClipPlane = m_nearClipPlane + 0.001f;
}
}
void CameraComponent::SetFarClipPlane(float value) {
m_farClipPlane = std::max(m_nearClipPlane + 0.001f, value);
}
void CameraComponent::Serialize(std::ostream& os) const {
os << "projection=" << static_cast<int>(m_projectionType) << ";";
os << "fov=" << m_fieldOfView << ";";
os << "orthoSize=" << m_orthographicSize << ";";
os << "near=" << m_nearClipPlane << ";";
os << "far=" << m_farClipPlane << ";";
os << "depth=" << m_depth << ";";
os << "primary=" << (m_primary ? 1 : 0) << ";";
os << "clearColor=" << m_clearColor.r << "," << m_clearColor.g << "," << m_clearColor.b << "," << m_clearColor.a << ";";
}
void CameraComponent::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 == "projection") {
m_projectionType = static_cast<CameraProjectionType>(std::stoi(value));
} else if (key == "fov") {
SetFieldOfView(std::stof(value));
} else if (key == "orthoSize") {
SetOrthographicSize(std::stof(value));
} else if (key == "near") {
SetNearClipPlane(std::stof(value));
} else if (key == "far") {
SetFarClipPlane(std::stof(value));
} else if (key == "depth") {
m_depth = std::stof(value);
} else if (key == "primary") {
m_primary = (std::stoi(value) != 0);
} else if (key == "clearColor") {
std::replace(value.begin(), value.end(), ',', ' ');
std::istringstream ss(value);
ss >> m_clearColor.r >> m_clearColor.g >> m_clearColor.b >> m_clearColor.a;
}
}
}
} // namespace Components
} // namespace XCEngine