CoriEngine
Loading...
Searching...
No Matches
Application.cpp
Go to the documentation of this file.
1#include "Application.hpp"
2#include "Graphics/API.hpp"
11
12namespace Cori {
13 namespace Core {
14 Application* Application::s_Instance{ nullptr };
15
16 Application::Application(const char* windowName) : m_WorkerPool(std::thread::hardware_concurrency() == 1 ? 1 : std::thread::hardware_concurrency() - 1) {
17
18 //m_ManualStep = true;
19 CORI_CORE_ASSERT(!s_Instance, "Trying to construct application for the second time. Application already exists!");
20 s_Instance = this;
21
22 FileSystem::PathManager::Get();
23
24 m_Window = Window::Create(windowName, false);
25
26 m_Window->SetEventCallback(CORI_BIND_EVENT_FN(Application::OnEvent, CORI_PLACEHOLDERS(1)));
27 m_Window->SetVSync(false);
28
29 m_ImGuiLayer = new Internal::ImGuiLayer();
30
31 m_LayerStack.PushOverlay(m_ImGuiLayer);
32
33 AssetManager::Init();
34 World::SceneManager::Init();
36 Audio::Mixer::Init();
37
38 m_GameTimer.SetTickrate(120);
39 m_GameTimer.SetTickrateUpdateFunc(CORI_BIND_EVENT_FN(Application::TickrateUpdate, CORI_PLACEHOLDERS(1)));
40 CORI_CORE_INFO_TAGGED({ Logger::Tags::Core::Self }, "Cori Engine started.");
41 }
42
44 m_LayerStack.ClearStack();
45 AssetManager::Shutdown();
46 World::SceneManager::Shutdown();
48 Audio::Mixer::Shutdown();
49 }
50
52 s_Instance->OnEvent(event);
53 }
54
56 return s_Instance->m_WorkerPool.GetWorkerCount();
57 }
58
59 void Application::OnEvent(Event& event) {
60 EventDispatcher dispatcher(event);
61 dispatcher.Dispatch<WindowCloseEvent>(CORI_BIND_EVENT_FN(Application::OnWindowClose));
62
63 dispatcher.Dispatch<KeyReleasedEvent>([this](const KeyReleasedEvent& e) -> bool {
64 if (e.GetKeyCode() == CORI_KEY_F9) {
65 m_RenderImGui = !m_RenderImGui;
66 }
67 return false;
68 });
69 dispatcher.Dispatch<WindowResizeEvent>([](const WindowResizeEvent& e) -> bool {
70 Graphics::Internal::API::SetViewport(0, 0, e.GetWidth(), e.GetHeight());
71 return false;
72 });
73
74 for (auto it = m_LayerStack.end(); it != m_LayerStack.begin();) {
75 --it;
76 (*it)->OnEvent(event);
77 if (event.m_Handled || (*it)->IsModal()) {
78 break;
79 }
80 }
81 }
82
83 std::expected<void, CoriError<>> Application::PushLayer(Layer* layer) {
84 return s_Instance->m_LayerStack.PushLayerToQueue(layer);
85 }
86
87 std::expected<void, CoriError<>> Application::PushOverlay(Layer* overlay) {
88 return s_Instance->m_LayerStack.PushOverlayToQueue(overlay);
89 }
90
92 s_Instance->m_LayerStack.PopLayerToQueue();
93 }
94
96 s_Instance->m_LayerStack.PopOverlayToQueue();
97 }
98
99 void Application::SetBackgroundColor(const glm::vec4& color) {
100 s_Instance->m_BackgroundColor = color;
101 }
102
104 while(m_Running) {
106 {
107 CORI_PROFILE_SCOPE("Cori Engine Global Update");
108 m_CommandQueue.Execute();
109 m_GameTimer.Update();
110
111 {
112 CORI_PROFILE_SCOPE("Clear Color and Clear Framebuffer");
115 }
116
118
119 for (Layer* layer : m_LayerStack) {
120 layer->ActiveScene.BeginRender();
121 layer->OnUpdate(m_GameTimer);
122 layer->SceneUpdate(m_GameTimer);
123 layer->ActiveScene.EndRender();
124 }
125
127
128 {
129 CORI_PROFILE_SCOPE("ImGui Render");
130 m_ImGuiLayer->StartFrame();
131
132 if (m_RenderImGui) {
133 for (Layer* layer : m_LayerStack) {
134 layer->OnImGuiRender(m_GameTimer);
135 layer->SceneImGuiRender(m_GameTimer);
136 if (layer->IsModal()) {
137 break;
138 }
139 }
140 }
141
142 m_ImGuiLayer->EndFrame();
143 }
144
145 m_Window->OnUpdate();
146
147 m_LayerStack.ProcessQueue();
148 }
149 }
150 }
151
152 void Application::TickrateUpdate(GameTimer& gameTimer) {
153 for (Layer* layer : m_LayerStack) {
154 layer->SceneTickrateUpdate(gameTimer);
155 layer->OnTickUpdate(gameTimer);
156 if (layer->IsModal()) {
157 break;
158 }
159 }
160 }
161
162 bool Application::OnWindowClose() {
163 m_Running = false;
164 return true;
165 }
166 }
167}
#define CORI_CORE_ASSERT(x,...)
Definition Logger.hpp:1029
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
#define CORI_PLACEHOLDERS(x)
Definition Macros.hpp:4
#define CORI_BIND_EVENT_FN(x,...)
Definition Macros.hpp:27
#define CORI_PROFILER_FRAME_START()
Definition Profiler.hpp:11
#define CORI_PROFILE_SCOPE(name)
Definition Profiler.hpp:10
Main Application object, there can only be one Application object. Basically a root of the program.
static void PopOverlay()
Pops the overlay Layer from the LayerStack.
static void EmitEvent(Event &event)
Emits the event and propagates it thought the LayerStack.
Application(const char *windowName)
static void SetBackgroundColor(const glm::vec4 &color)
Setts the background color of the rendering canvas.
static std::expected< void, CoriError<> > PushLayer(Layer *layer)
Pushes a Layer to the LayerStack.
static void PopLayer()
Pops the Layer from the LayerStack.
void Run()
Run loop, internal.
static std::expected< void, CoriError<> > PushOverlay(Layer *overlay)
Pushes an overlay Layer to the LayerStack.
static uint16_t GetWorkerCount()
Returns a number of available worker threads.
Used in OnEvent methods to dispatch and handle Events.
Definition Event.hpp:69
An abstract class that is ment to be used as a template for defining events.
Definition Event.hpp:18
A GameTimer is responsible for managing everything that is connected with time, ticks,...
Definition Time.hpp:8
An abstract class that is ment to be used as a template for defining layers.
Definition Layer.hpp:13
static void SetClearColor(const glm::vec4 &color)
Definition API.hpp:21
static void Shutdown()
Definition API.cpp:14
static void SetViewport(const int32_t x, const int32_t y, const int32_t width, const int32_t height)
Definition API.hpp:17
static void ClearFramebuffer()
Definition API.hpp:25
Core systems of the engine are here.
Global engine namespace.
static constexpr char Self[]
Definition Logger.hpp:83