0

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.

https://github.com/Rust-SDL2/rust-sdl2/issues/884

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
        // replace "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 minimal reproducible example 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 rust/emscripten/sdl2 tutorials out there, but they all seem to have bit-rotted. If you've ever cured the "Invalid renderer" panic, I'd love to hear more about it.

Thanks!

Greg Buchholz
  • 900
  • 4
  • 17
  • 1
    Can you provide a [mre]? In particular, how do you initialize SDL2 and how is `renderer` created? – Jmb Oct 28 '21 at 06:36
  • Yes, the minimal reproducible example is at the gist mentioned above: https://gist.github.com/gregbuchholz/7d731191fcea95b7450859aeb0f4eb20 – Greg Buchholz Oct 28 '21 at 14:06
  • Here's the solution: https://users.rust-lang.org/t/sdl2-emscripten-asmjs-and-invalid-renderer-panic/66567 – Greg Buchholz Oct 30 '21 at 05:28
  • And here's a better get-a-simple-Rust-SDL2-emscripten program to compile: https://github.com/gregbuchholz/RuSDLem – Greg Buchholz Nov 10 '21 at 04:40

0 Answers0