/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "Drawable.hpp" #include "Scene.hpp" #include "Base/Utils.hpp" namespace openVulkanoCpp::Scene { void Drawable::SetScene(Scene* scene) { if (m_scene == scene) return; if (scene && m_scene) throw std::runtime_error("Drawable has been associated with a scene already!"); const auto oldScene = m_scene; m_scene = scene; if(scene) scene->RegisterDrawable(this); else if (oldScene) oldScene->RemoveDrawable(this); } void Drawable::AddNode(Node* node) { if (!m_mesh) throw std::runtime_error("Drawable is not initialized."); if (Utils::Contains(m_nodes, node)) throw std::runtime_error("A drawable must not use the same node more than once."); m_nodes.push_back(node); } void Drawable::RemoveNode(Node* node) { Utils::Remove(m_nodes, node); if (m_nodes.empty()) { m_scene->RemoveDrawable(this); m_scene = nullptr; } } void Drawable::Init(Geometry* mesh, Material* material) { if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized."); m_mesh = mesh; m_material = material; } void Drawable::Init(Drawable* drawable) { if (m_mesh || m_material) throw std::runtime_error("Drawable is already initialized."); m_mesh = drawable->m_mesh; m_material = drawable->m_material; } void Drawable::Close() { if (!m_nodes.empty()) throw std::runtime_error("Drawable is still being used!!!"); m_mesh = nullptr; m_material = nullptr; } }