Why we are converting strings to sbyte*?
danilwhale opened this issue · comments
Before submitting a new issue, please verify and check:
- The issue is specific to Raylib-cs and not raylib
- I checked there is no similar issue already reported
- My code has no errors or misuse of Raylib-cs
Issue description
We can use CharSet
in DllImport
attribute or MarshalAs
attribute before string
parameter, instead of writing utility method to convert string
to sbyte*
I have implemented little program with current Raylib-cs string conversion, CharSet.Ansi
parameter and MarshalAs
attribute:
internal class Program
{
[DllImport(Raylib.NativeLibName, CallingConvention = CallingConvention.Cdecl)]
private static extern void InitWindow(int width, int height, [MarshalAs(UnmanagedType.LPUTF8Str)] string title);
[DllImport(Raylib.NativeLibName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, EntryPoint = "DrawText")]
private static extern void DrawTextCharSet(string text, int posX, int posY, int fontSize, Color color);
[DllImport(Raylib.NativeLibName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "DrawText")]
private static extern void DrawTextMarshalAs([MarshalAs(UnmanagedType.LPUTF8Str)] string text, int posX, int posY, int fontSize, Color color);
private static void Main()
{
InitWindow(800, 600, "hello");
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.RayWhite);
DrawTextCharSet("This is CharSet.Ansi - hi !! йцукенгшщзхъ", 0, 0, 20, Color.Black);
DrawTextMarshalAs("This is MarshalAs - hi !! йцукенгшщзхъ", 0, 20, 20, Color.Black);
Raylib.DrawText("This is Utf8Buffer - hi !! йцукенгшщзхъ", 0, 40, 20, Color.Black);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
}
}
And every line results in same text:
Environment
OS: Windows 11 23H2
OpenGL/GPU:
INFO: GL: OpenGL device information:
INFO: > Vendor: ATI Technologies Inc.
INFO: > Renderer: AMD Radeon(TM) Graphics
INFO: > Version: 3.3.0 Core Profile Context 24.3.1.240216
INFO: > GLSL: 4.60
Raylib-cs: 6.0.0
Hi,
What exactly is your issue? Are you having trouble rendering utf8 text?
This is easily possible.
Here is an example which renders directly from a string:
https://github.com/ChrisDill/Raylib-cs/blob/master/Examples/Text/FontLoading.cs#L84
Hi,
What exactly is your issue? Are you having trouble rendering utf8 text? This is easily possible.
Here is an example which renders directly from a string:
https://github.com/ChrisDill/Raylib-cs/blob/master/Examples/Text/FontLoading.cs#L84
No, I'm showing that current string conversion is probably not necessary, because C# provides methods for this already
Originally used MarshalAs attributes but switched to the unsafe/safe approach to provide users with more control and to make the marshalling code more explicit.
There is similar discussion around using LibraryImport so this may change if that idea proves itself out but otherwise there are no plans to change this.