-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathplatform_glfw.cpp
297 lines (233 loc) · 6.87 KB
/
platform_glfw.cpp
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
# include "platform.h"
# include "setup.h"
# if BACKEND(IMGUI_GLFW)
# include "application.h"
# include "renderer.h"
# include <GLFW/glfw3.h>
# if PLATFORM(WINDOWS)
# define GLFW_EXPOSE_NATIVE_WIN32
# include <GLFW/glfw3native.h>
# endif
# include <imgui.h>
# include "imgui_impl_glfw.h"
struct PlatformGLFW final
: Platform
{
static PlatformGLFW* s_Instance;
PlatformGLFW(Application& application);
bool ApplicationStart(int argc, char** argv) override;
void ApplicationStop() override;
bool OpenMainWindow(const char* title, int width, int height) override;
bool CloseMainWindow() override;
void* GetMainWindowHandle() const override;
void SetMainWindowTitle(const char* title) override;
void ShowMainWindow() override;
bool ProcessMainWindowEvents() override;
bool IsMainWindowVisible() const override;
void SetRenderer(Renderer* renderer) override;
void NewFrame() override;
void FinishFrame() override;
void Quit() override;
float GetDisplayScaleFactor();
void UpdatePixelDensity();
Application& m_Application;
GLFWwindow* m_Window = nullptr;
bool m_QuitRequested = false;
bool m_IsMinimized = false;
bool m_WasMinimized = false;
Renderer* m_Renderer = nullptr;
};
std::unique_ptr<Platform> CreatePlatform(Application& application)
{
return std::make_unique<PlatformGLFW>(application);
}
PlatformGLFW::PlatformGLFW(Application& application)
: m_Application(application)
{
}
bool PlatformGLFW::ApplicationStart(int argc, char** argv)
{
if (!glfwInit())
return false;
return true;
}
void PlatformGLFW::ApplicationStop()
{
glfwTerminate();
}
bool PlatformGLFW::OpenMainWindow(const char* title, int width, int height)
{
if (m_Window)
return false;
glfwWindowHint(GLFW_VISIBLE, 0);
using InitializerType = bool (*)(GLFWwindow* window, bool install_callbacks);
InitializerType initializer = nullptr;
# if RENDERER(IMGUI_OGL3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
# if PLATFORM(MACOS)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
# ifdef GLFW_COCOA_RETINA_FRAMEBUFFER
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GL_TRUE);
# endif
# ifdef GLFW_COCOA_GRAPHICS_SWITCHING
glfwWindowHint(GLFW_COCOA_GRAPHICS_SWITCHING, GL_TRUE);
# endif
# endif
initializer = &ImGui_ImplGlfw_InitForOpenGL;
# else
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
initializer = &ImGui_ImplGlfw_InitForNone;
# endif
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GL_TRUE);
width = width < 0 ? 1440 : width;
height = height < 0 ? 800 : height;
m_Window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!m_Window)
return false;
if (!initializer || !initializer(m_Window, true))
{
glfwDestroyWindow(m_Window);
m_Window = nullptr;
return false;
}
glfwSetWindowUserPointer(m_Window, this);
glfwSetWindowCloseCallback(m_Window, [](GLFWwindow* window)
{
auto self = reinterpret_cast<PlatformGLFW*>(glfwGetWindowUserPointer(window));
if (!self->m_QuitRequested)
self->CloseMainWindow();
});
glfwSetWindowIconifyCallback(m_Window, [](GLFWwindow* window, int iconified)
{
auto self = reinterpret_cast<PlatformGLFW*>(glfwGetWindowUserPointer(window));
if (iconified)
{
self->m_IsMinimized = true;
self->m_WasMinimized = true;
}
else
{
self->m_IsMinimized = false;
}
});
auto onFramebuferSizeChanged = [](GLFWwindow* window, int width, int height)
{
auto self = reinterpret_cast<PlatformGLFW*>(glfwGetWindowUserPointer(window));
if (self->m_Renderer)
{
self->m_Renderer->Resize(width, height);
self->UpdatePixelDensity();
}
};
glfwSetFramebufferSizeCallback(m_Window, onFramebuferSizeChanged);
auto onWindowContentScaleChanged = [](GLFWwindow* window, float xscale, float yscale)
{
auto self = reinterpret_cast<PlatformGLFW*>(glfwGetWindowUserPointer(window));
self->UpdatePixelDensity();
};
glfwSetWindowContentScaleCallback(m_Window, onWindowContentScaleChanged);
UpdatePixelDensity();
glfwMakeContextCurrent(m_Window);
glfwSwapInterval(1); // Enable vsync
return true;
}
bool PlatformGLFW::CloseMainWindow()
{
if (m_Window == nullptr)
return true;
auto canClose = m_Application.CanClose();
glfwSetWindowShouldClose(m_Window, canClose ? 1 : 0);
return canClose;
}
void* PlatformGLFW::GetMainWindowHandle() const
{
# if PLATFORM(WINDOWS)
return m_Window ? glfwGetWin32Window(m_Window) : nullptr;
# else
return nullptr;
# endif
}
void PlatformGLFW::SetMainWindowTitle(const char* title)
{
glfwSetWindowTitle(m_Window, title);
}
void PlatformGLFW::ShowMainWindow()
{
if (m_Window == nullptr)
return;
glfwShowWindow(m_Window);
}
bool PlatformGLFW::ProcessMainWindowEvents()
{
if (m_Window == nullptr)
return false;
if (m_IsMinimized)
glfwWaitEvents();
else
glfwPollEvents();
if (m_QuitRequested || glfwWindowShouldClose(m_Window))
{
ImGui_ImplGlfw_Shutdown();
glfwDestroyWindow(m_Window);
return false;
}
return true;
}
bool PlatformGLFW::IsMainWindowVisible() const
{
if (m_Window == nullptr)
return false;
if (m_IsMinimized)
return false;
return true;
}
void PlatformGLFW::SetRenderer(Renderer* renderer)
{
m_Renderer = renderer;
}
void PlatformGLFW::NewFrame()
{
ImGui_ImplGlfw_NewFrame();
if (m_WasMinimized)
{
ImGui::GetIO().DeltaTime = 0.1e-6f;
m_WasMinimized = false;
}
}
void PlatformGLFW::FinishFrame()
{
if (m_Renderer)
m_Renderer->Present();
glfwSwapBuffers(m_Window);
}
void PlatformGLFW::Quit()
{
m_QuitRequested = true;
glfwPostEmptyEvent();
}
float PlatformGLFW::GetDisplayScaleFactor() {
float xscale = 1.f;
float yscale = 1.f;
auto monitor = glfwGetPrimaryMonitor();
if (monitor)
glfwGetMonitorContentScale(monitor, &xscale, &yscale);
return 0.5f * (xscale + yscale);
}
void PlatformGLFW::UpdatePixelDensity()
{
float xscale, yscale;
glfwGetWindowContentScale(m_Window, &xscale, &yscale);
float scale = xscale > yscale ? xscale : yscale;
# if PLATFORM(WINDOWS)
float windowScale = scale;
float framebufferScale = scale;
# else
float windowScale = GetDisplayScaleFactor();
float framebufferScale = scale;
# endif
SetWindowScale(windowScale); // this is how windows is scaled, not window content
SetFramebufferScale(framebufferScale);
}
# endif // BACKEND(IMGUI_GLFW)