hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/core/window.cpp
blob: 619d7e1a5a9fbea5cbb3941225ce1ece89f2ac21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <glad/glad.h>
#include "window.h"
#include "theme_manager.h"
#include "rendering/renderer.h"

#include <cstdint>

#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>

namespace Donut
{
    static bool     s_glfw_initialized = false;
    static uint32_t s_glfw_window_count = 0;

    Window::Window(const std::string& title, int width, int height)
        : m_title(title), m_width(width),
          m_height(height), m_is_closed(false)
    {
        init();
    }

    Window::~Window()
    {
        shutdown();
    }

    auto Window::init() -> void
    {
        DONUT_INFO("Initializing window: ", m_title, " (", m_width, "x", m_height, ")");

        if (!s_glfw_initialized)
        {
            int success = glfwInit();
            if (!success)
            {
                DONUT_ERROR("Could not initialize GLFW!");
                return;
            }

            s_glfw_initialized = true;
            DONUT_INFO("GLFW initialized successfully");
        }

        if (RendererAPI::get_api() == RendererAPI::API::Vulkan)
        {
            glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
        }
        else
        {
#ifdef __APPLE__
            glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
            glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
            glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
            glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
#endif
        }

        m_window = glfwCreateWindow(m_width, m_height, m_title.c_str(), nullptr, nullptr);
        if (!m_window)
        {
            DONUT_ERROR("Could not create GLFW window!");
            glfwTerminate();
            return;
        }

        DONUT_INFO("GLFW window created successfully");

        if (RendererAPI::get_api() != RendererAPI::API::Vulkan)
            glfwMakeContextCurrent(m_window);
        glfwSetWindowUserPointer(m_window, this);

        glfwSetErrorCallback(glfw_error_callback);
        glfwSetWindowCloseCallback(m_window, glfw_window_close_callback);
        glfwSetWindowSizeCallback(m_window,  glfw_window_size_callback);
        glfwSetWindowFocusCallback(m_window, glfw_window_focus_callback);
        glfwSetWindowPosCallback(m_window,   glfw_window_pos_callback);
        glfwSetKeyCallback(m_window,         glfw_key_callback);
        glfwSetCharCallback(m_window,        glfw_char_callback);
        glfwSetMouseButtonCallback(m_window, glfw_mouse_button_callback);
        glfwSetScrollCallback(m_window,      glfw_mouse_scroll_callback);
        glfwSetCursorPosCallback(m_window,   glfw_cursor_pos_callback);

        s_glfw_window_count++;
    }

    auto Window::shutdown() -> void
    {
        DONUT_INFO("Shutting down window: ", m_title);

        shutdown_im_gui();

        glfwDestroyWindow(m_window);
        s_glfw_window_count--;

        if (s_glfw_window_count == 0)
        {
            glfwTerminate();
            s_glfw_initialized = false;
            DONUT_INFO("GLFW terminated (no more windows)");
        }
    }

    auto Window::on_update() const -> void
    {
        glfwPollEvents();
        if (RendererAPI::get_api() != RendererAPI::API::Vulkan)
            glfwSwapBuffers(m_window);
    }

    auto Window::should_close() const -> bool
    {
        return glfwWindowShouldClose(m_window) || m_is_closed;
    }

    auto Window::set_cursor_locked(bool locked) -> void
    {
        m_cursor_locked = locked;
        glfwSetInputMode(m_window, GLFW_CURSOR, locked ?
                         GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
    }

    auto Window::set_cursor_visible(bool visible) -> void
    {
        m_cursor_visible = visible;
        glfwSetInputMode(m_window, GLFW_CURSOR, visible ?
                         GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN);
    }

    auto Window::init_im_gui() -> void
    {
        IMGUI_CHECKVERSION();
        ImGui::CreateContext();
        ImGuiIO& io = ImGui::GetIO();
        io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
        io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
#ifndef __APPLE__
        io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
#endif

        setup_im_gui_fonts();
        ThemeManager::set_theme(Theme::Dark);

        ImGuiStyle& style = ImGui::GetStyle();
        if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
        {
            style.WindowRounding = 0.0f;
            style.Colors[ImGuiCol_WindowBg].w = 1.0f;
        }

        ImGui_ImplGlfw_InitForOpenGL(m_window, true);
#ifdef __APPLE__
        ImGui_ImplOpenGL3_Init("#version 150");
#else
        ImGui_ImplOpenGL3_Init("#version 130");
#endif

        DONUT_INFO("ImGUI initialized successfully");
    }

    auto Window::setup_im_gui_fonts() -> void
    {
        ImGuiIO& io = ImGui::GetIO();
        io.Fonts->Clear();

        m_main_font = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Regular.ttf", 16.0f);
        if (!m_main_font)
        {
            DONUT_WARN("Failed to load Inter font, falling back to default");
            m_main_font = io.Fonts->AddFontDefault();
        }
        else
            DONUT_INFO("Successfully loaded Inter font");

        m_large_font = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Bold.ttf", 20.0f);
        if (!m_large_font)
            m_large_font = m_main_font;

        m_small_font = io.Fonts->AddFontFromFileTTF("assets/fonts/inter/static/Inter_18pt-Light.ttf", 12.0f);
        if (!m_small_font)
            m_small_font = m_main_font;

        io.FontDefault = m_main_font;
        DONUT_INFO("ImGUI fonts loaded successfully");
    }

    auto Window::shutdown_im_gui() -> void
    {
        ImGui_ImplOpenGL3_Shutdown();
        ImGui_ImplGlfw_Shutdown();
        ImGui::DestroyContext();

        DONUT_INFO("ImGUI shutdown");
    }

    auto Window::begin_im_gui_frame() -> void
    {
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();
    }

    auto Window::end_im_gui_frame() -> void
    {
        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

        ImGuiIO& io = ImGui::GetIO();
        if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
        {
            GLFWwindow* backup_current_context = glfwGetCurrentContext();
            ImGui::UpdatePlatformWindows();
            ImGui::RenderPlatformWindowsDefault();
            glfwMakeContextCurrent(backup_current_context);
        }
    }

    auto Window::glfw_error_callback(int error, const char* description) -> void
    {
        DONUT_ERROR("GLFW Error ({}): {}", error, description ? description : "");
    }

    auto Window::glfw_window_close_callback(GLFWwindow* window) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        WindowCloseEvent event;
        win->m_event_handler.on_event(event);
    }

    auto Window::glfw_window_size_callback(GLFWwindow* window, int width, int height) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        win->m_width = width;
        win->m_height = height;

        WindowResizeEvent event(width, height);
        win->m_event_handler.on_event(event);
    }

    auto Window::glfw_window_focus_callback(GLFWwindow* window, int focused) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));

        if (focused)
        {
            WindowFocusEvent event;
            win->m_event_handler.on_event(event);
        }
        else
        {
            WindowLostFocusEvent event;
            win->m_event_handler.on_event(event);
        }
    }

    auto Window::glfw_window_pos_callback(GLFWwindow* window, int xpos, int ypos) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        WindowMovedEvent event(xpos, ypos);
        win->m_event_handler.on_event(event);
    }

    auto Window::glfw_key_callback(GLFWwindow* window, int key, int scancode,
                                   int action, int mods) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));

        switch (action)
        {
            case GLFW_PRESS:
            {
                KeyPressedEvent event(key, false);
                win->m_event_handler.on_event(event);
            } break;
            case GLFW_RELEASE:
            {
                KeyReleasedEvent event(key);
                win->m_event_handler.on_event(event);
            } break;
            case GLFW_REPEAT:
            {
                KeyPressedEvent event(key, true);
                win->m_event_handler.on_event(event);
            } break;
        }
    }

    auto Window::glfw_char_callback(GLFWwindow* window, unsigned int keycode) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        KeyTypedEvent event(keycode);
        win->m_event_handler.on_event(event);
    }

    auto Window::glfw_mouse_button_callback(GLFWwindow* window, int button,
                                            int action, int mods) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));

        switch (action)
        {
            case GLFW_PRESS:
            {
                MouseButtonPressedEvent event(button);
                win->m_event_handler.on_event(event);
            } break;
            case GLFW_RELEASE:
            {
                MouseButtonReleasedEvent event(button);
                win->m_event_handler.on_event(event);
            } break;
        }
    }

    auto Window::glfw_mouse_scroll_callback(GLFWwindow* window, double x_offset, double y_offset) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        MouseScrolledEvent event((float)x_offset, (float)y_offset);
        win->m_event_handler.on_event(event);
    }

    auto Window::glfw_cursor_pos_callback(GLFWwindow* window, double x_pos, double y_pos) -> void
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        MouseMovedEvent event((float)x_pos, (float)y_pos);
        win->m_event_handler.on_event(event);
    }
}