I'm teaching C# and game dev to high school students, and we're using Raylib-cs as a simple introduction to graphics APIs and libraries.
We've hit a small snag: We're swedish, and we'd like to use some special non-ascii letters – namely å, ä and ö (lots of swedish words use them). However, I can't get Raylib-cs to display anything above codepoint 127 – at least not using DrawText.
Instead, all I get is ?.
This is on Windows 10, 64-bit, 20H2. Using dotnet 5 (latest) and primarily the Raylib-cs available as a nuget package.
What I've tried so far:
Here's my simple test code (just writes out characters 0-255):
static void Main(string[] args)
{
int font_size = 10;
Raylib.InitWindow(800, font_size * 64, "åäö");
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.BEIGE);
for (int i = 0; i < 255; i++)
{
int col = i / 64;
int x = col * 200;
int y = (i % 64) * font_size;
string text = i.ToString() + " | " + ((char)i).ToString();
Raylib.DrawText(text, x, y, font_size, Color.BLACK);
}
Raylib.EndDrawing();
}
}
Result in windows (at least for me): Window title bar is "???", as are all characters beyond the second column. Result in debian: Window title bar is "åäö", and all characters are drawn as they should.
Has anyone come across this problem? Anyone got (tested) solutions?
Is there some known quirk in how C# specifically on windows handles strings or something?
The way the library imports DrawText
is faulty; its default behavior "marshals" string values to fit a legacy character encoding, not necessarily UTF-8: https://github.com/ChrisDill/Raylib-cs/blob/b1f46d33071387800559523950aa131448251461/Raylib-cs/Raylib.cs#L2116 (Specifying CharSet=Unicode probably won't help because the DrawText function doesn't use wide-character strings, but rather ordinary char* pointers interpreted as UTF-8.)
I see you have posted an issue to the GitHub repository. And fortunately, the library's author suggested two workarounds. As they said, one of them is: "Using [MarshalAs(UnmanagedType.LPUTF8Str)]
to marshal strings as UTF8."