SDEngine
Game Engine
Loading...
Searching...
No Matches
Application.hpp
Go to the documentation of this file.
1#pragma once
2#include <atomic>
3#include <expected>
4#include <functional>
5#include <string>
6#include <type_traits>
7#include <utility>
8
9#include "SD/arena.hpp"
13#include "SD/core/LayerList.hpp"
14#include "SD/core/Scene.hpp"
16#include "SD/core/View.hpp"
20#include "SD/export.hpp"
21
22namespace sd {
23struct EventVariant;
24struct Layer;
25struct LayoutManager;
27struct VulkanWindow;
28struct Window;
29} // namespace sd
30
31
32namespace sd {
33
35 std::string name = "SDEngine App";
36 int width = 1600;
37 int height = 900;
38 bool enableHotReload = true;
39 std::string gameSoPath = "libSandboxApp.so";
40};
41
42struct SD_EXPORT Application {
44
45 Application(const ApplicationSpecification& spec, RuntimeStateManager* state_manager = nullptr);
47
48 void run(const std::atomic<bool>* external_stop);
49 void frame();
50 void close() { is_running = false; }
51 void on_app_event(EventVariant& e);
52
53 template<typename T, typename... Args>
54 requires std::is_base_of_v<Layer, T>
55 T& push_layer(Args&&... args) {
56 auto& layer = global_layers.push_layer<T>(std::forward<Args>(args)...);
57 layer.app = this;
58 return layer;
59 }
60
61 [[nodiscard]] WindowId create_window(const WindowProps& props) {
62 return window_manager->create(props, engine_arena);
63 }
64 [[nodiscard]] Window& get_window(const WindowId id) const {
65 return window_manager->get_window(id);
66 }
67 [[nodiscard]] VulkanWindow& get_render_window(const WindowId id) const {
68 return window_manager->get_render_window(id);
69 }
70
71 template<typename T, typename... Args>
72 requires std::is_base_of_v<Layer, T>
73 T& push_window_layer(WindowId id, Args&&... args);
74
75 template<typename T, typename... Args>
76 requires std::is_base_of_v<View, T>
77 T& create_view(Arena* arena, std::string name, Args&&... args) {
78 auto& view = view_manager->create<T>(arena, std::move(name), std::forward<Args>(args)...);
79 view.m_app = this;
80 return view;
81 }
82
83 [[nodiscard]] ViewResult get_view(const ViewId id) const { return view_manager->get(id); }
84 [[nodiscard]] ViewResult get_view(const std::string& name) const {
85 return view_manager->get(name);
86 }
87 [[nodiscard]] std::expected<ViewId, ViewError> get_view_id(const std::string& name) const {
88 return view_manager->get_id(name);
89 }
90
91 [[nodiscard]] ViewError remove_view(ViewId id) { return view_manager->remove(id); }
92 [[nodiscard]] ViewError remove_view(const std::string& name) {
93 return view_manager->remove(name);
94 }
95
96 template<typename T, typename... Args>
97 requires std::is_base_of_v<Layer, T>
98 std::expected<std::reference_wrapper<T>, ViewError> push_layer_to_view(ViewId id, Args&&... args);
99
100 template<typename T, typename... Args>
101 requires std::is_base_of_v<Layer, T>
102 std::expected<std::reference_wrapper<T>, ViewError> push_layer_to_view(const std::string& name,
103 Args&&... args);
104
105 Scene* create_scene(Arena* arena, const std::string& name) {
106 return scene_manager.create(arena, name);
107 }
108 [[nodiscard]] Scene* get_scene(const std::string& name) const { return scene_manager.get(name); }
109
110 void clear_game_layers();
111
112 void request_restart() { m_restart_requested.store(true, std::memory_order_release); }
113 [[nodiscard]] bool consume_restart_request() {
114 return m_restart_requested.exchange(false, std::memory_order_acq_rel);
115 }
116
117 void request_shader_reload() { m_shader_reload_requested.store(true, std::memory_order_release); }
118 [[nodiscard]] bool consume_shader_reload_request() {
119 return m_shader_reload_requested.exchange(false, std::memory_order_acq_rel);
120 }
121 void dispatch_shader_reload();
122
123 [[nodiscard]] EngineServices services() const {
124 return EngineServices{
125 .glfw = *m_glfw_ctx,
126 .vulkan = *m_vulkan_ctx,
127 .renderer = *m_renderer,
128 .imgui = *m_imgui_ctx,
129 };
130 }
131
132 [[nodiscard]] ApplicationRuntime runtime() {
133 return ApplicationRuntime{
134 .views = *view_manager,
135 .scenes = scene_manager,
136 .layout = *layout_manager,
137 .events = app_event_manager,
138 .timer = timer,
139 .global_layers = global_layers,
140 .hot_reload_enabled = hot_reload_enabled,
141 };
142 }
143
144 [[nodiscard]] Arena* frame_arena() const { return m_frame_arena; }
145
146 // Data members
150
152
157
160
163
166
167 std::atomic<bool> m_restart_requested;
168 std::atomic<bool> m_shader_reload_requested;
169
174};
175
176
177} // namespace sd
178
179
180#include "impl/Application.inl"
Definition Application.hpp:22
ViewError
Definition View.hpp:27
Definition arena.hpp:85
Definition ApplicationRuntime.hpp:12
ViewManager & views
Definition ApplicationRuntime.hpp:13
Definition Application.hpp:34
int width
Definition Application.hpp:36
std::string gameSoPath
Definition Application.hpp:39
int height
Definition Application.hpp:37
bool enableHotReload
Definition Application.hpp:38
std::string name
Definition Application.hpp:35
Definition Application.hpp:42
ViewError remove_view(ViewId id)
Definition Application.hpp:91
void request_shader_reload()
Definition Application.hpp:117
FrameTimer timer
Definition Application.hpp:162
ViewManager * view_manager
Definition Application.hpp:154
SDImGuiContext * m_imgui_ctx
Definition Application.hpp:173
RuntimeStateManager * state_manager
Definition Application.hpp:161
std::expected< ViewId, ViewError > get_view_id(const std::string &name) const
Definition Application.hpp:87
ViewError remove_view(const std::string &name)
Definition Application.hpp:92
ViewResult get_view(const std::string &name) const
Definition Application.hpp:84
EngineServices services() const
Definition Application.hpp:123
GlfwContext * m_glfw_ctx
Definition Application.hpp:170
WindowManager * window_manager
Definition Application.hpp:153
bool game_code_reload_paused
Definition Application.hpp:149
Scene * get_scene(const std::string &name) const
Definition Application.hpp:108
Arena * m_frame_arena
Definition Application.hpp:165
ApplicationRuntime runtime()
Definition Application.hpp:132
Scene * create_scene(Arena *arena, const std::string &name)
Definition Application.hpp:105
ViewManager::ViewResult ViewResult
Definition Application.hpp:43
LayoutManager * layout_manager
Definition Application.hpp:155
std::atomic< bool > m_restart_requested
Definition Application.hpp:167
Arena * engine_arena
Definition Application.hpp:164
bool is_running
Definition Application.hpp:147
void close()
Definition Application.hpp:50
SceneManager scene_manager
Definition Application.hpp:156
Arena * frame_arena() const
Definition Application.hpp:144
bool consume_restart_request()
Definition Application.hpp:113
void request_restart()
Definition Application.hpp:112
T & create_view(Arena *arena, std::string name, Args &&... args)
Definition Application.hpp:77
VulkanWindow & get_render_window(const WindowId id) const
Definition Application.hpp:67
Window & get_window(const WindowId id) const
Definition Application.hpp:64
T & push_layer(Args &&... args)
Definition Application.hpp:55
EventManager app_event_manager
Definition Application.hpp:159
ApplicationSpecification app_spec
Definition Application.hpp:151
std::atomic< bool > m_shader_reload_requested
Definition Application.hpp:168
ViewResult get_view(const ViewId id) const
Definition Application.hpp:83
bool consume_shader_reload_request()
Definition Application.hpp:118
VulkanRenderer * m_renderer
Definition Application.hpp:172
VulkanContext * m_vulkan_ctx
Definition Application.hpp:171
bool hot_reload_enabled
Definition Application.hpp:148
LayerList global_layers
Definition Application.hpp:158
WindowId create_window(const WindowProps &props)
Definition Application.hpp:61
Definition EngineServices.hpp:10
GlfwContext & glfw
Definition EngineServices.hpp:11
Definition EventManager.hpp:12
Definition EventVariant.hpp:19
Tracks frame timing, fixed timestep accumulation, and CPU work time.
Definition FrameTimer.hpp:21
RAII wrapper for initializing and terminating Glfw. Also sets a glfwErrorCallback.
Definition GlfwContext.hpp:25
Definition LayerList.hpp:15
Definition Layer.hpp:12
Manages window layouts using ImGui DockBuilder for presets and INI for user layouts.
Definition LayoutManager.hpp:14
Definition RuntimeStateManager.hpp:24
Definition SDImGuiContext.hpp:27
Definition SceneManager.hpp:14
Definition Scene.hpp:9
Definition id_types.hpp:7
Definition ViewManager.hpp:21
std::expected< std::reference_wrapper< View >, ViewError > ViewResult
Definition ViewManager.hpp:41
Definition VulkanContext.hpp:22
Definition VulkanRenderer.hpp:10
Definition VulkanWindow.hpp:35
Definition id_types.hpp:29
Definition WindowManager.hpp:43
Definition WindowManager.hpp:26
Definition Window.hpp:49