From 26012d0b74b41acbf6964cc0070c99fdae612d25 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sun, 20 Apr 2025 15:57:18 +0300 Subject: [PATCH] Initial support for building and running SDL platform on macOS --- .../Vulkan/Mac/MacVulkanPlatform.cpp | 13 ++-- Source/Engine/Platform/Mac/MacPlatform.cpp | 4 ++ Source/Engine/Platform/Mac/MacWindow.cpp | 2 +- .../Engine/Platform/SDL/SDLPlatform.Mac.cpp | 72 ++++++++++++++++++- Source/Engine/Platform/SDL/SDLPlatform.h | 8 +++ Source/Engine/Platform/SDL/SDLWindow.cpp | 4 ++ Source/Engine/Platform/Types.h | 2 + Source/Tools/Flax.Build/Configuration.cs | 4 +- .../Tools/Flax.Build/Deps/Dependencies/SDL.cs | 10 +-- .../Flax.Build/Platforms/Mac/MacToolchain.cs | 8 +++ 10 files changed, 113 insertions(+), 14 deletions(-) diff --git a/Source/Engine/GraphicsDevice/Vulkan/Mac/MacVulkanPlatform.cpp b/Source/Engine/GraphicsDevice/Vulkan/Mac/MacVulkanPlatform.cpp index 137d39e0a..771011bea 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/Mac/MacVulkanPlatform.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/Mac/MacVulkanPlatform.cpp @@ -6,6 +6,7 @@ #include "../RenderToolsVulkan.h" #include "Engine/Platform/Window.h" #include +#include void MacVulkanPlatform::GetInstanceExtensions(Array& extensions, Array& layers) { @@ -17,10 +18,14 @@ void MacVulkanPlatform::CreateSurface(Window* window, VkInstance instance, VkSur { void* windowHandle = window->GetNativePtr(); NSWindow* nswindow = (NSWindow*)windowHandle; - VkMacOSSurfaceCreateInfoMVK surfaceCreateInfo; - RenderToolsVulkan::ZeroStruct(surfaceCreateInfo, VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK); - surfaceCreateInfo.pView = (void*)nswindow.contentView; - VALIDATE_VULKAN_RESULT(vkCreateMacOSSurfaceMVK(instance, &surfaceCreateInfo, nullptr, surface)); +#if PLATFORM_SDL + nswindow.contentView.wantsLayer = YES; + nswindow.contentView.layer = [CAMetalLayer layer]; +#endif + VkMacOSSurfaceCreateInfoMVK surfaceCreateInfo; + RenderToolsVulkan::ZeroStruct(surfaceCreateInfo, VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK); + surfaceCreateInfo.pView = (void*)nswindow.contentView; + VALIDATE_VULKAN_RESULT(vkCreateMacOSSurfaceMVK(instance, &surfaceCreateInfo, nullptr, surface)); } #endif diff --git a/Source/Engine/Platform/Mac/MacPlatform.cpp b/Source/Engine/Platform/Mac/MacPlatform.cpp index fca66cf42..92a7d39fe 100644 --- a/Source/Engine/Platform/Mac/MacPlatform.cpp +++ b/Source/Engine/Platform/Mac/MacPlatform.cpp @@ -423,11 +423,15 @@ String MacPlatform::GetMainDirectory() return path; } +#if !PLATFORM_SDL + Window* MacPlatform::CreateWindow(const CreateWindowSettings& settings) { return New(settings); } +#endif + int32 MacPlatform::CreateProcess(CreateProcessSettings& settings) { LOG(Info, "Command: {0} {1}", settings.FileName, settings.Arguments); diff --git a/Source/Engine/Platform/Mac/MacWindow.cpp b/Source/Engine/Platform/Mac/MacWindow.cpp index 49a30ab42..3b511ca75 100644 --- a/Source/Engine/Platform/Mac/MacWindow.cpp +++ b/Source/Engine/Platform/Mac/MacWindow.cpp @@ -1,6 +1,6 @@ // Copyright (c) Wojciech Figat. All rights reserved. -#if PLATFORM_MAC +#if PLATFORM_MAC && !PLATFORM_SDL #include "../Window.h" #include "Engine/Platform/Apple/AppleUtils.h" diff --git a/Source/Engine/Platform/SDL/SDLPlatform.Mac.cpp b/Source/Engine/Platform/SDL/SDLPlatform.Mac.cpp index 2ebf49b79..6cecca574 100644 --- a/Source/Engine/Platform/SDL/SDLPlatform.Mac.cpp +++ b/Source/Engine/Platform/SDL/SDLPlatform.Mac.cpp @@ -2,11 +2,81 @@ #if PLATFORM_SDL && PLATFORM_MAC -static_assert(false, "TODO"); +#include "SDLWindow.h" +#include "Engine/Core/Log.h" +#include "Engine/Core/Collections/Array.h" +#include "Engine/Engine/CommandLine.h" +#include "Engine/Engine/Engine.h" +#include "Engine/Engine/Time.h" +#include "Engine/Graphics/RenderTask.h" +#include "Engine/Input/Input.h" +#include "Engine/Input/Mouse.h" +#include "Engine/Platform/IGuiData.h" +#include "Engine/Platform/MessageBox.h" +#include "Engine/Platform/Platform.h" +#include "Engine/Platform/WindowsManager.h" +#include "Engine/Platform/Base/DragDropHelper.h" +#include "Engine/Platform/SDL/SDLClipboard.h" +#include "Engine/Platform/Unix/UnixFile.h" +#include "Engine/Profiler/ProfilerCPU.h" + +#include "Engine/Platform/Linux/IncludeX11.h" + +#include +#include +#include +#include +#include +#include +#include + +bool SDLPlatform::InitInternal() +{ + return false; +} + +bool SDLPlatform::UsesWindows() +{ + return false; +} + +bool SDLPlatform::UsesWayland() +{ + return false; +} + +bool SDLPlatform::UsesX11() +{ + return false; +} + +void SDLPlatform::PreHandleEvents() +{ +} + +void SDLPlatform::PostHandleEvents() +{ +} + +bool SDLWindow::HandleEventInternal(SDL_Event& event) +{ + return false; +} void SDLPlatform::SetHighDpiAwarenessEnabled(bool enable) { // TODO: This is now called before Platform::Init, ensure the scaling is changed accordingly during Platform::Init (see ApplePlatform::SetHighDpiAwarenessEnabled) } +DragDropEffect SDLWindow::DoDragDrop(const StringView& data) +{ + return DragDropEffect::None; +} + +DragDropEffect SDLWindow::DoDragDrop(const StringView& data, const Float2& offset, Window* dragSourceWindow) +{ + Show(); + return DragDropEffect::None; +} + #endif diff --git a/Source/Engine/Platform/SDL/SDLPlatform.h b/Source/Engine/Platform/SDL/SDLPlatform.h index 52f8b8bfe..7c88254cb 100644 --- a/Source/Engine/Platform/SDL/SDLPlatform.h +++ b/Source/Engine/Platform/SDL/SDLPlatform.h @@ -11,7 +11,10 @@ typedef struct tagMSG MSG; #elif PLATFORM_LINUX #include "Engine/Platform/Linux/LinuxPlatform.h" union _XEvent; +#elif PLATFORM_MAC +#include "Engine/Platform/Mac/MacPlatform.h" #else +static_assert(false, "Unsupported Platform"); #endif class SDLWindow; @@ -29,8 +32,13 @@ class FLAXENGINE_API SDLPlatform : public LinuxPlatform { using base = LinuxPlatform; +#elif PLATFORM_MAC +: public MacPlatform +{ + using base = MacPlatform; #else { + static_assert(false, "Unsupported Platform"); #endif friend SDLWindow; diff --git a/Source/Engine/Platform/SDL/SDLWindow.cpp b/Source/Engine/Platform/SDL/SDLWindow.cpp index 0d801c063..494ce4d7f 100644 --- a/Source/Engine/Platform/SDL/SDLWindow.cpp +++ b/Source/Engine/Platform/SDL/SDLWindow.cpp @@ -30,6 +30,10 @@ #endif #elif PLATFORM_LINUX #include "Engine/Platform/Linux/IncludeX11.h" +#elif PLATFORM_MAC + +#else +static_assert(false, "Unsupported Platform"); #endif #define DefaultDPI 96 diff --git a/Source/Engine/Platform/Types.h b/Source/Engine/Platform/Types.h index 2a7a5938f..891a9a09c 100644 --- a/Source/Engine/Platform/Types.h +++ b/Source/Engine/Platform/Types.h @@ -265,10 +265,12 @@ class AppleThread; typedef AppleThread Thread; class MacClipboard; typedef MacClipboard Clipboard; +#if !PLATFORM_SDL class MacPlatform; typedef MacPlatform Platform; class MacWindow; typedef MacWindow Window; +#endif class UnixNetwork; typedef UnixNetwork Network; class UserBase; diff --git a/Source/Tools/Flax.Build/Configuration.cs b/Source/Tools/Flax.Build/Configuration.cs index d78163e2e..7579c9f4f 100644 --- a/Source/Tools/Flax.Build/Configuration.cs +++ b/Source/Tools/Flax.Build/Configuration.cs @@ -300,7 +300,9 @@ namespace Flax.Build public static bool WithSDL(NativeCpp.BuildOptions options) { - bool supportedPlatform = options.Platform.Target == TargetPlatform.Windows || options.Platform.Target == TargetPlatform.Linux; + bool supportedPlatform = options.Platform.Target == TargetPlatform.Windows || + options.Platform.Target == TargetPlatform.Linux || + options.Platform.Target == TargetPlatform.Mac; return UseSDL && supportedPlatform; } } diff --git a/Source/Tools/Flax.Build/Deps/Dependencies/SDL.cs b/Source/Tools/Flax.Build/Deps/Dependencies/SDL.cs index 18f074df3..daac4a976 100644 --- a/Source/Tools/Flax.Build/Deps/Dependencies/SDL.cs +++ b/Source/Tools/Flax.Build/Deps/Dependencies/SDL.cs @@ -37,7 +37,6 @@ namespace Flax.Deps.Dependencies return new[] { TargetPlatform.Mac, - TargetPlatform.iOS, }; default: return new TargetPlatform[0]; } @@ -63,6 +62,7 @@ namespace Flax.Deps.Dependencies "-DSDL_RENDER_VULKAN=OFF", "-DSDL_DIRECTX=OFF", + "-DSDL_METAL=OFF", "-DSDL_VULKAN=OFF", "-DSDL_OPENGL=OFF", "-DSDL_OPENGLES=OFF", @@ -123,8 +123,9 @@ namespace Flax.Deps.Dependencies break; } case TargetPlatform.Linux: + case TargetPlatform.Mac: { - foreach (var architecture in new TargetArchitecture[] { TargetArchitecture.x64 }) + foreach (var architecture in new [] { TargetArchitecture.x64, TargetArchitecture.ARM64 }) { var buildDir = Path.Combine(root, "build-" + architecture.ToString()); @@ -147,11 +148,6 @@ namespace Flax.Deps.Dependencies } break; } - case TargetPlatform.Mac: - { - // TODO - break; - } } } diff --git a/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs b/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs index a9210274f..9a4261383 100644 --- a/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs +++ b/Source/Tools/Flax.Build/Platforms/Mac/MacToolchain.cs @@ -52,6 +52,14 @@ namespace Flax.Build.Platforms options.LinkEnv.InputLibraries.Add("Cocoa.framework"); options.LinkEnv.InputLibraries.Add("QuartzCore.framework"); options.LinkEnv.InputLibraries.Add("AVFoundation.framework"); + + // SDL3 requires the following frameworks: + options.LinkEnv.InputLibraries.Add("Foundation.framework"); + options.LinkEnv.InputLibraries.Add("GameController.framework"); + options.LinkEnv.InputLibraries.Add("Carbon.framework"); + options.LinkEnv.InputLibraries.Add("ForceFeedback.framework"); + options.LinkEnv.InputLibraries.Add("UniformTypeIdentifiers.framework"); + options.LinkEnv.InputLibraries.Add("CoreHaptics.framework"); } protected override void AddArgsCommon(BuildOptions options, List args)