Zen 0.3.0
Loading...
Searching...
No Matches
ZEN_Application.cpp
1#include <include/imgui/imgui_impl_sdl3.h>
2#include <zen/core/ZEN_Application.h>
3#include <zen/core/ZEN_Window.h>
4#include <zen/gui/ZEN_ImGuiLayer.h>
5#include <zen/inputs/ZEN_Input.h>
6#include <zen/log/ZEN_Log.h>
7#include <zen/particles/ZEN_ParticleTestLayer.h>
8#include <zen/time/ZEN_EngineTime.h>
9
10namespace Zen {
11 Application *Application::s_instance = nullptr;
12
13 Application::Application() {
14 s_instance = this;
15
16 Log::init();
17 WindowProperties properties = {"Zen Window Test", 1280, 720, true, false};
18 m_eventsDispatcher.registerListener(this);
19 m_window = Window::create(properties);
20 m_eventsDispatcher.registerListener(m_window.get());
21
22 ZEN_LOG_DEBUG("new ImGui");
23 m_ImGui = new ImGuiLayer;
24 pushLayer(m_ImGui);
25
26 // pushLayer(new ParticleTestLayer());
27 };
28
29 Application::~Application() {
30
31 };
32
33 bool Application::onEvent(const ZenEvent &event) {
34 if (event.header.type == EventType::Quit) {
35 ZEN_LOG_INFO("Quitting!");
36 m_isRunning = false;
37 return true;
38 };
39 return false;
40 }
41
42 void Application::run() {
43 ZEN_LOG_INFO("Running Application...");
44 while (m_isRunning) {
45 float currentTime = EngineTime::getTime();
46
47 DeltaTime dt = currentTime - m_previousTime;
48 m_previousTime = currentTime;
49
50 m_inputSystem.begin();
51
52 SDL_Event eventFromSDL;
53 while (SDL_PollEvent(&eventFromSDL)) {
54 ImGui_ImplSDL3_ProcessEvent(&eventFromSDL);
55
56 ZenEvent e = TranslateEvent(eventFromSDL);
57 if (e.header.type != EventType::None) {
58 m_eventBuffer.enqueue(e);
59 }
60 }
61 m_ImGui->begin();
62 while (!m_eventBuffer.isEmpty()) {
63 m_eventsDispatcher.dispatch(m_eventBuffer.dequeue());
64 }
65
66 Input::bind(&m_inputSystem);
67 for (auto &layer : m_layerList) {
68 layer->onUpdate(dt);
69 }
70
71 for (auto &layer : m_layerList) {
72 layer->onGUIRender();
73 }
74
75 Input::unbind();
76
77 m_ImGui->end();
78 m_inputSystem.end();
79 m_window->onUpdate();
80 };
81 ZEN_LOG_INFO("Closing Application...");
82 };
83
84 void Application::onUpdate(DeltaTime deltaTime) {}
85
86 void Application::pushLayer(Layer *layer) {
87 m_layerList.pushLayer(layer);
88 m_eventsDispatcher.registerListener(layer);
89 layer->onAttach();
90 }
91
92 void Application::pushOverlay(Layer *overlay) {
93 m_layerList.pushLayer(overlay);
94 m_eventsDispatcher.registerListener(overlay);
95 overlay->onAttach();
96 }
97
98 void Application::popLayer(Layer *layer) {
99 m_layerList.popLayer(layer);
100 m_eventsDispatcher.unregisterListener(layer);
101 }
102
103 void Application::popOverlay(Layer *overlay) {
104 m_layerList.popLayer(overlay);
105 m_eventsDispatcher.unregisterListener(overlay);
106 }
107
108 Application &Application::get() { return *s_instance; }
109
110 Window &Application::getWindow() { return *m_window; };
111
112 int Application::getPriority() const { return 1; }
113}; // namespace Zen