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
Allows compiling with emscripten #897
Conversation
Thanks! If you know of additional steps that are unique to sdl2 for emscripten rust, I would be glad to hear them! We definitely need more documentation regarding everything that is not desktop related. |
This should be enough to get SDL2 working with the browser. I guess all the rest is up to the application using SDL2. Anyway I'm trying to port a simple project of mine over to the browser so I'll let you know if I stumble upon any blocks |
@Cobrand I have an almost working prototype here on the webgl branch: https://github.com/tanis2000/minigame-rust/tree/webgl But I still have an issue as it looks like for some reason I'm getting an invalid SDL2 renderer with the browser:
I can't figure out why this is happening though, do you have any clue? |
The error comes from the function in engine.rs L90~ |
That's not entirely true.
I am actually printing the list of OpenGL drivers that SDL2 is returning and I get only I tried skipping setting the index but the result is the same. There must be something else going on that I'm overlooking. |
This panic must come from some rust code, otherwise it'd be just a segfault. The message "invalid renderer" doesn't look like it's coming from rust code though, so it's probably an SDL message. Can you enable backtraces and maybe using the SDL documentation we'll be able to see more clearly? |
I've almost got there. I had to skip over the canvas completely and use the gl_create_context() function to get the correct OpenGL driver working. Now I'm able to clear out the screen ;) I need to find the equivalent of the canvas.present() call. In my C code I just call SDL_GL_SwapWindow() but I couldn't find a wrapper for that one and maybe there's a better way as this one fails on iOS, where I need to do something like:
|
Yes it looks like it should work. I have to rewrite some code dependent on canvas to actually see if it works again on all the different platforms, though. |
I moved forward and it's almost working, but I'm getting an allocation error that I'm not having on the desktop and I'm struggling figuring out where the actual problem lies. The current error is the following:
|
The problem could come from many places... If you could create a minimal example where this problem happens, we might be able to make some progress. That'll allow you to know whether it's coming from rust code, from the rust <-> C bindngs (bindgen?), from your C SDL2 library, from something else, ... |
There you go, minimum reproduction of the issue: https://github.com/tanis2000/rust-sdl2-wasm I didn't even need to go further and complete the drawing stuff as it shows the issue even before that. |
I don't know if it makes any sense but it looks like those kind of errors happen only when calling out to the sdl2::sys functions that in turn call the C code. Does this ring a bell by any chance? |
I narrowed down this issue further.. it's happening only if you call unsafe code in the loop. As soon as you call some unsafe code, like simple OpenGL calls, it starts corrupting. I wonder why though... |
Do you have an example of the call in question that crashes? I suspect that the sizes of the structs/pointers sent to opengl are the wrong size. I suspect the bindgen generation (which is for linux x86_64) is not adapted to webgl somehow. |
It can be pretty much anything.. just have a look at the current master of https://github.com/tanis2000/rust-sdl2-wasm That kinds of defeats what I said before though because it doesn't matter if I call unsafe code in the main loop.. it's more like that if I call into any unsafe code anywhere in the app it corrupts memory. But then I'm wondering.. since we're using the SDL2 version that's in the emscripten port, we shouldn't be using the one from this crate, or are we actually linking both for some reason? I have no clue what cargo is doing under the hood when it comes to those things. |
It looks like the closure needs to be saved on the heap to avoid stack-allocated memory to be released and thus leading to undefined behavior and memory corruption. I've updated the sample project with the correct code and it works fine. I've also updated my own little game project that compiles pretty much anywhere now, so we definitely have a sample project using Rust + SDL2 that compiles to all the desktop and mobile platforms and now to all major browsers as well, hurray! :D |
This PR simply allows compiling any project using SDL2 with cargo web. It's just a matter of avoiding linking the SDL2 library as the port from emscripten will be used instead.