Rust-SDL2 / rust-sdl2

SDL2 bindings for Rust
MIT License
2.74k stars 468 forks source link

SDL_Android_Init and SDL_SetMainReady not exposed #670

Open tanis2000 opened 7 years ago

tanis2000 commented 7 years ago

I'm trying to build a project using SDL2 that can compile on both iOS and Android. While iOS is pretty straightforward and works fine just by compiling the static SDL2 libraries from their own Xcode iOS project, the Android project has to be tweaked to integrate with user code.

This lead me to having to create an Android library to compile the C sources of SDL2 but omitting the SDL_android_main.c as that would require SDL_main to be defined inside the SDL2 library itself which is something I need to avoid.

The solution I came up with is to just skip that SDL_android_main.c file and reimplement the JNIEXPORT int JNICALL Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array) function in Rust.

The problem is that the function needs both SDL_Android_Init and SDL_SetMainReady to be exposed as I need to call them before calling my own SDL_main.

Can they be exposed or am I asking for trouble? Have you got any idea of a different approach in case this is not the right path to follow? (Sorry but I'm new to Rust and there are still many moving parts of the whole toolchain that I do not know). Cheers!

Cobrand commented 7 years ago

I'm a little busy right now, butgetting in touch with the author of this article might be worthwhile: https://michaelfairley.com/blog/i-made-a-game-in-rust/

tanis2000 commented 7 years ago

I already am :)

Anyway I managed to get around this issue with the following code:

#[cfg(target_os="android")]
extern "C" {
    fn SDL_Android_Init(env: JNIEnv, cls: JClass);
    fn SDL_SetMainReady();
}

It's not elegant, but it gets the work done.

Cobrand commented 7 years ago

I can't seem to see where these functions are defined in the SDL2 headers, since it appears you've found something, could you tell me where did you find it? I'm especially interested by those JNIEnv and JClass structs of yours.

tanis2000 commented 7 years ago

I'm on mobile at the moment, but you can see where those come from by looking at my sources here https://github.com/tanis2000/minigame-rust/blob/master/src/lib.rs