diff --git a/src/experimental/platform/CMakeLists.txt b/src/experimental/platform/CMakeLists.txt index f2e7fba9..527c528f 100644 --- a/src/experimental/platform/CMakeLists.txt +++ b/src/experimental/platform/CMakeLists.txt @@ -39,6 +39,15 @@ target_sources(${MUJOCO_PLATFORM_TARGET_NAME} window.cc window.h ) + +if(APPLE) + set_source_files_properties(window_osx.mm PROPERTIES + COMPILE_FLAGS "-x objective-c++") + target_sources(${MUJOCO_PLATFORM_TARGET_NAME} PUBLIC + window_osx.mm + ) +endif() + target_include_directories(${MUJOCO_PLATFORM_TARGET_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include diff --git a/src/experimental/platform/window.cc b/src/experimental/platform/window.cc index abdc3aee..7361925e 100644 --- a/src/experimental/platform/window.cc +++ b/src/experimental/platform/window.cc @@ -34,6 +34,10 @@ #undef Status #endif +#if defined(__APPLE__) +extern void* GetNativeWindowOsx(void* window); +#endif + namespace mujoco::platform { static void InitImGui(SDL_Window* window, const LoadAssetFn& load_asset_fn, @@ -117,11 +121,17 @@ Window::Window(std::string_view title, int width, int height, Config config, SDL_GL_MakeCurrent(sdl_window_, gl_context); } - #ifdef __linux__ - SDL_SysWMinfo wmi; - SDL_VERSION(&wmi.version); - SDL_GetWindowWMInfo(sdl_window_, &wmi); + SDL_SysWMinfo wmi; + SDL_VERSION(&wmi.version); + SDL_GetWindowWMInfo(sdl_window_, &wmi); + + #if defined(__linux__) native_window_ = reinterpret_cast(wmi.info.x11.window); + #elif defined(__WIN32__) + native_window_ = reinterpret_cast(wmi.info.win.window); + #elif defined(__APPLE__) + native_window_ = + GetNativeWindowOsx(reinterpret_cast(wmi.info.cocoa.window)); #endif } diff --git a/src/experimental/platform/window_osx.mm b/src/experimental/platform/window_osx.mm new file mode 100644 index 00000000..5601d471 --- /dev/null +++ b/src/experimental/platform/window_osx.mm @@ -0,0 +1,22 @@ +// Copyright 2025 DeepMind Technologies Limited +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import +#import + +void* GetNativeWindowOsx(void* window) { + NSWindow* ns_window = (__bridge NSWindow*)window; + NSView* ns_view = [ns_window contentView]; + return (__bridge void*)ns_view; +}