deltaphc / raylib-rs

Rust bindings for raylib
Other
715 stars 123 forks source link

[question] How exactly do I draw to a RenderTexture2D? #174

Closed lipx1508 closed 8 months ago

lipx1508 commented 8 months ago

Title. I tried searching a lot through the documentation but I still can't find it. Was this feature implemented yet?

jestarray commented 8 months ago
let mut main_layer = rl
        .load_render_texture(&thread, GAME_WIDTH, GAME_HEIGHT)
        .expect("could not create texture");

            let mut d = rl.begin_drawing(&thread);
            d.clear_background(Color::BLACK);
            {
                let mut d = d.begin_texture_mode(&thread, &mut main_layer);
                d.clear_background(Color::BLACK);
                // do drawing rendertexture2d stuff in here

                // at the end of this scope, EndTextureMode() will be called automatically
             }
lipx1508 commented 8 months ago

Thank you!

jestarray commented 8 months ago

Most of the API is close to raylib, the main diff being that you don't have to call End functions because the drop check will call it when scope ends, so:

BeginDrawing();
    BeginTextureMode();
    // drawing things into the render texture here
    EndTextureMode();
EndDrawing();

the rust equiv is:

// using brackets and only calling begin because at the end of scope, End() will be called
{
let mut d = rl.begin_drawing();
  {
      let mut d  = d.begin_texture_mode(&thread, &mut render_texture);
      // we shadow d in this scope! you don't have to though if its confusing
      // drawing things into the render texture here

  // EndTextureMode(); automatically gets called at end of bracket scope    
  } 

// EndDrawing(); automatically gets called at end of bracket scope
}