Brenta Engine 1.2
Loading...
Searching...
No Matches
scene.cpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#include <brenta/renderer/renderer.hpp>
7#include <brenta/scene.hpp>
8#include <brenta/ecs/ecs.hpp>
9#include <brenta/node.hpp>
10#include <brenta/renderer/skybox.hpp>
11
12using namespace brenta;
13
14Scene::Scene(Camera&& camera)
15{
16 this->active_camera = tenno::make_shared<Camera>(tenno::move(camera));
17 this->root = tenno::make_shared<Node>();
18}
19
20Scene::Scene(Camera::Builder& camera)
21{
22 this->active_camera = tenno::make_shared<Camera>(camera.build());
23 this->root = tenno::make_shared<Node>();
24}
25
26Scene::Scene(tenno::shared_ptr<Camera> camera)
27{
28 this->active_camera = camera;
29 this->root = tenno::make_shared<Node>();
30}
31
32void Scene::set_skybox(tenno::shared_ptr<Skybox> skybox)
33{
34 this->skybox = skybox;
35}
36
37void Scene::set_skybox(const tenno::vector<std::filesystem::path>& faces)
38{
39 this->skybox = tenno::make_shared<Skybox>(faces);
40}
41
42tenno::shared_ptr<Node> Scene::get_root() const
43{
44 return this->root;
45}
46
47tenno::shared_ptr<Camera> Scene::get_camera() const
48{
49 return this->active_camera;
50}
51
52tenno::shared_ptr<Node> Scene::create_child(tenno::shared_ptr<Node> parent)
53{
54 auto child = tenno::make_shared<Node>();
55 if (parent)
56 {
57 parent->children.push_back(child);
58 child->parent = parent;
59 }
60 return child;
61}
62
63void Scene::add_component(tenno::shared_ptr<Node> owner,
64 tenno::shared_ptr<NodeComponent> component)
65{
66 component->owner = owner;
67 owner->components.push_back(component);
68}
69
70void Scene::set_script(tenno::weak_ptr<Node> node, const std::filesystem::path &path)
71{
72 if (auto node_ptr = node.lock())
73 node_ptr->script = Script(node, path);
74}
75
76void Scene::set_script(tenno::weak_ptr<Node> node, const std::string &source)
77{
78 if (auto node_ptr = node.lock())
79 node_ptr->script = Script(node, source);
80}
81
82void Scene::update(float delta_time)
83{
84 #ifndef BRENTA_NO_ECS
85 viotecs::World::tick();
86 #endif
87 this->root->update(delta_time);
88}
89
90void Scene::draw(tenno::shared_ptr<RenderPipeline> pipeline,
91 int width, int height)
92{
93 this->root->update_world_matrix();
94
95 Renderer::begin_frame(*this->active_camera, width, height);
96
97 if (this->skybox)
98 Renderer::submit_skybox(this->skybox.value());
99
100 this->root->draw();
101
102 Renderer::end_frame(pipeline);
103}
104
105Scene::Builder &Scene::Builder::camera(tenno::shared_ptr<Camera> camera)
106{
107 this->_camera = camera;
108 return *this;
109}
110
111Scene::Builder &Scene::Builder::camera(Camera&& camera)
112{
113 this->_camera = tenno::make_shared<Camera>(tenno::move(camera));
114 return *this;
115}
116
117Scene::Builder &Scene::Builder::camera(Camera::Builder& camera)
118{
119 this->_camera = tenno::make_shared<Camera>(camera.build());
120 return *this;
121}
122
123Scene Scene::Builder::build()
124{
125 return Scene(this->_camera);
126}