17cupsofcoffee / tetra

🎮 A simple 2D game framework written in Rust
MIT License
907 stars 63 forks source link

Pass DrawParams as a reference to the Texture's draw method #338

Open aakodadi opened 1 year ago

aakodadi commented 1 year ago

Summary

The Texture::draw() takes a DrawParams as a value. Which makes it impossible to reuse the DrawParams object.

Motivation/Examples

I was optimizing my program by reusing the same objects. I was able to reuse the Texture objects by calling the replace_data instead of creating a new object every frame. But I couldn't do the same for the DrawParams object because the draw method takes ownership of it. I also don't know the reason behind the use of generics in this case. Because I made a small change to code by eliminating the generics and accepting a reference instead of a value and it worked.

My changes:

     pub fn draw<P>(&self, ctx: &mut Context, params: P)
     where
         P: Into<DrawParams>,
     {
-        self.texture.draw(ctx, params)
+        self.texture.draw(ctx, &params.into())
     }

Alternatives Considered

No response