sandbox
Loading...
Searching...
No Matches
scenes_module.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_SCENES_SCENE_MODULE_HPP_
3#define LIBSBX_SCENES_SCENE_MODULE_HPP_
4
5#include <memory>
6#include <unordered_map>
7#include <utility>
8#include <filesystem>
9
10#include <libsbx/memory/tracking_allocator.hpp>
11
12#include <libsbx/math/vector4.hpp>
13#include <libsbx/math/color.hpp>
14
15#include <libsbx/utility/hashed_string.hpp>
16
17#include <libsbx/core/module.hpp>
18
19#include <libsbx/scenes/asset_registry.hpp>
20#include <libsbx/scenes/component_io.hpp>
21#include <libsbx/scenes/asset_io.hpp>
22#include <libsbx/scenes/scene.hpp>
23
24namespace sbx::scenes {
25
26class scenes_module final : public core::module<scenes_module> {
27
28 inline static const auto is_registered = register_module(stage::normal);
29
30public:
31
32 struct line {
33 sbx::math::vector4 position;
34 sbx::math::color color;
35 }; // struct line
36
38
39 ~scenes_module() override;
40
41 auto update() -> void override;
42
43 auto create_scene(const std::string& name = "Scene") -> scenes::scene&;
44
45 auto load_scene(const utility::hashed_string& name, const std::filesystem::path& path) -> scenes::scene&;
46
47 auto close_scene(const utility::hashed_string& name) -> void;
48
49 auto set_active_scene(const utility::hashed_string& name) -> void;
50
51 auto active_scene() -> scenes::scene& {
52 return *_active_scene;
53 }
54
55 auto active_scene() const -> const scenes::scene& {
56 return *_active_scene;
57 }
58
59 auto find_scene(const utility::hashed_string& name) -> memory::observer_ptr<scenes::scene> {
60 if (auto entry = _scenes.find(name); entry != _scenes.end()) {
61 return entry->second.get();
62 }
63
64 return nullptr;
65 }
66
67 auto has_active_scene() const -> bool {
68 return _active_scene != nullptr;
69 }
70
71 auto scene_count() const -> std::size_t {
72 return _scenes.size();
73 }
74
75 auto save_scene(const std::filesystem::path& path) -> void {
76 if (_active_scene) {
77 _active_scene->save(path);
78 }
79 }
80
81 auto set_scene_viewport(std::string name) -> void {
82 _scene_viewport = std::move(name);
83 }
84
85 auto scene_viewport() const -> const std::string& {
86 return _scene_viewport;
87 }
88
89 auto asset_registry() -> scenes::asset_registry& {
90 return _asset_registry;
91 }
92
93 auto asset_registry() const -> const scenes::asset_registry& {
94 return _asset_registry;
95 }
96
97 auto get_component_io_registry() -> component_io_registry& {
98 return _component_io_registry;
99 }
100
101 auto get_asset_io_registry() -> asset_io_registry& {
102 return _asset_io_registry;
103 }
104
105 auto get_component_io(const std::uint32_t id) -> scenes::component_io& {
106 return _component_io_registry.get(id);
107 }
108
109 auto has_component_io(const std::uint32_t id) const -> bool {
110 return _component_io_registry.has(id);
111 }
112
113 auto debug_lines() const -> const std::vector<line>&;
114
115 auto clear_debug_lines() -> void;
116
117 auto add_debug_line(const sbx::math::vector3& start, const sbx::math::vector3& end, const sbx::math::color& color) -> void;
118
119 auto add_coordinate_arrows(const math::matrix4x4& transform, std::float_t length = 2.0f) -> void;
120
121 auto add_debug_plane(const sbx::math::vector3& origin, const sbx::math::vector3& v1, const sbx::math::vector3& v2, std::uint32_t n1, std::uint32_t n2, std::float_t s1, std::float_t s2, const sbx::math::color& color, const sbx::math::color& outline) -> void;
122
123 auto add_debug_volume(const math::matrix4x4& matrix, const math::volume& volume, const sbx::math::color& color) -> void;
124
125 auto add_debug_box(const math::matrix4x4& matrix, const math::volume& volume, const sbx::math::color& color) -> void;
126
127 auto add_debug_circle(const math::vector3& center, const std::float_t radius, const math::vector3& normal, const math::color& color, const std::uint32_t segments = 32) -> void;
128
129 auto add_debug_sphere(const math::vector3& center, const std::float_t radius, const math::color& color, const std::uint32_t segments = 32) -> void;
130
131 auto add_debug_frustum(const math::matrix4x4& view, const math::matrix4x4& projection, const sbx::math::color& color) -> void;
132
133private:
134
135 scenes::asset_registry _asset_registry;
136 scenes::component_io_registry _component_io_registry;
137 scenes::asset_io_registry _asset_io_registry;
138
139 std::unordered_map<utility::hashed_string, std::unique_ptr<scenes::scene>> _scenes;
140 memory::observer_ptr<scenes::scene> _active_scene{nullptr};
141
142 std::vector<line> _debug_lines{};
143
144 std::string _scene_viewport{std::string{graphics::viewport::window_name}};
145
146}; // class scenes_module
147
148} // namespace sbx::scenes
149
150#endif // LIBSBX_SCENES_SCENE_MODULE_HPP_
Definition: module.hpp:92
Definition: vector4.hpp:24
RGBA color value type.
Definition: color.hpp:48
A non-owning pointer that can be used to observe the value of a pointer.
Definition: observer_ptr.hpp:29
Definition: scene.hpp:27
Definition: scenes_module.hpp:26
Definition: hashed_string.hpp:17
RGBA color representation and utilities.
Definition: scenes_module.hpp:32