Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aakodadi opened this issue Mar 20, 2023 · 0 comments
Open

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

aakodadi opened this issue Mar 20, 2023 · 0 comments
Labels
Type: Feature Request Improvements that could be made to the code/documentation.

Comments

@aakodadi
Copy link

aakodadi commented Mar 20, 2023

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 aakodadi added the Type: Feature Request Improvements that could be made to the code/documentation. label Mar 20, 2023
@aakodadi aakodadi changed the title Pass DrawParams as a reference to the Texture's draw method Passe DrawParams as a reference to the Texture's draw method Mar 20, 2023
@aakodadi aakodadi changed the title Passe DrawParams as a reference to the Texture's draw method Pass DrawParams as a reference to the Texture's draw method Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature Request Improvements that could be made to the code/documentation.
Projects
None yet
Development

No branches or pull requests

1 participant