Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would I use libxmp in Unity? #587

Closed
RenegadeScooter opened this issue Apr 15, 2022 · 5 comments
Closed

How would I use libxmp in Unity? #587

RenegadeScooter opened this issue Apr 15, 2022 · 5 comments

Comments

@RenegadeScooter
Copy link

I'm a beginner developer, currently learning how to develop a game in Unity by developing a simple project that I plan to build upon later.

I really wanted to use some Creative-Commons-licensed tracker music composed by Andreas Viklund for my Unity project, and I decided to give this library a try because of its simple API. However, this library is coded in C and Unity uses C#, so I have to build the library into a .dll and then somehow get that running in Unity. Unity treats the .dll as a "native plugin," and the library contains several custom functions and data types.

How do I make this work?

@sezero
Copy link
Collaborator

sezero commented Apr 15, 2022

You need to write your own C# bindings, I guess. (I don't know anything about C#, so I can't help.)

@AliceLR
Copy link
Contributor

AliceLR commented Apr 15, 2022

First off, I've never used C#, so I don't know what I'm talking about. I have marginal experience with the Java equivalent of this.

Right, you're going to have to add native function declarations to your class that interfaces with libxmp as described here: https://docs.unity3d.com/Manual/NativePlugins.html (note: don't statically link libxmp unless your software is GPL or LGPL or you're using libxmp-lite. This seems to only matter for iOS.)

Unfortunately, they don't provide much in terms of examples of how to do this, but they link to this MSDN document that seems like it should be useful: https://docs.microsoft.com/en-us/dotnet/framework/interop/marshalling-data-with-platform-invoke?redirectedfrom=MSDN

The C# native interface and libxmp's API makes this a little obnoxious because it seems that C# chars are 16-bits. The xmp_context used by most libxmp function calls is a char * typedef instead of void * (??), and the native interface maps C char * to C# string. You might have to force 8-bit char strings here.

This could be completely wrong, though, as it seems like there's some separate handling for unmanaged pointers, which is what you'd actually want for xmp_context. I asked in the MZX Discord server to see if anyone who actually knows what they're doing can help.

@RenegadeScooter
Copy link
Author

I asked in the MZX Discord server to see if anyone who actually knows what they're doing can help.

Hear back from them yet? @AliceLR

@AliceLR
Copy link
Contributor

AliceLR commented Apr 20, 2022

No one responded either time I asked, sorry. :(

@sezero
Copy link
Collaborator

sezero commented Sep 26, 2022

I think this can be closed now.

@sezero sezero closed this as completed Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants