title: Rust, SDL2, Emscripten and 'Invalid renderer' panic
body: <p>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.</p>
  <p><a href="https://github.com/Rust-SDL2/rust-sdl2/issues/884" rel="nofollow noreferrer">https://github.com/Rust-SDL2/rust-sdl2/issues/884</a></p>
  <p>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:</p>
  <p><a href="https://puddleofcode.com/story/definitive-guide-to-rust-sdl2-and-emscriptem" rel="nofollow noreferrer">https://puddleofcode.com/story/definitive-guide-to-rust-sdl2-and-emscriptem</a></p>
  <p>...that with a little coaxing, I was able to compile with <code>--target=asmjs-unknown-emscripten</code>, but when run in firefox, I get a message in the console:</p>
  <blockquote>
  <p>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</p>
  </blockquote>
  <p>...which if you look at line 990 in <code>render.rs</code>, it has the obvious panic in :</p>
  <pre><code>/// Sets the color used for drawing operations (Rect, Line and Clear).
  #[doc(alias = &quot;SDL_SetRenderDrawColor&quot;)]
  pub fn set_draw_color&lt;C: Into&lt;pixels::Color&gt;&gt;(&amp;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!(&quot;{}&quot;, get_error()) //LINE 990
      }
  }
  </code></pre>
  <p>...details on how SDL gets initialized and the renderer get created look like:</p>
  <pre><code>fn main() {
      let ctx = sdl2::init().unwrap();
      let video_ctx = ctx.video().unwrap();
  
      let window  = match video_ctx
          .window(&quot;updating SDL2 example with rust and asmjs...&quot;, 640, 480)
          .position_centered()
          .opengl()  //same results whether .opengl is used or not
          .build() {
              Ok(window) =&gt; window,
              Err(err)   =&gt; panic!(&quot;failed to create window: {}&quot;, err)
          };
  
      let mut renderer = match window
          // replace &quot;renderer()&quot; with &quot;into_canvas()&quot; since the &quot;renderer()&quot; 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) =&gt; renderer,
              Err(err) =&gt; panic!(&quot;failed to create renderer: {}&quot;, err)
          };
  //...
  </code></pre>
  <p>...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-&gt;asmjs app works for getting mouse and keyboard events.  The source files for the minimal reproducible example over at:</p>
  <p><a href="https://gist.github.com/gregbuchholz/7d731191fcea95b7450859aeb0f4eb20" rel="nofollow noreferrer">https://gist.github.com/gregbuchholz/7d731191fcea95b7450859aeb0f4eb20</a></p>
  <p>...(which includes the main.rs, Cargo.toml, etc. files).  The panic occurs in main.rs at line 68:</p>
  <pre><code>    println!(&quot;I'm going to panic on the next statement...&quot;); 
      // with an &quot;Invalid renderer&quot; at sdl2/render.rs:990...
      let _ = renderer.set_draw_color(black);  //LINE 68
      let _ = renderer.clear();
      
  </code></pre>
  <p>...anyone have thoughts on how to go about debugging this?  <code>Println!()</code> debugging would normally seem to be the name of the game here, but currently the various &quot;renderer&quot; objects don't support the debug fmt. (adding that may be the next step).</p>
  <p>versions of interest:</p>
  <pre><code>$ 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.
  </code></pre>
  <p>There are various rust/emscripten/sdl2 tutorials out there, but they all seem to have bit-rotted. If you've ever cured the &quot;Invalid renderer&quot; panic, I'd love to hear more about it.</p>
  <p>Thanks!</p>