Skip to content

Commit

Permalink
Add missing Vulkan and Keyboard functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Marioalexsan committed May 23, 2024
1 parent 321101d commit c011349
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/SFML.Graphics/RenderWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,20 @@ public override bool HasFocus()
return sfRenderWindow_hasFocus(CPointer);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Create a Vulkan rendering surface
/// </summary>
/// <param name="vkInstance">Vulkan instance</param>
/// <param name="vkSurface">Created surface</param>
/// <param name="vkAllocator">Allocator to use</param>
/// <returns>True if surface creation was successful, false otherwise</returns>
////////////////////////////////////////////////////////////
public override bool CreateVulkanSurface(IntPtr vkInstance, out IntPtr vkSurface, IntPtr vkAllocator)
{
return sfRenderWindow_createVulkanSurface(CPointer, vkInstance, out vkSurface, vkAllocator);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Display the window on screen
Expand Down Expand Up @@ -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
}
}
26 changes: 26 additions & 0 deletions src/SFML.System/Allocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Runtime.InteropServices;
using System.Security;
using System;

namespace SFML.System
{
/// <summary>
/// Contains functions related to memory allocation.
/// For internal use only.
/// </summary>
public static class Allocation
{
/// <summary>
/// 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.
/// </summary>
/// <param name="ptr">Pointer to the memory to deallocate</param>
public static void Free(IntPtr ptr) => sfFree(ptr);

[DllImport(CSFML.system, CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
private static extern void sfFree(IntPtr ptr);
}
}
34 changes: 32 additions & 2 deletions src/SFML.Window/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,36 @@ public static Scancode Delocalize(Key key)
return sfKeyboard_delocalize(key);
}

// TODO Implement GetDescription
////////////////////////////////////////////////////////////
/// <summary>
/// Provide a string representation for a given scancode
/// <para/>
/// 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.
/// <para/>
/// The current keyboard layout set by the operating system is used to
/// interpret the scancode: for example, <see cref="Scancode.Semicolon"/> is
/// mapped to ";" for layout and to "é" for others.
/// </summary>
/// <remarks>
/// The result is OS-dependent: for example, <see cref="Scancode.LSystem"/>
/// is "Left Meta" on Linux, "Left Windows" on Windows and
/// "Left Command" on macOS.
/// </remarks>
/// <param name="code">Scancode to describe</param>
/// <returns>The localized description of the code</returns>
////////////////////////////////////////////////////////////
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;
}

////////////////////////////////////////////////////////////
/// <summary>
Expand All @@ -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);
Expand Down
25 changes: 23 additions & 2 deletions src/SFML.Window/Vulkan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,27 @@ public static class Vulkan
////////////////////////////////////////////////////////////
public static IntPtr GetFunction(string name) => sfVulkan_getFunction(name);

// TODO: Implement GetGraphicsRequiredInstanceExtensions
////////////////////////////////////////////////////////////
/// <summary>
/// Get Vulkan instance extensions required for graphics
/// </summary>
/// <returns>Vulkan instance extensions required for graphics</returns>
////////////////////////////////////////////////////////////
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]
Expand All @@ -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
}
}
17 changes: 17 additions & 0 deletions src/SFML.Window/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,20 @@ public override bool HasFocus()
return sfWindow_hasFocus(CPointer);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Create a Vulkan rendering surface
/// </summary>
/// <param name="vkInstance">Vulkan instance</param>
/// <param name="vkSurface">Created surface</param>
/// <param name="vkAllocator">Allocator to use</param>
/// <returns>True if surface creation was successful, false otherwise</returns>
////////////////////////////////////////////////////////////
public override bool CreateVulkanSurface(IntPtr vkInstance, out IntPtr vkSurface, IntPtr vkAllocator)
{
return sfWindow_createVulkanSurface(CPointer, vkInstance, out vkSurface, vkAllocator);
}

////////////////////////////////////////////////////////////
/// <summary>
/// Provide a string describing the object
Expand Down Expand Up @@ -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
}
}

0 comments on commit c011349

Please sign in to comment.