-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.cpp
More file actions
143 lines (118 loc) · 3.89 KB
/
Copy pathWindow.cpp
File metadata and controls
143 lines (118 loc) · 3.89 KB
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
#include "Window.h"
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
Window::Window()
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
m_window = glfwCreateWindow(1920, 1080, "miri-tfm", glfwGetPrimaryMonitor(), nullptr);
}
Window::~Window()
{
glfwDestroyWindow(m_window);
}
void Window::Init()
{
MakeCurrent();
InstallCallbacks();
InitImGui();
int width, height;
glfwGetFramebufferSize(m_window, &width, &height);
m_application = std::make_unique<Application>(width, height, this);
}
void Window::MakeCurrent() const
{
glfwMakeContextCurrent(m_window);
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
glfwSwapInterval(0);
}
void Window::InstallCallbacks()
{
glfwSetWindowUserPointer(m_window, this);
glfwSetCursorPosCallback(m_window, [](GLFWwindow* window, double xpos, double ypos)
{
Window* thisWindow = static_cast<Window*>(glfwGetWindowUserPointer(window));
thisWindow->OnCursorPos(xpos, ypos);
});
glfwSetMouseButtonCallback(m_window, [](GLFWwindow* window, int button, int action, int mods)
{
Window* thisWindow = static_cast<Window*>(glfwGetWindowUserPointer(window));
thisWindow->OnMouseButton(button, action, mods);
});
glfwSetScrollCallback(m_window, [](GLFWwindow* window, double xoffset, double yoffset)
{
Window* thisWindow = static_cast<Window*>(glfwGetWindowUserPointer(window));
thisWindow->OnScroll(xoffset, yoffset);
});
glfwSetFramebufferSizeCallback(m_window, [](GLFWwindow* window, int width, int height)
{
Window* thisWindow = static_cast<Window*>(glfwGetWindowUserPointer(window));
thisWindow->OnFramebufferSize(width, height);
});
}
void Window::InitImGui() const
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
ImGui::StyleColorsDark();
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);
ImGui_ImplOpenGL3_Init("#version 330 core");
}
void Window::MainLoop()
{
while (!glfwWindowShouldClose(m_window))
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
glfwPollEvents();
m_application->OnUpdate();
m_application->OnRender();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
glfwSwapBuffers(m_window);
}
}
void Window::OnCursorPos(double xpos, double ypos)
{
if (ImGui::GetIO().WantCaptureMouse) return;
m_application->OnCursorPos(xpos, ypos);
}
void Window::OnMouseButton(int button, int action, int mods)
{
if (ImGui::GetIO().WantCaptureMouse) return;
m_application->OnMouseButton(button, action, mods);
}
void Window::OnScroll(double xoffset, double yoffset)
{
if (ImGui::GetIO().WantCaptureMouse) return;
m_application->OnScroll(xoffset, yoffset);
}
void Window::OnFramebufferSize(int width, int height)
{
m_application->OnFramebufferSize(width, height);
}
float Window::GetTime() const
{
return static_cast<float>(glfwGetTime());
}