Skip to content

Commit

Permalink
Backends: GLFW: Use glfwGetCurrentContext() instead of storing the gl…
Browse files Browse the repository at this point in the history
…fw window as global in order to better handle the situation where the caller might be self-managing multiple glfw contexts (ocornut#3012)
  • Loading branch information
bear24rw authored and Max Thrun committed Mar 17, 2021
1 parent a1a39c6 commit 7ac021a
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions backends/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ enum GlfwClientApi
GlfwClientApi_OpenGL,
GlfwClientApi_Vulkan
};
static GLFWwindow* g_Window = NULL; // Main window
static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown;
static double g_Time = 0.0;
static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {};
Expand Down Expand Up @@ -140,7 +139,6 @@ void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)

static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api)
{
g_Window = window;
g_Time = 0.0;

// Setup backend capabilities flags
Expand Down Expand Up @@ -175,9 +173,9 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw

io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
io.ClipboardUserData = g_Window;
io.ClipboardUserData = window;
#if defined(_WIN32)
io.ImeWindowHandle = (void*)glfwGetWin32Window(g_Window);
io.ImeWindowHandle = (void*)glfwGetWin32Window(window);
#endif

// Create mouse cursors
Expand Down Expand Up @@ -238,12 +236,14 @@ bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks)

void ImGui_ImplGlfw_Shutdown()
{
GLFWwindow* window = glfwGetCurrentContext();

if (g_InstalledCallbacks)
{
glfwSetMouseButtonCallback(g_Window, g_PrevUserCallbackMousebutton);
glfwSetScrollCallback(g_Window, g_PrevUserCallbackScroll);
glfwSetKeyCallback(g_Window, g_PrevUserCallbackKey);
glfwSetCharCallback(g_Window, g_PrevUserCallbackChar);
glfwSetMouseButtonCallback(window, g_PrevUserCallbackMousebutton);
glfwSetScrollCallback(window, g_PrevUserCallbackScroll);
glfwSetKeyCallback(window, g_PrevUserCallbackKey);
glfwSetCharCallback(window, g_PrevUserCallbackChar);
g_InstalledCallbacks = false;
}

Expand All @@ -255,14 +255,14 @@ void ImGui_ImplGlfw_Shutdown()
g_ClientApi = GlfwClientApi_Unknown;
}

static void ImGui_ImplGlfw_UpdateMousePosAndButtons()
static void ImGui_ImplGlfw_UpdateMousePosAndButtons(GLFWwindow* window)
{
// Update buttons
ImGuiIO& io = ImGui::GetIO();
for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
{
// 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.
io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0;
io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(window, i) != 0;
g_MouseJustPressed[i] = false;
}

Expand All @@ -272,41 +272,41 @@ static void ImGui_ImplGlfw_UpdateMousePosAndButtons()
#ifdef __EMSCRIPTEN__
const bool focused = true; // Emscripten
#else
const bool focused = glfwGetWindowAttrib(g_Window, GLFW_FOCUSED) != 0;
const bool focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0;
#endif
if (focused)
{
if (io.WantSetMousePos)
{
glfwSetCursorPos(g_Window, (double)mouse_pos_backup.x, (double)mouse_pos_backup.y);
glfwSetCursorPos(window, (double)mouse_pos_backup.x, (double)mouse_pos_backup.y);
}
else
{
double mouse_x, mouse_y;
glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
glfwGetCursorPos(window, &mouse_x, &mouse_y);
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y);
}
}
}

static void ImGui_ImplGlfw_UpdateMouseCursor()
static void ImGui_ImplGlfw_UpdateMouseCursor(GLFWwindow* window)
{
ImGuiIO& io = ImGui::GetIO();
if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(g_Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
return;

ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
{
// Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
else
{
// Show OS mouse cursor
// FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
glfwSetCursor(g_Window, g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
glfwSetCursor(window, g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}

Expand Down Expand Up @@ -352,11 +352,13 @@ void ImGui_ImplGlfw_NewFrame()
ImGuiIO& io = ImGui::GetIO();
IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");

GLFWwindow* window = glfwGetCurrentContext();

// Setup display size (every frame to accommodate for window resizing)
int w, h;
int display_w, display_h;
glfwGetWindowSize(g_Window, &w, &h);
glfwGetFramebufferSize(g_Window, &display_w, &display_h);
glfwGetWindowSize(window, &w, &h);
glfwGetFramebufferSize(window, &display_w, &display_h);
io.DisplaySize = ImVec2((float)w, (float)h);
if (w > 0 && h > 0)
io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
Expand All @@ -366,8 +368,8 @@ void ImGui_ImplGlfw_NewFrame()
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f / 60.0f);
g_Time = current_time;

ImGui_ImplGlfw_UpdateMousePosAndButtons();
ImGui_ImplGlfw_UpdateMouseCursor();
ImGui_ImplGlfw_UpdateMousePosAndButtons(window);
ImGui_ImplGlfw_UpdateMouseCursor(window);

// Update game controllers (if enabled and available)
ImGui_ImplGlfw_UpdateGamepads();
Expand Down

0 comments on commit 7ac021a

Please sign in to comment.