From c0113493e97414f7ded12ab7666fa975168099e1 Mon Sep 17 00:00:00 2001 From: Miron Alexandru Date: Thu, 23 May 2024 00:45:43 +0300 Subject: [PATCH] Add missing Vulkan and Keyboard functions --- src/SFML.Graphics/RenderWindow.cs | 17 ++++++++++++++++ src/SFML.System/Allocation.cs | 26 +++++++++++++++++++++++ src/SFML.Window/Keyboard.cs | 34 +++++++++++++++++++++++++++++-- src/SFML.Window/Vulkan.cs | 25 +++++++++++++++++++++-- src/SFML.Window/Window.cs | 17 ++++++++++++++++ 5 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 src/SFML.System/Allocation.cs diff --git a/src/SFML.Graphics/RenderWindow.cs b/src/SFML.Graphics/RenderWindow.cs index 9d0608ce..749dbde7 100644 --- a/src/SFML.Graphics/RenderWindow.cs +++ b/src/SFML.Graphics/RenderWindow.cs @@ -329,6 +329,20 @@ public override bool HasFocus() return sfRenderWindow_hasFocus(CPointer); } + //////////////////////////////////////////////////////////// + /// + /// Create a Vulkan rendering surface + /// + /// Vulkan instance + /// Created surface + /// Allocator to use + /// True if surface creation was successful, false otherwise + //////////////////////////////////////////////////////////// + public override bool CreateVulkanSurface(IntPtr vkInstance, out IntPtr vkSurface, IntPtr vkAllocator) + { + return sfRenderWindow_createVulkanSurface(CPointer, vkInstance, out vkSurface, vkAllocator); + } + //////////////////////////////////////////////////////////// /// /// Display the window on screen @@ -938,6 +952,9 @@ private void Initialize() [DllImport(CSFML.graphics, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern Vector2i sfTouch_getPositionRenderWindow(uint Finger, IntPtr RelativeTo); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfRenderWindow_createVulkanSurface(IntPtr CPointer, IntPtr vkInstance, out IntPtr surface, IntPtr vkAllocator); #endregion } } diff --git a/src/SFML.System/Allocation.cs b/src/SFML.System/Allocation.cs new file mode 100644 index 00000000..d4da1168 --- /dev/null +++ b/src/SFML.System/Allocation.cs @@ -0,0 +1,26 @@ +using System.Runtime.InteropServices; +using System.Security; +using System; + +namespace SFML.System +{ + /// + /// Contains functions related to memory allocation. + /// For internal use only. + /// + public static class Allocation + { + /// + /// This function deallocates the memory being pointed to + /// using the free function from the C standard library. + /// + /// The memory must have been previously allocated using a call + /// to malloc. + /// + /// Pointer to the memory to deallocate + public static void Free(IntPtr ptr) => sfFree(ptr); + + [DllImport(CSFML.system, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern void sfFree(IntPtr ptr); + } +} diff --git a/src/SFML.Window/Keyboard.cs b/src/SFML.Window/Keyboard.cs index f17d67a5..68a8f856 100644 --- a/src/SFML.Window/Keyboard.cs +++ b/src/SFML.Window/Keyboard.cs @@ -629,7 +629,36 @@ public static Scancode Delocalize(Key key) return sfKeyboard_delocalize(key); } - // TODO Implement GetDescription + //////////////////////////////////////////////////////////// + /// + /// Provide a string representation for a given scancode + /// + /// The returned string is a short, non-technical description of + /// the key represented with the given scancode. Most effectively + /// used in user interfaces, as the description for the key takes + /// the users keyboard layout into consideration. + /// + /// The current keyboard layout set by the operating system is used to + /// interpret the scancode: for example, is + /// mapped to ";" for layout and to "é" for others. + /// + /// + /// The result is OS-dependent: for example, + /// is "Left Meta" on Linux, "Left Windows" on Windows and + /// "Left Command" on macOS. + /// + /// Scancode to describe + /// The localized description of the code + //////////////////////////////////////////////////////////// + public static string GetDescription(Scancode code) + { + // this returns an owning C pointer + var ptr = sfKeyboard_getDescription(code); + var description = Marshal.PtrToStringAnsi(ptr); + Allocation.Free(ptr); + + return description; + } //////////////////////////////////////////////////////////// /// @@ -656,7 +685,8 @@ public static void SetVirtualKeyboardVisible(bool visible) [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern Scancode sfKeyboard_delocalize(Key key); - // TODO Import sfKeyboard_getDescription + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern IntPtr sfKeyboard_getDescription(Scancode code); [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern void sfKeyboard_setVirtualKeyboardVisible(bool visible); diff --git a/src/SFML.Window/Vulkan.cs b/src/SFML.Window/Vulkan.cs index bc72574f..1d8a376a 100644 --- a/src/SFML.Window/Vulkan.cs +++ b/src/SFML.Window/Vulkan.cs @@ -34,7 +34,27 @@ public static class Vulkan //////////////////////////////////////////////////////////// public static IntPtr GetFunction(string name) => sfVulkan_getFunction(name); - // TODO: Implement GetGraphicsRequiredInstanceExtensions + //////////////////////////////////////////////////////////// + /// + /// Get Vulkan instance extensions required for graphics + /// + /// Vulkan instance extensions required for graphics + //////////////////////////////////////////////////////////// + public static string[] GetGraphicsRequiredInstanceExtensions() + { + unsafe + { + var extensionsPtr = sfVulkan_getGraphicsRequiredInstanceExtensions(out var count); + var extensions = new string[count]; + + for (uint i = 0; i < count; ++i) + { + extensions[i] = Marshal.PtrToStringAnsi(extensionsPtr[i]); + } + + return extensions; + } + } #region Imports [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] @@ -43,7 +63,8 @@ public static class Vulkan [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern IntPtr sfVulkan_getFunction(string name); - // TODO: Import sfVulkan_getGraphicsRequiredInstanceExtensions + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private unsafe static extern IntPtr* sfVulkan_getGraphicsRequiredInstanceExtensions(out uint count); #endregion } } \ No newline at end of file diff --git a/src/SFML.Window/Window.cs b/src/SFML.Window/Window.cs index d58a7b8f..cf4e0eaf 100644 --- a/src/SFML.Window/Window.cs +++ b/src/SFML.Window/Window.cs @@ -347,6 +347,20 @@ public override bool HasFocus() return sfWindow_hasFocus(CPointer); } + //////////////////////////////////////////////////////////// + /// + /// Create a Vulkan rendering surface + /// + /// Vulkan instance + /// Created surface + /// Allocator to use + /// True if surface creation was successful, false otherwise + //////////////////////////////////////////////////////////// + public override bool CreateVulkanSurface(IntPtr vkInstance, out IntPtr vkSurface, IntPtr vkAllocator) + { + return sfWindow_createVulkanSurface(CPointer, vkInstance, out vkSurface, vkAllocator); + } + //////////////////////////////////////////////////////////// /// /// Provide a string describing the object @@ -545,6 +559,9 @@ protected override void Destroy(bool disposing) [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] private static extern Vector2i sfTouch_getPosition(uint Finger, IntPtr RelativeTo); + + [DllImport(CSFML.window, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] + private static extern bool sfWindow_createVulkanSurface(IntPtr CPointer, IntPtr vkInstance, out IntPtr surface, IntPtr vkAllocator); #endregion } }