0

I've had issues for a while now with reading a joystick/controller using SDL2 in C#.

I am using SDL2 for cross platform dev using MAUI for Mac and Windows

I tried to install SDL2 using brew: brew install sdl2 which installed successfully. Then I installed SDL-CS using NuGet, both in terminal and in the Rider NuGet Section, I checked and it is loaded into my csproj file already, yet the following code would return errors when building: using SDL2; When I built I would simply get this error:

0>JoystickPage.xaml.cs(1,7): Error CS0246 : The type or namespace name 'SDL2' could not be found (are you missing a using directive or an assembly reference?)
0>------- Finished building project: InfiniteConnect. Succeeded: False. Errors: 1. Warnings: 0

This is on minimal Verbose logging and diagnostics did not even say why, just that the build failed with one error.

I have tried to add lines into my csproj such as:


    <ItemGroup>
        <NativeLibrary Include="/opt/homebrew/opt/sdl2/lib/libSDL2.dylib" />
    </ItemGroup>
    <ItemGroup>
        <None Update="/opt/homebrew/opt/sdl2/lib/libSDL2.dylib">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
    </ItemGroup>

But again nothing worked, I attempted other things but also nothing, maybe I am not doing it right.

brew --prefix sdl2

simply returns the correct directory:

/opt/homebrew/opt/sdl2

I added DYLD_LIBRARY_PATH=/opt/homebrew/opt/sdl2/lib:$DYLD_LIBRARY_PATH to environment variables in maccatalyst which is what I am using to run with.

Am I missing something? I restarted Rider and cleaned and rebuilt every time.

EDIT: Here is where I target the libraries;

    <ItemGroup>
        <PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="SDL2-CS" Version="2.0.0" />
    </ItemGroup>
5
  • where's your PackageReference? Commented Aug 28, 2024 at 15:04
  • Are you trying to reference native libraries and use them as managed ones? Cause that's not how it works. Commented Aug 28, 2024 at 15:37
  • @Blindy I am not to sure to be honest! I tried to research on sdl2 with C# but there was honestly nothing, so that’s what chat gpt and GitHub AI told me Commented Aug 28, 2024 at 15:57
  • Your error is about C# code, so the problem is with how you include the .Net SDL package (SDL-CS in your case). Everything else isn't relevant, you're just polluting your question with noise. In fact, you don't even show how you reference the package. Commented Aug 28, 2024 at 15:58
  • @Blindy I added it to the post Commented Aug 28, 2024 at 18:00

1 Answer 1

1

You're still missing relevant information (specifically, what complete .Net framework you're binding against), but if we assume that you're using .Net Core then the package you want is SDL2-CS.NetCore:

enter image description here

Whereas SDL2-CS runs against the legacy .Net Framework:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

Ah ok sorry, I am using .NET 8.0
I have now managed! However, If I run my code with: using SDL2 and then initialise SDL2, it gives me a huge error about tried: '/opt/homebrew/lib/libSDL2.dll' (no such file) even though I want it to use dydl and not dll, how can I make it do this? If I however do: [DllImport("/opt/homebrew/lib/libSDL2.dylib", EntryPoint = "SDL_Init")] in my code without using SDL2 it works.
That I don't know, I use neither SDL nor MacOS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.