aboutsummaryrefslogtreecommitdiff
path: root/src/Core/Window.cpp
blob: 13025cf88173549d0dcf8a3707df2c23300918a5 (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
#include "Window.h"
#include <cstdint>
#include <iostream>

namespace Donut
{
    static bool s_GLFWInitialized = false;
    static uint32_t s_GLFWWindowCount = 0;

    Window::Window(const std::string& title, int width, int height)
        : m_Title(title), m_Width(width), 
          m_Height(height), m_IsClosed(false)
    {
        Init();
    }

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

    void Window::Init()
    {
        if (!s_GLFWInitialized)
        {
            int success = glfwInit();
            if (!success)
            {
                std::cout << "Could not initialize GLFW!" << std::endl;
                return;
            }

            s_GLFWInitialized = true;
        }

        m_Window = glfwCreateWindow(m_Width, m_Height, m_Title.c_str(), nullptr, nullptr);
        if (!m_Window)
        {
            std::cout << "Could not create GLFW window!" << std::endl;
            glfwTerminate();
            return;
        }

        glfwMakeContextCurrent(m_Window);
        glfwSetWindowUserPointer(m_Window, this);

        glfwSetErrorCallback(GLFWErrorCallback);
        glfwSetWindowCloseCallback(m_Window, GLFWWindowCloseCallback);
        glfwSetWindowSizeCallback(m_Window, GLFWWindowSizeCallback);
        glfwSetWindowFocusCallback(m_Window, GLFWWindowFocusCallback);
        glfwSetWindowPosCallback(m_Window, GLFWWindowPosCallback);
        glfwSetKeyCallback(m_Window, GLFWKeyCallback);
        glfwSetCharCallback(m_Window, GLFWCharCallback);
        glfwSetMouseButtonCallback(m_Window, GLFWMouseButtonCallback);
        glfwSetScrollCallback(m_Window, GLFWMouseScrollCallback);
        glfwSetCursorPosCallback(m_Window, GLFWCursorPosCallback);

        s_GLFWWindowCount++;
    }

    void Window::Shutdown()
    {
        glfwDestroyWindow(m_Window);
        s_GLFWWindowCount--;

        if (s_GLFWWindowCount == 0)
        {
            glfwTerminate();
            s_GLFWInitialized = false;
        }
    }

    void Window::OnUpdate() const
    {
        glfwPollEvents();
        glfwSwapBuffers(m_Window);
    }

    bool Window::ShouldClose() const
    {
        return glfwWindowShouldClose(m_Window) || m_IsClosed;
    }

    void Window::GLFWErrorCallback(int error, const char* description)
    {
        std::cout << "GLFW Error (" << error << "): " << description << std::endl;
    }

    void Window::GLFWWindowCloseCallback(GLFWwindow* window)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        WindowCloseEvent event;
        win->m_EventHandler.OnEvent(event);
    }

    void Window::GLFWWindowSizeCallback(GLFWwindow* window, int width, int height)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        win->m_Width = width;
        win->m_Height = height;
        
        WindowResizeEvent event(width, height);
        win->m_EventHandler.OnEvent(event);
    }

    void Window::GLFWWindowFocusCallback(GLFWwindow* window, int focused)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        
        if (focused)
        {
            WindowFocusEvent event;
            win->m_EventHandler.OnEvent(event);
        }
        else
        {
            WindowLostFocusEvent event;
            win->m_EventHandler.OnEvent(event);
        }
    }

    void Window::GLFWWindowPosCallback(GLFWwindow* window, int xpos, int ypos)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        WindowMovedEvent event(xpos, ypos);
        win->m_EventHandler.OnEvent(event);
    }

    void Window::GLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        
        switch (action)
        {
            case GLFW_PRESS:
            {
                KeyPressedEvent event(key, false);
                win->m_EventHandler.OnEvent(event);
            } break;
            case GLFW_RELEASE:
            {
                KeyReleasedEvent event(key);
                win->m_EventHandler.OnEvent(event);
            } break;
            case GLFW_REPEAT:
            {
                KeyPressedEvent event(key, true);
                win->m_EventHandler.OnEvent(event);
            } break;
        }
    }

    void Window::GLFWCharCallback(GLFWwindow* window, unsigned int keycode)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        KeyTypedEvent event(keycode);
        win->m_EventHandler.OnEvent(event);
    }

    void Window::GLFWMouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        
        switch (action)
        {
            case GLFW_PRESS:
            {
                MouseButtonPressedEvent event(button);
                win->m_EventHandler.OnEvent(event);
            } break;
            case GLFW_RELEASE:
            {
                MouseButtonReleasedEvent event(button);
                win->m_EventHandler.OnEvent(event);
                break;
            }
        }
    }

    void Window::GLFWMouseScrollCallback(GLFWwindow* window, double xOffset, double yOffset)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        MouseScrolledEvent event((float)xOffset, (float)yOffset);
        win->m_EventHandler.OnEvent(event);
    }

    void Window::GLFWCursorPosCallback(GLFWwindow* window, double xPos, double yPos)
    {
        Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
        MouseMovedEvent event((float)xPos, (float)yPos);
        win->m_EventHandler.OnEvent(event);
    }
}