tetra icon indicating copy to clipboard operation
tetra copied to clipboard

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

Open aakodadi opened this issue 1 year ago • 0 comments

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:

     /// Draws the texture to the screen (or to a canvas, if one is enabled).
-    pub fn draw<P>(&self, ctx: &mut Context, params: P)
+    pub fn draw(&self, ctx: &mut Context, params: &DrawParams)
-    where
-        P: Into<DrawParams>,
     {
-        let params = params.into();

         graphics::set_texture(ctx, self);
         graphics::push_quad(
             ctx,
             0.0,
             0.0,
             self.width() as f32,
             self.height() as f32,
             0.0,
             0.0,
             1.0,
             1.0,
-            &params,
+            params,
         );
     } 
     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

aakodadi avatar Mar 20 '23 19:03 aakodadi