Skip to content

Commit 09bacfb

Browse files
committed
OpenGL example: allow resizing window.
1 parent cca5f47 commit 09bacfb

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

examples/opengl3_example/main.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
static GLFWwindow* window;
1818
static GLuint fontTex;
1919
static bool mousePressed[2] = { false, false };
20-
static ImVec2 mousePosScale(1.0f, 1.0f);
2120

2221
// Shader variables
2322
static int shader_handle, vert_handle, frag_handle;
@@ -162,7 +161,6 @@ void InitGL()
162161
if (!glfwInit())
163162
exit(1);
164163

165-
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
166164
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
167165
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
168166
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@@ -243,15 +241,7 @@ void InitGL()
243241

244242
void InitImGui()
245243
{
246-
int w, h;
247-
int display_w, display_h;
248-
glfwGetWindowSize(window, &w, &h);
249-
glfwGetFramebufferSize(window, &display_w, &display_h);
250-
mousePosScale.x = (float)display_w / w; // Some screens e.g. Retina display have framebuffer size != from window size, and mouse inputs are given in window/screen coordinates.
251-
mousePosScale.y = (float)display_h / h;
252-
253244
ImGuiIO& io = ImGui::GetIO();
254-
io.DisplaySize = ImVec2((float)display_w, (float)display_h); // Display size, in pixels. For clamping windows positions.
255245
io.DeltaTime = 1.0f / 60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our timestep is variable)
256246
io.PixelCenterOffset = 0.5f; // Align OpenGL texels
257247
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
@@ -294,7 +284,14 @@ void UpdateImGui()
294284
{
295285
ImGuiIO& io = ImGui::GetIO();
296286

297-
// Setup timestep
287+
// Setup resolution (every frame to accommodate for window resizing)
288+
int w, h;
289+
int display_w, display_h;
290+
glfwGetWindowSize(window, &w, &h);
291+
glfwGetFramebufferSize(window, &display_w, &display_h);
292+
io.DisplaySize = ImVec2((float)display_w, (float)display_h); // Display size, in pixels. For clamping windows positions.
293+
294+
// Setup time step
298295
static double time = 0.0f;
299296
const double current_time = glfwGetTime();
300297
io.DeltaTime = (float)(current_time - time);
@@ -304,7 +301,9 @@ void UpdateImGui()
304301
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
305302
double mouse_x, mouse_y;
306303
glfwGetCursorPos(window, &mouse_x, &mouse_y);
307-
io.MousePos = ImVec2((float)mouse_x * mousePosScale.x, (float)mouse_y * mousePosScale.y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
304+
mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
305+
mouse_y *= (float)display_h / h;
306+
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
308307
io.MouseDown[0] = mousePressed[0] || glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
309308
io.MouseDown[1] = mousePressed[1] || glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != 0;
310309

examples/opengl_example/main.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
static GLFWwindow* window;
1818
static GLuint fontTex;
1919
static bool mousePressed[2] = { false, false };
20-
static ImVec2 mousePosScale(1.0f, 1.0f);
2120

2221
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
2322
// If text or lines are blurry when integrating ImGui in your engine:
@@ -140,7 +139,6 @@ void InitGL()
140139
if (!glfwInit())
141140
exit(1);
142141

143-
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
144142
window = glfwCreateWindow(1280, 720, "ImGui OpenGL example", NULL, NULL);
145143
glfwMakeContextCurrent(window);
146144
glfwSetKeyCallback(window, glfw_key_callback);
@@ -153,15 +151,7 @@ void InitGL()
153151

154152
void InitImGui()
155153
{
156-
int w, h;
157-
int display_w, display_h;
158-
glfwGetWindowSize(window, &w, &h);
159-
glfwGetFramebufferSize(window, &display_w, &display_h);
160-
mousePosScale.x = (float)display_w / w; // Some screens e.g. Retina display have framebuffer size != from window size, and mouse inputs are given in window/screen coordinates.
161-
mousePosScale.y = (float)display_h / h;
162-
163154
ImGuiIO& io = ImGui::GetIO();
164-
io.DisplaySize = ImVec2((float)display_w, (float)display_h); // Display size, in pixels. For clamping windows positions.
165155
io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our time step is variable)
166156
io.PixelCenterOffset = 0.0f; // Align OpenGL texels
167157
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
@@ -228,6 +218,13 @@ void UpdateImGui()
228218
{
229219
ImGuiIO& io = ImGui::GetIO();
230220

221+
// Setup resolution (every frame to accommodate for window resizing)
222+
int w, h;
223+
int display_w, display_h;
224+
glfwGetWindowSize(window, &w, &h);
225+
glfwGetFramebufferSize(window, &display_w, &display_h);
226+
io.DisplaySize = ImVec2((float)display_w, (float)display_h); // Display size, in pixels. For clamping windows positions.
227+
231228
// Setup time step
232229
static double time = 0.0f;
233230
const double current_time = glfwGetTime();
@@ -238,7 +235,9 @@ void UpdateImGui()
238235
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
239236
double mouse_x, mouse_y;
240237
glfwGetCursorPos(window, &mouse_x, &mouse_y);
241-
io.MousePos = ImVec2((float)mouse_x * mousePosScale.x, (float)mouse_y * mousePosScale.y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
238+
mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
239+
mouse_y *= (float)display_h / h;
240+
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
242241
io.MouseDown[0] = mousePressed[0] || glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
243242
io.MouseDown[1] = mousePressed[1] || glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != 0;
244243

0 commit comments

Comments
 (0)