Using Rust with SDL2 with iOS


#1

I have a small test project made with Rust that uses rust-sdl2 to manage windows, graphics, etc…
It’s pretty simple as it only display a window and clears it at the moment.

It works fine on macOS and I’m trying to make it work on iOS as well. I managed to compile the library for the simulator using cargo build --no-default-features --target x86_64-apple-ios --lib and I can see both the dylib and .a being generated.

I created a new iOS project in Xcode and added the library to the project. I also made sure to mark the function that I want to call with #[no_mangle] and added the extern "C" prefix.

I did something similar in the main view controller (I know it’s the wrong place as I should actually replace the main.m file and let SDL2 manage the whole application lifecycle, but I’ll get there later):

extern void run_loop();

- (void)viewDidLoad {
  [super viewDidLoad];
  run_loop();
}

I also added the path to the generated library to the project’s libraries search path.

Now when I compile it everything is fine, but the linker is complaining that it can’t find the SDL2 libraries. And that makes sense as I’m not including the iOS version of those libraries. The point is, is there any way to have Rust add the SDL2 iOS libraries inside my own library so that I do not need to carry around more dependencies or should I compile the iOS version of SDL2 by hand and add them to the project? Any recommendations? Cheers!


#2

In the meanwhile I managed to get it working compiling the SDL2 C libraries from within its own Xcode-iOS project and copying the .a libraries over to my target/[platform] folders and linking them in my own Xcode project.

Now I’ve started with the Android version. I wanted to use the SDL2 template but since Ant has been abandoned by Google I’ve started with a brand new Gradle project and I still need to make it work.

Anyway, if there’s anyone interested or that can lend a hand, I’ve made the project public at https://github.com/tanis2000/minigame-rust


#3

Always great to see people share back their hard-won insights (in the form of a public repo).
Thanks for sharing, it was an interesting read for me to see basic SDL2 setup!


#4

No worries :slight_smile:
I’m slowly making some progress with the Android version now. It’s a bit more complicated to get all the right pieces together though.


#5

Now I’m at a point where I managed to separate the Android gradle project in way that SDL lives in its own library project and the app itself is in a different project.

But I have a big issue because I can’t compile SDL2 as SDL_android_main.c is referencing SDL_main. But SDL_main is in my Rust code and it’s part of a different library that I will link together with SDL2 in the Android app.
It’s like a chain where my Rust code needs SDL2 and SDL2 needs my code. Any idea for a decent solution?


#6

The other approach is to remove SDL_android_main.c from my SDL2 library and reimplement those calls in my Rust code, but I can’t find any reference to SDL_Android_Init and SDL_SetMainReady in the sdl2 crate. Any clue?


#7

Got it working on Android as well. I’ve pushed the Android project to the repo on GitHub if anybody wants to play with it. It still needs a lot of love to make it better and more usable, but it gets the job done so far.