From fee0e530aa17af4336f55ca82c7f6cb5931793c3 Mon Sep 17 00:00:00 2001 From: secrart <79920995+secrart@users.noreply.github.com> Date: Mon, 13 Jun 2022 12:58:08 -0400 Subject: [PATCH] feat: Add new glfwAddCocoaMTKSubview function this new function allows glfw to better interface with Apple's metal API. Before one had to directly interface with the native cocoa window and replace GLFW's content view. Thus removing GLFW's ability to detect input from the window. Now this function gives user's the ability to add a Metal MTKView as a "subview" which allows the default glfw window view to still capture input while also displaying a Metal view. --- README.md | 1 + include/GLFW/glfw3native.h | 14 ++++++++++++++ src/cocoa_window.m | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index fe5a2c76..a990d108 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,7 @@ information on what to include when reporting a bug. ## Changelog + - Added `glfwAddCocoaMTKSubview` function for better metal support - Added `GLFW_PLATFORM` init hint for runtime platform selection (#1958) - Added `GLFW_ANY_PLATFORM`, `GLFW_PLATFORM_WIN32`, `GLFW_PLATFORM_COCOA`, `GLFW_PLATFORM_WAYLAND`, `GLFW_PLATFORM_X11` and `GLFW_PLATFORM_NULL` symbols to diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index 36934120..2669603e 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -275,6 +275,20 @@ GLFWAPI CGDirectDisplayID glfwGetCocoaMonitor(GLFWmonitor* monitor); * @ingroup native */ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* window); + +/*! @brief Adds an 'MTKView' to the window's main NSView as a subview + * + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED. + * + * @thread_safety Call this function from the main thread. + * + * @since Added in version 3.3. + * + * @ingroup native + */ +GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* window, void* view); + #endif #if defined(GLFW_EXPOSE_NATIVE_NSGL) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 3726f782..81bd9aec 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -30,6 +30,7 @@ #include #include +#import // Returns the style mask corresponding to the window settings // @@ -1950,3 +1951,20 @@ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* handle) return window->ns.object; } +GLFWAPI void glfwAddCocoaMTKSubview(GLFWwindow* handle, void* view) { + + _GLFWwindow* window = (_GLFWwindow*) handle; + _GLFW_REQUIRE_INIT_OR_RETURN(); + + if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA) + { + _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, + "Cocoa: Platform not initialized"); + return; + } + + [window->ns.view addSubview: (MTKView*) view]; + + +} +