It seems like the combination of Rust, SDL2, and Emscripten is something that some people have made to work at various points in the past.
I have an issue that has me a bit stumped. I'm a complete newbie when it comes to Rust, so I can't discount that factor. I borrowed a tiny program from:
https://puddleofcode.com/story/definitive-guide-to-rust-sdl2-and-emscriptem
...that with a little coaxing, I was able to compile with --target=asmjs-unknown-emscripten
, but when run in firefox, I get a message in the console:
thread 'main' panicked at 'Invalid renderer', /home/blah/.cargo/registry/src/github.com-blah-blah/sdl2-0.35.1/src/sdl2/render.rs:990:13
...which if you look at line 990 in render.rs
, it has the obvious panic in :
/// Sets the color used for drawing operations (Rect, Line and Clear).
#[doc(alias = "SDL_SetRenderDrawColor")]
pub fn set_draw_color<C: Into<pixels::Color>>(&mut self, color: C) {
let (r, g, b, a) = color.into().rgba();
let ret = unsafe { sys::SDL_SetRenderDrawColor(self.raw, r, g, b, a) };
// Should only fail on an invalid renderer
if ret != 0 {
panic!("{}", get_error()) //LINE 990
}
}
...details on how SDL gets initialized and the renderer get created look like:
fn main() {
let ctx = sdl2::init().unwrap();
let video_ctx = ctx.video().unwrap();
let window = match video_ctx
.window("updating SDL2 example with rust and asmjs...", 640, 480)
.position_centered()
.opengl() //same results whether .opengl is used or not
.build() {
Ok(window) => window,
Err(err) => panic!("failed to create window: {}", err)
};
let mut renderer = match window
// replaced "renderer()" with "into_canvas()" since the "renderer()" method no
// longer seems available in the upgrade from rust-SDL2 v0.29.0 to v0.35.0.
// Compiling with v0.29.0 is apparently incompatible with my recent
// version of emscripten (2.0.31).
//.renderer()
.into_canvas()
.build() {
Ok(renderer) => renderer,
Err(err) => panic!("failed to create renderer: {}", err)
};
//...
...With the above running without issue. What I do know is that the program works as expected when compiled as a Linux native app. And I also know that my Emscripten installation is working, since I can create working C++ and SDL2 apps that work in the browser. And it only panics when dealing with the graphics system. The event loop in the Rust->asmjs app works for getting mouse and keyboard events. The source files for the small example with the panic are over at:
https://gist.github.com/gregbuchholz/7d731191fcea95b7450859aeb0f4eb20
...(which includes the main.rs, Cargo.toml, etc. files). The panic occurs in main.rs at line 68:
println!("I'm going to panic on the next statement...");
// with an "Invalid renderer" at sdl2/render.rs:990...
let _ = renderer.set_draw_color(black); //LINE 68
let _ = renderer.clear();
...anyone have thoughts on how to go about debugging this? Println!()
debugging would normally seem to be the name of the game here, but currently the various "renderer" objects don't support the debug fmt. (adding that may be the next step).
versions of interest:
$ rustc --version
rustc 1.55.0 (c8dfcfe04 2021-09-06)
$ emcc --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.31 (4fcbf0239ccca29771f9044c990b0d34fac6e2e7)
Copyright (C) 2014 the Emscripten authors (see AUTHORS.txt)
This is free and open source software under the MIT license.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
...there are various tutorials out there which attempt to get rust, sdl2, and some flavor of asmjs/wasm working, but they all seem to have bit-rotted. Any help would be greatly appreciated!
Thanks!